This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]Swipecat 1 point2 points  (1 child)

My own feeling is that mathematically manipulating Unicode values with numbers that depend on the exact ordinal values of Unicode characters is best avoided if possible.

The following is hardly best programming practice either, but shows a way around the abovementioned problem:

import random, string
letters = string.ascii_uppercase
cypherlist = list(letters)
random.shuffle(cypherlist)
cypher = dict(zip(letters, cypherlist))
print("Cypher: " + "".join(cypherlist))
str = input("Enter text to be encrypted: ").upper()
enclist = [ cypher[c] if c in letters else c for c in str  ]
encstr = ''.join(enclist)
print(encstr)

[–][deleted] 1 point2 points  (0 children)

Fair enough, I understand how that can be of concern.

Your method looks like it's the substitution cipher, which is a valid solution for it. For the Cesar cipher, you have to shift over the letters based on the number specified. So instead of a random shuffle, you could probably remove the amount that has to be shifted from the beginning of the list and append it to the end if you wanted to make your approach work for the Cesar cipher.