Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Caesar Cipher Help
#1
The following Caesar Cypher code is not working (Python 3). The text file is being written, but with incorrect cipher.

e.g. Input text as 'Hello' should be written as 'Uryyb!' with a key of 13. Instead it is being written as 'b'

# Caesar cypher function
def rot(text, key):

    # Iterate through each character in the message.
    for char in text:
        # Set cypher_text to an empty string to add to later
        cypher_text = ''
        # Check if the character is a letter (A-Z/a-z).
        if char.isalpha():
            # Get the Unicode number from of the character.
            num = ord(char)
            # If the final number is greater than 'z'/122..
            if (num + key) > 122:
                # If we go too far, work out how many spaces passed
                # 'a'/97 it should be using the proper key.
                x = (num + key) - 122
                # Add the chr version of x more characters passed
                # 'a'/97, take 1 to account for the 'a' position.
                # This adds a string character on to the cypher_text
                # variable.
                cypher_text += chr(x + ord('a') - 1)
            # If the rotated value doesn't go passed 'z'/122
            elif ((num + key <= 122)):
                # Use the key to add to the decimal version of the
                # character, add the chr version of the value to the
                # cypher text.
                cypher_text += chr(num + key)
        # Else, if the chracter is not a letter, simply add it as is.
        # This way we don't change symbols or spaces.
        else:
            cypher_text += char
    # Return the final result of the processed characters for use.
    return cypher_text


# Ask the user for their message
plain_input = input('Input the text you want to encode: ')
# Aks the user for the rotation key
rot_key = int(input('Input the key you want to use from 0 to 25: '))
# Secret message is the result of the rot function
secret_message = rot(plain_input, rot_key)
# Print out message for feedback
print('Writing the following cypher text to file:', secret_message)
# Write the message to file
with open('TestFile.txt', 'a+') as file:
    file.write(secret_message)
Reply
#2
On each iteration of for char in text: cypher_text is being emptied cypher_text = ''
Move cypher_text = '' to be set only once before the for loop.
Reply
#3
Are you sure your logic is correct? Should 'Zzzz' shift 1 be 'Aaaa' or '[aaa? Your code returns the latter. Normally I see a Caesar Cipher only using upper case letters. If you want to do upper and lower you probably need special code for each.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Rsa Cipher Paragoon2 3 578 Nov-27-2023, 12:30 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 453 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  Transposition Cipher Issues beginner_geek07 0 1,045 Apr-08-2022, 07:44 PM
Last Post: beginner_geek07
  Learning Python with a Caesar cipher Drone4four 5 4,678 Nov-21-2020, 07:21 PM
Last Post: bowlofred
  Coding caesar's cipher drewbty 3 2,720 May-16-2020, 10:05 AM
Last Post: DPaul
  cipher decryption tool nightfox82 0 1,297 Mar-25-2020, 06:36 AM
Last Post: nightfox82
  Caesar Cypher--- I don't understand why it doesn't work ironsheep 12 5,774 Nov-03-2018, 06:53 PM
Last Post: j.crater
  Newbie: Help with code related to Caesar Cipher jessiblah 2 3,363 May-15-2018, 04:28 PM
Last Post: nilamo
  Problem with caesar cipher lucaron 2 2,907 Feb-05-2018, 05:17 PM
Last Post: lucaron
  Decrypt Caesar Cipher WantToImprove 22 17,347 Jan-30-2018, 08:43 AM
Last Post: WantToImprove

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020