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


Messages In This Thread
Caesar Cipher Help - by pbrowne - Jun-29-2021, 07:20 AM
RE: Caesar Cipher Help - by Yoriz - Jun-29-2021, 07:39 AM
RE: Caesar Cipher Help - by deanhystad - Jun-30-2021, 02:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cesar Cipher ForsakenDusk 5 631 Apr-07-2024, 04:30 PM
Last Post: Pedroski55
Question Rsa Cipher Paragoon2 3 748 Nov-27-2023, 12:30 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 573 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  Learning Python with a Caesar cipher Drone4four 5 5,046 Nov-21-2020, 07:21 PM
Last Post: bowlofred
  Coding caesar's cipher drewbty 3 2,883 May-16-2020, 10:05 AM
Last Post: DPaul
  Caesar Cypher--- I don't understand why it doesn't work ironsheep 12 6,069 Nov-03-2018, 06:53 PM
Last Post: j.crater
  Newbie: Help with code related to Caesar Cipher jessiblah 2 3,470 May-15-2018, 04:28 PM
Last Post: nilamo
  Problem with caesar cipher lucaron 2 3,011 Feb-05-2018, 05:17 PM
Last Post: lucaron
  Decrypt Caesar Cipher WantToImprove 22 17,877 Jan-30-2018, 08:43 AM
Last Post: WantToImprove
  Simple Caesar Cipher Help as1221 6 5,520 Jun-15-2017, 10:22 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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