Encrypt & Decrypt using PyCrypto AES 256

后端 未结 12 884
夕颜
夕颜 2020-11-22 13:07

I\'m trying to build two functions using PyCrypto that accept two parameters: the message and the key, and then encrypt/decrypt the message.

I found several links on

12条回答
  •  囚心锁ツ
    2020-11-22 13:26

    For the benefit of others, here is my decryption implementation which I got to by combining the answers of @Cyril and @Marcus. This assumes that this coming in via HTTP Request with the encryptedText quoted and base64 encoded.

    import base64
    import urllib2
    from Crypto.Cipher import AES
    
    
    def decrypt(quotedEncodedEncrypted):
        key = 'SecretKey'
    
        encodedEncrypted = urllib2.unquote(quotedEncodedEncrypted)
    
        cipher = AES.new(key)
        decrypted = cipher.decrypt(base64.b64decode(encodedEncrypted))[:16]
    
        for i in range(1, len(base64.b64decode(encodedEncrypted))/16):
            cipher = AES.new(key, AES.MODE_CBC, base64.b64decode(encodedEncrypted)[(i-1)*16:i*16])
            decrypted += cipher.decrypt(base64.b64decode(encodedEncrypted)[i*16:])[:16]
    
        return decrypted.strip()
    

提交回复
热议问题