【发布时间】:2015-05-05 12:12:25
【问题描述】:
我正在尝试制作一个 python 程序,它将接收纯文本并使用密钥对其进行加密。它是用 python 2.7.4 编写的
这是我目前的代码
def encrypter(intext, shift, modder):
plain = list(intext)
out = ''
j = 0
key = list(shift)
for i in plain:
if mod > 0:
x = chr((ord(i) + ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
if mod < 0:
x = chr(((ord(i) - ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48))
out += x
j += 1
return out
sel = raw_input("Encrypt (e)/ Decrypt (d)")
if sel == 'e':
mod = 1
elif sel == 'd':
mod = -1
else:
mod = 0
print('Enter a proper value!')
if mod != 0:
print(encrypter(raw_input('Enter the text:'), raw_input('Enter the key-phrase:'), mod)
当我加密某些东西时,我得到了这个:
C:\Users\aesha_000\py\Scripts\python.exe "D:/Programming/Computing GCSE/Resources/functiontest3.py"
Encrypt (e)/ Decrypt (d)e
Enter the text:alex
Enter the key-phrase:pass
]Yd:
Process finished with exit code 0
但问题是当我再次解密时,我得到了错误的输出:
C:\Users\aesha_000\py\Scripts\python.exe "D:/Programming/Computing GCSE/Resources/functiontest3.py"
Encrypt (e)/ Decrypt (d)d
Enter the text:]Yd:
Enter the key-phrase:pass
a2e>
Process finished with exit code 0
有谁知道这个问题的答案还是我只是愚蠢?
【问题讨论】:
-
只需将 +48 放在 mod
-
Python File Encryption 的可能重复项
标签: python python-2.7 encryption python-2.x vigenere