问题
I'm trying to create a cipher with a scrambled alphabet contanting all possible characters, including unicodes, chinease/japanease symbols and etc...
I found out that I can print up to 65536 characters. I'm building the regular alphabet with a dict and a list of the numbers of those characters.
alphabet = { }
numeral = []
for n in xrange(65536):
alphabet[unichr(n)] = n
numeral.append(n)
As for the cipher:
cipher_alphabet = { }
for char in alphabet:
cipher_alphabet[char] = choice(numeral)
numeral.remove(cipher_alphabet[char])
To make a key/password I'm using the random.seed(key).
The problem is, when I try to use the dict to compare inputs from a file which contains unicode characters it give me:
KeyError: '\xe0'
�
In the file this character is 'à'.
The crypting part is like this:
message = open(file+'.txt').read()
crypted_message = ""
for word in message:
for char in word:
letter = cipher_alphabet.keys()[cipher_alphabet.values().index(alphabet[char])]
crypted_message += letter
I've manage to use commom printable characters using:
for n in xrange(32, 127):
alphabet[chr(n)] = n
But if I change the chr() to unichr() it gives me those errors.
Any hint?
Also, I've read that seed() is not a good method for cryptography, any hint for that too?
EDIT:
Thanks to @Joran manage to make it work.
For those interesed ...I've changed a bit the code.
for the alphabet:
for n in xrange(0, 65536):
alphabet[n] = unichr(n)
numeral.append(n)
for the cipher:
for x in alphabet:
num = choice(numeral)
crypted_alphabet[num] = alphabet[x]
numeral.remove(num)
the crypting part:
message = open(file+'.txt','rb').read()
for n in message:
num = alphabet.values().index(crypted_alphabet[n])
crypted_message.append(num)
来源:https://stackoverflow.com/questions/20534750/cipher-with-all-unicode-characters