2 回答

TA貢獻1833條經(jīng)驗 獲得超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'
>>>
是的,你不能每次都創(chuàng)建密碼,你必須做一個,然后重用它,否則你的結(jié)果會有點隨機=)

TA貢獻1821條經(jīng)驗 獲得超6個贊
您一次只采用一位數(shù)字來錯誤地轉(zhuǎn)換 ASCII,而此時您應(yīng)采用兩位數(shù)字。只需在解密函數(shù)中更改 for 循環(huán)的步驟:
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
添加回答
舉報