我正在尝试创建一个包含所有可能字符的加扰字母的密码,包括 unicodes、chinease/japanease 符号等......
我发现我最多可以打印 65536 个字符。我正在使用字典和这些字符的数字列表构建常规字母表。
alphabet = { }
numeral = []
for n in xrange(65536):
alphabet[unichr(n)] = n
numeral.append(n)
至于密码:
cipher_alphabet = { }
for char in alphabet:
cipher_alphabet[char] = choice(numeral)
numeral.remove(cipher_alphabet[char])
要制作密钥/密码,我使用的是 random.seed(key)。
问题是,当我尝试使用 dict 比较包含 unicode 字符的文件的输入时,它给了我:
KeyError: '\xe0'
�
在文件中这个字符是'à'。
加密部分是这样的:
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
我已经设法使用commom可打印字符:
for n in xrange(32, 127):
alphabet[chr(n)] = n
但是,如果我将 chr() 更改为 unichr() 它会给我这些错误。
有什么提示吗?
另外,我读过 seed() 不是密码学的好方法,还有什么提示吗?
编辑:
感谢@Joran 设法使它工作。
对于那些感兴趣的人......我已经更改了一些代码。
对于字母表:
for n in xrange(0, 65536):
alphabet[n] = unichr(n)
numeral.append(n)
对于密码:
for x in alphabet:
num = choice(numeral)
crypted_alphabet[num] = alphabet[x]
numeral.remove(num)
加密部分:
message = open(file+'.txt','rb').read()
for n in message:
num = alphabet.values().index(crypted_alphabet[n])
crypted_message.append(num)