我想要一些python和flutter之间加密的例子,以加密客户机和服务器之间的请求和响应体。
我找到了一些用于AES CRT加密的示例代码,但是我看不到在颤振和python中有同样的结果。
有人能帮我吗?
更新:颤振密码包没有计数器参数,但是python Crypto.Cipher包有计数器参数。
这是python的示例代码:
plaintext = '123'.encode('utf-8')
key = '12345678911234567891123456789123'.encode("utf-8")
iv = '12345'.encode('utf')
iv_int = int(binascii.hexlify(iv), 16)
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
aes = AES.new(key, AES.MODE_CTR, counter=ctr)
ciphertext = aes.encrypt(plaintext)
print('ctr = ' + str(ctr))
print('iv = ' + str(base64.b64encode(iv)))
print('iv_int = ' + str(iv_int))
print('plaintext = ' + str(plaintext))
print('key = ' + str(base64.b64encode(key)))
print('ciphertext = ' + str(base64.b64encode(ciphertext)))这是颤振的示例代码:
final plainText = '123';
final key = encrypt.Key.fromUtf8('12345678911234567891123456789123');
final iv = encrypt.IV.fromUtf8('12345');
final encrypter = encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.ctr));
final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
print('key = ' + key.base64);
print('iv =' + iv.base64);
print('encrypted = ' + encrypted.base64);发布于 2022-09-01 09:35:41
我有同样的问题,并发布了一个非常类似的问题。幸运的是,我自己发现了错误。
下面是链接:Python AES CTR Mode only encrypts first two bytes
总之,问题是:在使用AES加密时,Dart自动使用PKCS7填充,但是Python不会这样做。
因此,要么将Dart代码中的填充设置为null,例如:
aes = AES.new(key, AES.MODE_CTR, counter=ctr, padding: null)或者将PKCS7填充添加到您的python代码中,如下所示:
# The message has to in bytes format
def pkcs7padding(message):
pl = 16 - (len(message) % 16)
return message + bytearray([pl for i in range(pl)])
ciphertext = aes.encrypt(pkcs7padding(plaintext))https://stackoverflow.com/questions/62580708
复制相似问题