首页 > 解决方案 > Flutter 和 python [AES] 之间的相同加密

问题描述

我想要一些python和flutter之间加密的例子来加密客户端和服务器之间的请求和响应主体

我找到了一些用于 AES CRT 加密的示例代码,但我在颤振和 python 中看不到相同的结果

有谁能够帮我 ?

更新:Flutter crypt 包没有计数器参数,但 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);

标签: pythonflutterdartencryptionaes

解决方案


推荐阅读