基于Golang documentation on CFB decryption,我编写了一个最小的工作示例来解密一个用AES-CFB加密,然后用python3编码的base64的字符串。在
当消息在golang中加密时,golang解密工作正常(使用golang doc示例中的加密函数)。 但是,当我使用python crypto包在python脚本中加密消息时,我无法在golang脚本中成功地解密它。我无法获取正确的字节。在
$ python3 stack.py
Going to encrypt and base64 "This is not encrypted" result:
b'jf9A5LCxKWPuNb1XiH+G3APAgR//'
Now going to call the Golang script:
b'Hello from Golang, going to decrypt: jf9A5LCxKWPuNb1XiH+G3APAgR//
Result: Tl!\xca/\xf1\xc0\xb2\xd01Y\x02V\xec\xdf\xecy\xd38&\xd9\n'
两种AES实现的块大小默认为16。在
所以问题是:出了什么问题?在
果郎抄本:
^{pr2}$Python脚本:
#!/usr/bin/env python3
import base64
from Crypto.Cipher import AES
from subprocess import check_output
original_message = 'This is not encrypted'
key = 'TfvY7I358yospfWKcoviZizOShpm5hyH'
iv = 'mb13KcoviZizvYhp'
#prepare encryption
cfb_cipher_encrypt = AES.new(key, AES.MODE_CFB, iv)
#encrypt and base64 encode
encryptedpayload = base64.b64encode(cfb_cipher_encrypt.encrypt(original_message))
print('Going to encrypt and base64 "{}" result:\n{}\n'.format(original_message,encryptedpayload))
print('Now going to call the Golang script:')
print(check_output('go run stack.go {}'.format(encryptedpayload.decode()),shell=True))