Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Caesar Cipher Python decryption function.
import string
from time import sleep
alphabet = string.ascii_lowercase # "abcdefghijklmnopqrstuvwxyz"
def decrypt():
print("Welcome to Caesar Cipher Decryption.\n")
encrypted_message = input("Enter the message you would like to decrypt: ").strip()
print()
key = int(input("Enter key to decrypt: "))
decrypted_message = ""
for c in encrypted_message:
if c in alphabet:
position = alphabet.find(c)
new_position = (position - key) % 26
new_character = alphabet[new_position]
decrypted_message += new_character
else:
decrypted_message += c
print("\nDecrypting your message...\n")
sleep(2) # give an appearance of doing something complicated
print("Stand by, almost finished...\n")
sleep(2) # more of the same
print("Your decrypted message is:\n")
print(decrypted_message)
decrypt()
@procoder12-max
Copy link

procoder12-max commented Oct 21, 2021

thanks

@flaccoo
Copy link

flaccoo commented Mar 15, 2022

thanks

this ^

@peymanmajidi
Copy link

peymanmajidi commented Mar 21, 2022

cool, but I have another solution
you do not have to enter the key
the system tries to guess the right key

Example

Call:
result = decrypt('KYZJ ZJ R MVIP TFFC DVJJRXV')

Output:
Message: THIS IS A VERY COOL MESSAGE => Point: 6022 => Key{1..25}: 17

Impilimentation

# import words from linux dictionary
dicts = []
with open('/usr/share/dict/words') as f:
    dicts = f.readlines()
    
    
class Item:
    def __init__(self, word, point, key):
        self.word = word
        self.point = point
        self.key = key
        

def is_charachter(ch):
    return ch.upper() != ch.lower()


def caesar(word, key=3):
    if key < 0:
        key += 26
    if key > 26:
        key = key % 26
        
    encrypted = ''
    A = ord('A')
    
    for ch in word.upper():
        if is_charachter(ch):
            code = ord(ch) - A
            encrypted += chr(((code + key)%26)+A)
        else:
            encrypted += ch
            
    return encrypted

def in_dict(word):
    if word.lower()+"\n" in dicts:
        return True

def decrypt(words):
    result = []
    
    for i in range(25):
        decrypted = caesar(words, i)
        sentense = decrypted.split(' ')
        point = 0
        for word in sentense:
            if in_dict(word):
                point+=1000 + len(word)
                
        result.append( Item(decrypted, point, 26 - i))
        
    return result
            
        
            
result = decrypt(caesar("This is a very cool message", 17))
#or result = decrypt('KYZJ ZJ R MVIP TFFC DVJJRXV')


#return top 10 result
for item in sorted(result, key=lambda x: x.point, reverse=True )[:10]:
    print( "Message: ", item.word ,  " => Point: ",item.point," => Key{1..25}: " , item.key)

# OUTPUT
# >> Message:  THIS IS A VERY COOL MESSAGE  => Point:  6022  => Key{1..25}:  17
```PYTHON

@Ndichuh-Consultants
Copy link

Ndichuh-Consultants commented Jul 18, 2022

Thanks for the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment