文件开始为: the whispers in the morning of lovers sleeping tight are rolling by like thunder now as i look in your eyes i hold on to your body and feel each move you make your voice is warm and tender a love that i could not forsake cause i am your lady and you are my man whenever you reach for me i will do all that i can
In [4]:
fh=open(r'd:\temp\秘密花园.txt')
text = fh.read()
words = text.split(' ')
fh.close()
编辑为密码:
In [5]:
import random
fh=open(r'd:\temp\秘密花园.txt','w')
for word in words:
length=len(word)
idiom=word
number=random.randint(100000000,999999999)
key=(length-1)*10**9+number
key1=str(key)
i=0
wordkey=[]
if key/1000000000>=1:
wordkey.append(chr(97+int(key1[0])))
for i in range(1,length+1):
if ord(idiom[i-1])+int(key1[i])<123:
wordkey.append(chr(ord(idiom[i-1])+int(key1[i])))
else:
wordkey.append(chr(ord(idiom[i-1])+int(key1[i])-57))
for i in range(length,9):
wordkey.append(chr(97+int(key1[i])))
else:
wordkey.append('a')
for i in range(0,length+1):
if ord(idiom[i-1])+int(key1[i-1])<123:
wordkey.append(chr(ord(idiom[i-1])+int(key1[i-1])))
else:
wordkey.append(chr(ord(idiom[i-1])+int(key1[i-1])-57))
for i in range(length,8):
wordkey.append(chr(97+int(key1[i])))
wordkey1=''.join(wordkey)
wordkey2=wordkey1+' '
fh.write(wordkey2)
fh.close()
文件最终显示为: cDmfbdajbj hEmjCxjrai bogeejbbeh cDmfbbbggj gtqBrpqdfh bpbhjgghcb fosxitcfgi hylmnwlsff evkmibgbab civeabibij gvoloivicd bkjbeigfjf dnnoeaidfi gwmurigcga cttffbhaaj bffgfibjfd afihibjije dspvhbfcai bpheffiagh dFxDidiiij diymifgebc aidhjaihai dqsncjceaf bpbigafaii bBhhgiceab dzquahhajh deogdhgcbj cguhdfdggc dkghdeiied dhfdbhegdg dpuvafifjc cGxjafhhbe dverhfdhjd dGxwchjbae eDujgehaab bkcdeeghfa dzhwfcecff cbreggeghe fxmsdnjjai agjcbgfagg droEicdajj dvjbbfhfif aeeccfbedi ekxCmbejhf cqrdhjbejb gjurvimcjb eefExfafje adfeaecjee biigjgjaie dCtzffhcfg dncdahiafb chtgjbhfhe cIvhcjbdgb cdxgcefgih buijffhagd coddaacijb hxpepkFlhg cCpbafbbjb esjhljagjd cordedjaja bvjffeiddd afbgcjcdbj dFpshabefc bmjdjadfge ciujeiaffg dymjjagbci abafghefce cfggecgigd
解密:
In [13]:
fh=open(r'd:\temp\秘密花园.txt','r+')
text = fh.read()
wordkey2 = text.split(' ')
true_word=[]
for wordkey1 in wordkey2:
if wordkey1[0]=='a':
if ord(wordkey1[1])<97:
true_word=chr(ord(wordkey1[1])+57-int(key1[1]))
else:
true_word=chr(ord(wordkey1[1])-int(key1[1]))
else:
headword=ord(wordkey1[0])-97
for i in range(1,headword):
if ord(wordkey1[i-1])<97:
true_word.append=chr(ord(wordkey1[i])+57-int(key1[i]))
elif ord(wordkey1[i-1])>97:
true_word.append=chr(ord(wordkey1[i])-int(key1[i]))
true_word1=''.join(true_word)
true_word2=true_word1+' '
fh.write(true_word2)
fh.close()