【问题标题】:Reversing encryption algorithm in PythonPython中的逆向加密算法
【发布时间】:2014-04-16 06:50:13
【问题描述】:

大家好,我在使用这个脚本时遇到了一些问题。该脚本应该基本上提示输入,然后加密用户输入的任何内容,然后用户可以返回并通过键入加密消息来解密消息。脚本的加密部分工作正常,只是解密部分给我带来了问题。这是代码,当然提前感谢:D

def encrypt(key, msg):
    encryped = []
    for i, c in enumerate(msg):
        key_c = ord(key[i % len(key)])
        msg_c = ord(c)
        encryped.append(chr((msg_c + key_c) % 127))
    return ''.join(encryped)

def decrypt(key, encryped):
    msg2 = []
    for i, c in enumerate(encryped):
        key_c = ord(key[i % len(key)])
        enc_c = ord(c)
        msg.append(chr((enc_c - key_c) % 127))
    return ''.join(msg)

welcome = str(input("Press 1 to encrypt or anything else to decrypt: "))
if welcome in ['1', '1']:
    var = input("Please type in what you want to encrypt: ")
    if __name__ == '__main__':
        key = 'This is the key'
        msg = var
        encrypted = encrypt(key, msg)
        decrypted = decrypt(key, encrypted)
    print ('Please take down the key and encryption message to decrypt this message at a later stage')
    print ('This is what you want to encrypt:' , repr(msg))
    print ('This is the encryption/decryption key:', repr(key))
    print ('This is the encrypted message:', repr(encrypted))

else:
    var2 = input("Please enter your encrypted message that you would like to decrypt: ")
    if __name__ == '__main__':
        key = 'This is the key'
        msg = var2
        decrypted = decrypt(key, var2)
    print ('This is the decrypted message:', repr(decrypted))

【问题讨论】:

  • 你能举一个加密消息的例子吗?
  • 这是“Hello World”加密后的样子:“\x1dNVO\nkOfD”

标签: python encryption cryptography


【解决方案1】:

稍微简化的版本:

def encrypt(key, msg):
    encrypted = []
    for i, c in enumerate(msg):
        key_c = ord(key[i % len(key)])
        msg_c = ord(c)
        encrypted.append(chr((msg_c + key_c) % 127))
    return ''.join(encrypted)

def decrypt(key, encryped):
    msg = []
    for i, c in enumerate(encryped):
        key_c = ord(key[i % len(key)])
        enc_c = ord(c)
        msg.append(chr((enc_c - key_c) % 127))
    return ''.join(msg)

if __name__ == '__main__':
    welcome = input('Press 1 to encrypt or anything else to decrypt: ')
    key = 'This is the key'
    if welcome == '1':
        msg = input('Please type in what you want to encrypt: ')
        encrypted = encrypt(key, msg)
        print('Please take down the key and encryption message to decrypt this message at a later stage')
        print('This is what you want to encrypt:', repr(msg))
        print('This is the encryption/decryption key:', repr(key))
        print('This is the encrypted message:', repr(encrypted))
    else:
        msg = input('Please enter your encrypted message that you would like to decrypt: ')
        msg = msg.encode('utf-8').decode('unicode_escape')
        decrypted = decrypt(key, msg)
        print('This is the decrypted message:', repr(decrypted))

因为您的加密文本包含前 32 个 ASCII 字符作为转义的十六进制文字,所以在解密输入时它们不会被解释为这样。要处理转义的文字,可以使用msg.encode("utf-8").decode("unicode_escape")(建议here)。 您不妨使用ascii 进行编码,因为您的加密文本仅包含最多 127 个 ASCII 字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2011-10-18
    • 2019-04-06
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多