Search code examples
pythonencryptioncaesar-cipher

Caesar cipher to return message decrypted in 26 ways - Python


I'm a beginner and working on a code that should return a word/sentence decrypted in 26 ways (using all steps i the alphabet) on Python. I was able to create a standard code to decrypt word/sentence when the key is given, but I can not figure out how to get it decrypted with 26 different keys. Any tips on how to make it work?

Below is the code i created to decrypt a word/sentence.

def decrypt(message,key): 
    result = "" 

    # traverse text 
    for i in range(len(message)): 
        char = message[i] 

        # decrypt uppercase characters 
        if char in string.ascii_uppercase: 
            result += chr((ord(char) - key-65) % 26+65)
        elif char ==" ":
            result+= " "

        # decrypt lowercase characters 
        else: 
            result += chr((ord(char) - key-97) % 26+97)

    return result

print decrypt('Udymts',5)
print decrypt('Exxego ex hear',4)

Solution

  • you can use a loop from 1 to 26 to print all the values for Caesar cipher

    for i in range(1,26):
        print(decrypt('Udymts',i))
        print(decrypt('Exxego ex hear',i))