【问题标题】:Python Encryption using Keyphrase使用关键字短语的 Python 加密
【发布时间】: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

有谁知道这个问题的答案还是我只是愚蠢?

【问题讨论】:

标签: python python-2.7 encryption python-2.x vigenere


【解决方案1】:

您应该删除% 58 + 48。 我假设您正在使用它来打印可打印的 ascii 字符,但您不应该这样做。您正在丢失信息。

如果要显示“不可打印的 ascii chard”,请使用 base64。

【讨论】:

  • 如果我使用它,我会得到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:test ���� Process finished with exit code 0
  • 对加密文本使用base64,如果需要解密则使用debase64
  • 这是一个经典密码。当使用完整的字节空间时,它并没有真正提高它的安全性,但它有助于发现错误,当只使用可打印字符时。
  • 我不允许使用任何其他密码,因为这是考试课程!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多