从ord()解码
我写了一个程序,用户可以输入自己的密码,然后这个密码会被移动几个 ord()
字符。
encrypt=str(ord(letter)*x+x*7) #where x is a random number picked from an array
接着,密码会被保存到一个文件里:
passwrd=input("\nokay then " + name + " now enter your password which we will encrypt \nnew password:")
x=int(random.choice(randomNumber)) #The randomNumber array
myfile.write(str(x) + "\n")
pasw_file=open('secerateStuff.txt', 'w')
for letter in passwrd: #passwrd is the user input
encrypt=str(ord(letter)*x+x*7)
pasw_file.write(encrypt + "\n")
pasw_file.close()
比如,mypassword
可能被编码成:
6 # In myfile
696 # In pasw_file
768
714
624
732
732
756
708
726
642
我想问的是,怎么才能把密码从 ord()
方法转换回原来的字符呢?(是不是和 chr()
有关?)
谢谢大家的回复!
1 个回答
4
因为你把盐值和代码写在一起,所以你可以使用下面的方式。
>>> passw = """6
696
768
714
624
732
732
756
708
726
642"""
>>> passw = map(int, passw.split())
>>> salt, passw = passw[0], passw[1:]
>>> salt
6
>>> passw
[696, 768, 714, 624, 732, 732, 756, 708, 726, 642]
>>> "".join([chr(elem/salt - 7) for elem in passw])
'mypassword'
在Python 3中,你可以这样做,我觉得看起来好多了。(感谢 J.F. Sebastian)
>>> salt, *passw = map(int, passw.split())