2 回答
TA贡献1833条经验 获得超4个赞
你可以试试这个:
LETTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()`~-=_+[]{}|;\':",./<>? '
def create_cypher_dictionary():
numbers = [ '%02d' % i for i in range(100) ]
random.shuffle( numbers )
return { a : b for a,b in zip( LETTERS, numbers ) }
def encrypt( cypher, string ) :
return ''.join( cypher[ch] for ch in string )
def decrypt( cypher, string ) :
inverse_cypher = { b : a for a,b in cypher.items() }
return ''.join( inverse_cypher[a+b] for a,b in zip(*[iter(string)]*2) )
检查:
>>> cypher = create_cypher_dictionary()
>>> encoded = encrypt( cypher, 'The quick brown fox jumps over the lazy dog' )
>>> encoded
'93684236886025540636378012826636001276363960074903361250428036306842367064856536261211'
>>> decrypt( cypher, encoded )
'The quick brown fox jumps over the lazy dog'
>>>
是的,你不能每次都创建密码,你必须做一个,然后重用它,否则你的结果会有点随机=)
TA贡献1821条经验 获得超6个赞
您一次只采用一位数字来错误地转换 ASCII,而此时您应采用两位数字。只需在解密函数中更改 for 循环的步骤:
def decrypt(s):
cypher=create_cypher_dictionary()
new_s=''
for i in range(0,len(s)-1,2): # Make the for loop step 2 instead of 1 (default)
c=s[i]+s[i+1]
for cc in cypher:
if cypher[cc]==c:
new_s=new_s+cc
return new_s
添加回答
举报