Home > Software engineering >  Caesar Cypher Issue
Caesar Cypher Issue

Time:10-25

Currently learning Python, and working on a Caesar Cypher... I can't seem to figure out why my decryption is spitting out the wrong text; currently it does this...

Plain text: Hello

Distance: 65

Encrypt: *&..0

Decrypt: gckkm

I've looked through other posts, but don't seem to find any that have the same issue with decryption - any help would be appreciated. Thank you!

##ENCYPTION
plainText = input("Enter plain text line: ")
distance = int(input("Enter the distance value: "))
code = ""
for ch in plainText:
    ordVal = ord(ch)
    cipherVal = ordVal   distance
    if cipherVal > 127 :
       cipherVal = distance - (127 - ordVal | 1)
    code  = chr(cipherVal)
print(code)


##DECRYPTION
code = input("Enter the coded text: ")
distance = int(input("Enter the distance value: "))
plainText = ""

for ch in code:
    ordVal = ord(ch)
    cipherVal = ordVal - distance
    if cipherVal < 0 :
        cipherVal = 127 - (distance - (ordVal - 1))
    plainText  = chr(cipherVal)
print(plainText)```

CodePudding user response:

I have debugged the code you posted. lets take for example the input 'm' and distance 20. the code will be 1 and in the ascii table this means 'SOH' - start of heading. when trying to copy the output text in the Console and paste it, it didn't know that kind of input. therefor, you should separate the encryption progress of numbers and letters, and make sure to make a difference between upper case and lower case. here is the ascii table: https://www.alpharithms.com/ascii-table-512119/

the reason for separating is to avoid non-input ascii codes. if you want to include other signs like '-', '_', etc. than you could ignore all 'cipherVals' between 0 to 31, or it will cause input problems as described above.

Do not worry about space and DEL, I checked and they do work in your code.

CodePudding user response:

There is a major flaw in your algorithm.
Assuming your cipherMessage will be printed on paper, you will have a problem with every cipherVal between 0 and 32 because these are non-printable characters (AKA control codes).
If they do not appear on paper, how will your correspondent will be able to enter them in decrypter?
Caesar was using a 26 character set while you are using a 127 character set (32 of them being non-printable). You can use your algorithm if you reduce your character set by excluding non-printable characters (first 32 characters plus DEL).

fcis =  32 # First Character In character Set
lcis = 126 # Last Character In character Set
scs = lcis - fcis   1 # Size of Character Set

# TODO: REPLACE THE FOLLOWING 2 LINES WITH INPUT()
distance = 20
plainText = "Caesar Cypher Issue"

code = ""
for ch in plainText:
  val = (ord(ch) - fcis   distance) % scs
  code  = chr( val   fcis)

print( "plainText:", plainText)
print( "codedText:", code)

#Decrypt
fcis =  32 # First Character In character Set
lcis = 126 # Last Character In character Set
scs = lcis - fcis   1 # Size of Character Set

# TODO: ADD 2 INPUT STATEMENTS
plainText = ""
for ch in code:
  val = (ord( ch) - fcis - distance   scs) % scs
  plainText  = chr( val   fcis)

print( "codedText:", code)
print( "plainText:", plainText)

Results:

plainText: Caesar Cypher Issue
codedText: Wuy(u'4W.%|y'4]((*y

codedText: Wuy(u'4W.%|y'4]((*y
plainText: Caesar Cypher Issue

You can even define your own custom character set, this way:

cset = " AaBbCcDdEeFfGgHh987654321"
scs = len( cset)

distance = 20
plainText = "Bach 6"

code = ""
for ch in plainText:
  val = cset.find(ch)
  if val<0:
    print( "Unacceptabler character")
    break
    
  code  = cset[ (val   distance) % scs]

print( "plainText:", plainText)
print( "codedText:", code)

# Decrypt
plaintext = ""
for ch in code:
  plaintext  = cset[ (cset.find( ch) - distance   scs) % scs]

print( "plainText:", plainText)

Results:

plainText: Bach 6
codedText: 34 e6g
plainText: Bach 6

Be sure your custom character set only use ASCII characters.
Unicode characters (and emojis) will not work with this algorithm.

  • Related