I'm writing a random substitution cipher, and it works well when being used to write regular sentences. However, when I try to test 'abbcccddddeeeee', I will get something back like 'G H H I I I J J J J K K K K K'. How can I change my code so that either 'key' or 'vari' changes for each letter in the cycle yet stays the same so that no letter corresponds to more than one other letter?
import random
intext = raw_input("Enter a message to encrypt: ")
intext = intext.upper()
key = random.randint(0,27)
vari = random.randint(0,27)
def randomsub(intext):
outtext = []
intext = intext.upper()
for c in intext:
num = ord(c)
if num >= ord('A') and num <= ord('Z'):
num = num - ord('A')
num = num + (key + vari) % 26
num += ord('A')
outtext.append(chr(num))
return ' '.join(outtext)
print randomsub(intext)
Aucun commentaire:
Enregistrer un commentaire