# Caesar Cipher - Gravity Falls Cryptic Messages 1

Posted by : Ali Khundmiri Last Updated : Aug. 10, 2017, 3:09 p.m.

I recently started watching Gravity falls and half way through season 1 I noticed a cryptic message at the end of each episode right after post credits. I went online and did a bit of research, and found that they have a message at the end of all episode. Sadly, they only have 2 seasons of 20 episodes each. But I took took it as 40 episodes of fun.

That same night I started solving them on a white board and quickly realised it would be much easier if i would be able to write a python script for this. And this blog is to share with you what I have come up with.

Hope you guys Like it!.

### Which Cipher to use?

I checked out various sites and they were all using Caesar Cipher for first few episodes. I was too curious to find out how did they know which cipher to use? I assumed there must have been a hint in the episode some where right? So i watched the pilot again, there was nothing in the episode anything to do with Caesar. Seriously i would have convened my self if they would have even mentioned Caesar Salad in the episode. But the hit was hidden in the intro of song.

So I opened Audacity and recorded the intro through speakers, When reversed, this mysterious female voice said “Three letters back”, It was not really audible first, but when you try to hear it, you will hear it. Because some people on the internet heard it as “He Deceived me” or even “Don’t Deceive me”.

### Writing The script

How Caesar Cipher works is that it shifts the alphabets in the same order one step wither clockwise or counter clockwise. This Pilot clearly wanted us to shift it three letter in reverse, but i had to have a few features in this script, such as

• what if it asks to to shift 8 letters back in any future episode.

• what if i have to use it else where?

• It must identify the valid words.

• It should show it the probability of how much sure it was of the sentence being in proper english

keeping this in mind i started writing a script.

Here’s what this script does.

1. takes the user input of Cryptic message.

2. Generate all of 26 possible keys.

3. Using each key, generate a possible decrypt.

4. check with the common and valid english words.

5. Calculate the probability.

6. If it has the probability higher than 20, print decrypts.

### Libraries Used

To keep things simple i decoded to use library and verify if words are proper english.
for this we have Enchant.

you can download it in terminal with the code, `pip install pyenchant`

### The Code

``````import osimport string

# pip install pyenchant# this library is used to check if the words in decrypted message are actually english.import enchant    '''

in a Caesar Cipher, the numbers are rotated clock wise or anti clock wise, it could be any of 26 possibilities.simple example:abc-bcaabc-cab'''

# DO NOT CHANGE ANY ON THE BELOW LINESintab = "abcdefghijklmnopqrstuvwxyz"d = enchant.Dict("en_US")translate_this = ""decrypt_list = []decrypt_score = []

# Step 1: get the Cryptic string.def get_crypted():    translate_this = input("enter the line which you believe is a Caesar Cipher : >> ")    translate_this = translate_this.lower()    print("you entered : " + translate_this)    return translate_this

# Step 2: get all 26 possible keys, for Caesar Cipher.def get_all_26(message):    # rotate the message 26 times    for x in range(0, 27):        out = get_outtab(x)        get_decrypt(message, out)

# Step 2.5: get the single key.def get_outtab(shift):    shift %= 26    alphabet = string.ascii_lowercase    shifted_alphabet = alphabet[shift:] + alphabet[:shift]    return shifted_alphabet

# Step 3: For each Key, get the decrypted String.def get_decrypt(str, outtab):    word = str.translate({ord(x): y for (x, y) in zip(intab, outtab)})    is_in_english(word)

# Check if decripted string has the valid english words.# It adds the word to decrypt_list and, moves to stage 2 check.def is_in_english(quote):    decrypt_list.append(quote)    level_2_check(quote)

# Level 2 check will give score.def level_2_check(line):    score = 0    words = line.split()    for word in words:        if d.check(word):            score += 1        else:            pass    decrypt_score.append(score)

# print out the output.def show_final_guess(input_word):    total_words = len(input_word.split())    for line in range(0,len(decrypt_list)):        probability = decrypt_score[line]/total_words * 100        if probability >= 20:            print(decrypt_list[line] +" - "+ str(probability) + "% certain.")

````if __name__ == "__main__":    crypt = get_crypted()    get_all_26(crypt)    print("\nThis is what I came up with :\n")    show_final_guess(crypt)````

### Github repository.

You can download the script from Github too.
Link : https://github.com/alikhundmiri/gravity_falls/blob/master/caesar_cipher_decrypter-en.py
Feel free to download the script and have fun.

### Happy Realisation

Later i realised that this code will work for a few episodes, later episodes use other ciphers like Atbash and A1Z26.

So, this is going to be one happy week for me. Ill be posting more blogs with other ciphers.

## Hopefully i will be writing the scripts for other Ciphers soon.

Do comment below how you liked this script.