In [74]:
!date

Mon Sep 16 23:48:07 CDT 2019


Hello there stalkers and welcome to

# day 17¶

Today I didn't do much of anything. I was of no use to anyone or anything.

Well, I was of litle use. Some days you have those days you know? Being of little use. Falling asleep on the couch for a few hours.

I did code though. I coded in code.

How bout tha'

Below is what I did and I am going to attempt to explain it because I barely understand what I did but the script looks fairly simple.

I got these (and the modified) scripts from a book called Cracking Codes with Python, a book by Al Sweigart. I've linked the online version there but if it gives you some sort of security warning..well...whatever.

The only thing you really need to know before diving in is that the Caesar Cipher, a code, the one that was used here was used by the Romans (most importantly, Caesar) used to communicate with his troops. The cipher begins with picking a key, a number usually. And the letters are shifting.

Let me show you rather than tell:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

This would be a key of 0 because the letters have shifted 0 places.

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z B C D E F G H I J K L M N O P Q R S T U V W X Y Z A

This would be a key of 1 because the ltters have shifted 1 place. Here's a (not so) easy way to do this in code.

In [87]:
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z = 'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a'

In [91]:
print(g+n+k+x + ' ' + b+n+v)

holy cow


and by the way, assigning all of those individual strings to the variables was not easy. It was so not easy in fact that I wrote a program to help me do which probably took me longer than it would have to type it out.

but I wrote the program instead and copy pasted the results, tacking the 'a' onto the end.

anything for an easy copy paste am I right? (I'm right)

In [86]:
alpha = 'abcdefghijklmnopqrstuvwxyz'
beta = []
for i in alpha:
beta.append(i)

print(beta)

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']


alright we finally make it to the code I was actually talking about earlier.

watch closely.

In [1]:
# import pyperclip # for the copy pasting

In [33]:
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.'
# this is like those rows of letters above

In [94]:
def caesar_cipher_encrypt_decrypt(mode, SYMBOLS, message, key):
"""
decrypts or encrypts a message with the symbols provided and shifted by the
key provided
"""
# ^ that's a doc string. it tells other people and more importantly, me later this
# week when I can't remember what this function does

import pyperclip
# fun fact, I didn't know you could import modules in a function
# maybe I did.

translated = '' # need a variable to return. make it empty first

for symbol in message:
if symbol in SYMBOLS:
symbolIndex = SYMBOLS.find(symbol)
# alrighty then so this for loop looks for symbols that match other symbols in the
# message variable/argument/parameter/thingy-that-is-message and matches them to
# our known symbols. note that if the message had these characters @#\$%^&*()_+=][]
# \|~ we'd be #@*ed

if mode == 'encrypt':
translatedIndex = symbolIndex + key
elif mode == 'decrypt':
translatedIndex = symbolIndex - key

# this little block takes the 'encrypt/decrypt' arg and either shifts our letters
# based on whether we are encrypting or decrypting

if translatedIndex >= len(SYMBOLS):
translatedIndex = translatedIndex - len(SYMBOLS)
elif translatedIndex < 0:
translatedIndex = translatedIndex + len(SYMBOLS)

# this block above accounts for if the translatedIndex is too high (which we set previously
# in the last code block) then it wraps it around. for example: if the symbol we are looking
# for comes AFTER '.', the last symbol in SYMBOLS, it subtracts the length of it so it can
# begin matching symbols again. it makes finding symbols CIRCULAR ooooooooooh

translated = translated + SYMBOLS[translatedIndex]
# eh I don't know what this does.
# just kidding, this builds the translated message whether it be the decryption or encryption

else:
translated = translated + symbol
# this tacks on extra symbols it doesn't understand

return(pyperclip.copy(translated))
# and this is my return statement. it hands me the data in a nice copy paste way.

In [95]:
# trying out my super snazzy function on an encryption from the book....
caesar_cipher_encrypt_decrypt('decrypt', SYMBOLS, 'XCBSw88S18A1S 2SB41SE .8zSEwAS50D5A5x81V', 22)


"But all else of the world was invisible."

wow...even the translated message is cryptic.

Below I have a function that is named "brute_force_blah_blah". Brute force is trying every single combination that we can think of. Ready for more code explanation?

In [1]:
def brute_force_caesar_cipher(message,SYMBOLS):
"""tries every key combination, takes encrypted message and a string of symbols"""

# remember your doc strings stalkers. super important for those of us later
# who want to know what your code does.
# if you want to see me rage quit from coding with your module, don't document.

key_list = [] # this is where we put the stuff we find so it's easily accessible

for key in range(len(SYMBOLS)):
translated = ''

# when you see double for loops, you know you're doing some programming power lifting.
# the top one sets translated to an empty string and runs the below code for the length
# of the entire string of symbols. This is because if we don't know the key, it could start
# ANYWHERE. so we test them all. SYMBOLS isn't too long, only 66 characters.

# if the below code looks familiar that's because it is the exact same code from above minus
# the encrypt/decrypt stuff. this translates the message using the key (every key it could be)
# from above.

for symbol in message:
if symbol in SYMBOLS:
symbolIndex = SYMBOLS.find(symbol)
translatedIndex = symbolIndex - key

if translatedIndex < 0:
translatedIndex = translatedIndex + len(SYMBOLS)

translated = translated + SYMBOLS[translatedIndex]

else:
translated = translated + symbol

key_list.append('key #%s: %s'%(key,translated))
# this adds the results to my cleverly created list from above and returns that list
# for our pleasure below.
return key_list

In [73]:
# let's try it for good measure.

brute_force_caesar_cipher('guv6Jv6Jz!J6rp5r7Jzr66ntrM','ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.')

Out[73]:
['key #0: guv6Jv6Jz!J6rp5r7Jzr66ntrM',
'key #1: ftu5Iu5Iy I5qo4q6Iyq55msqL',
'key #2: est4Ht4Hx0H4pn3p5Hxp44lrpK',
'key #3: drs3Gs3Gw9G3om2o4Gwo33kqoJ',
'key #4: cqr2Fr2Fv8F2nl1n3Fvn22jpnI',
'key #5: bpq1Eq1Eu7E1mkzm2Eum11iomH',
'key #6: aopzDpzDt6Dzljyl1DtlzzhnlG',
'key #7: ZnoyCoyCs5CykixkzCskyygmkF',
'key #8: YmnxBnxBr4BxjhwjyBrjxxfljE',
'key #9: XlmwAmwAq3AwigvixAqiwwekiD',
'key #10: Wklv.lv.p2.vhfuhw.phvvdjhC',
'key #11: Vjku?ku?o1?ugetgv?oguucigB',
'key #12: Uijt!jt!nz!tfdsfu!nfttbhfA',
'key #13: This is my secret message.',
'key #14: Sghr0hr0lx0rdbqds0ldrrZfd?',
'key #15: Rfgq9gq9kw9qcapcr9kcqqYec!',
'key #16: Qefp8fp8jv8pbZobq8jbppXdb ',
'key #17: Pdeo7eo7iu7oaYnap7iaooWca0',
'key #18: Ocdn6dn6ht6nZXmZo6hZnnVbZ9',
'key #19: Nbcm5cm5gs5mYWlYn5gYmmUaY8',
'key #20: Mabl4bl4fr4lXVkXm4fXllTZX7',
'key #21: LZak3ak3eq3kWUjWl3eWkkSYW6',
'key #22: KYZj2Zj2dp2jVTiVk2dVjjRXV5',
'key #23: JXYi1Yi1co1iUShUj1cUiiQWU4',
'key #24: IWXhzXhzbnzhTRgTizbThhPVT3',
'key #25: HVWgyWgyamygSQfShyaSggOUS2',
'key #26: GUVfxVfxZlxfRPeRgxZRffNTR1',
'key #27: FTUewUewYkweQOdQfwYQeeMSQz',
'key #28: ESTdvTdvXjvdPNcPevXPddLRPy',
'key #29: DRScuScuWiucOMbOduWOccKQOx',
'key #30: CQRbtRbtVhtbNLaNctVNbbJPNw',
'key #31: BPQasQasUgsaMKZMbsUMaaIOMv',
'key #32: AOPZrPZrTfrZLJYLarTLZZHNLu',
'key #33: .NOYqOYqSeqYKIXKZqSKYYGMKt',
'key #34: ?MNXpNXpRdpXJHWJYpRJXXFLJs',
'key #35: !LMWoMWoQcoWIGVIXoQIWWEKIr',
'key #36:  KLVnLVnPbnVHFUHWnPHVVDJHq',
'key #37: 0JKUmKUmOamUGETGVmOGUUCIGp',
'key #38: 9IJTlJTlNZlTFDSFUlNFTTBHFo',
'key #39: 8HISkISkMYkSECRETkMESSAGEn',
'key #40: 7GHRjHRjLXjRDBQDSjLDRR.FDm',
'key #41: 6FGQiGQiKWiQCAPCRiKCQQ?ECl',
'key #42: 5EFPhFPhJVhPB.OBQhJBPP!DBk',
'key #43: 4DEOgEOgIUgOA?NAPgIAOO CAj',
'key #44: 3CDNfDNfHTfN.!M.OfH.NN0B.i',
'key #45: 2BCMeCMeGSeM? L?NeG?MM9A?h',
'key #46: 1ABLdBLdFRdL!0K!MdF!LL8.!g',
'key #47: z.AKcAKcEQcK 9J LcE KK7? f',
'key #48: y?.Jb.JbDPbJ08I0KbD0JJ6!0e',
'key #49: x!?Ia?IaCOaI97H9JaC9II5 9d',
'key #50: w !HZ!HZBNZH86G8IZB8HH408c',
'key #51: v0 GY GYAMYG75F7HYA7GG397b',
'key #52: u90FX0FX.LXF64E6GX.6FF286a',
'key #53: t89EW9EW?KWE53D5FW?5EE175Z',
'key #54: s78DV8DV!JVD42C4EV!4DDz64Y',
'key #55: r67CU7CU IUC31B3DU 3CCy53X',
'key #56: q56BT6BT0HTB2zA2CT02BBx42W',
'key #57: p45AS5AS9GSA1y.1BS91AAw31V',
'key #58: o34.R4.R8FR.zx?zAR8z..v2zU',
'key #59: n23?Q3?Q7EQ?yw!y.Q7y??u1yT',
'key #60: m12!P2!P6DP!xv x?P6x!!tzxS',
'key #61: lz1 O1 O5CO wu0w!O5w  sywR',
'key #62: kyz0Nz0N4BN0vt9v N4v00rxvQ',
'key #63: jxy9My9M3AM9us8u0M3u99qwuP',
'key #64: iwx8Lx8L2.L8tr7t9L2t88pvtO',
'key #65: hvw7Kw7K1?K7sq6s8K1s77ousN']

Oh hey there, haven't seen you in awhile.

Did you find the message up there?

Unfortunately I didn't write a program that would automatically find english looking words for me in the brute-forced output. Maybe that can be for next time.

These messages I'm decrypting are straight from the book still. I'm not making them up or anything.

What did we learn?

I'm not sure. I hope you learned something because I'm still lost.

Below is me mining the rest of the messages. I didn't know where every single message was located, this post was starting to physically get way too long so I did all that coding "behind the scenes" as it were.

I suppose you're still here to read some sort of warm, encouraging message about programming and cryptography. Good. Because I have one.

GO READ MY NEXT FIRE POST STALKER.

peace.

In [55]:
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.'

In [59]:
first_sentence = brute_force_caesar_cipher('qeFIP?eGSeECNNS',SYMBOLS)
second_sentence = brute_force_caesar_cipher('5coOMXXcoPSZIWoQI', SYMBOLS)
third_sentence = brute_force_caesar_cipher('avnl1olyD4l\'ylDohww6DhzDjhuDil', SYMBOLS)
fourth_sentence = brute_force_caesar_cipher('z.GM?.cEQc. 70c.7KcKMKHA9AGFK', SYMBOLS)
fifth_sentence = brute_force_caesar_cipher('?MFYp2pPJJUpZSIJWpRdpMFY',SYMBOLS)
sixth_sentence = brute_force_caesar_cipher('ZqH8sl5HtqHTH4s3lyvH5zH5spH4t pHzqHlH3l5K', SYMBOLS)
seventh_sentence = brute_force_caesar_cipher('Zfbi,!tif!xpvme!qspcbcmz!fbu!nfA',SYMBOLS)

In [71]:
print(str(first_sentence[34]) + '\n' +
str(second_sentence[44]) + '\n' +
str(third_sentence[7]) + '\n' +
str(fourth_sentence[32]) + '\n' +
str(fifth_sentence[45]) + '\n' +
str(sixth_sentence[11]) + '\n' +
str(seventh_sentence[1]))

key #34: I love my kitty
key #44: My kitty loves me
key #7: Together we're happy as can be
key #32: Though my head has suspicions
key #45: That I keep under my hat
key #11: Of what if I shrank to the size of a rat.
key #1: Yeah, she would probably eat me.
`