实现Dart语言python语言js语言三端aes cbc zeropadding填充对等

有疑问可以进QQ群找我或者我们的小伙伴咨询解决方案:点我点我进入

网友qq群里咨询关于dart语言实现aes cbc zeropadding加密和js加密不一样,

发现确实加密结果不一样。

下面是解决方案:

主要原因就是js填充的是\x00

所以要想保持加解密一致就需要去处理python的和dart的填充

1)首先是js语言aes cbc zeropadding填充模式

大家可以去这个网站测试,代码我就不写了

http://tool.chacuo.net/cryptaes

2)python语言

#coding:utf-8
from Crypto.Cipher import AES
def zero_pad(data, block_size):
    padding = block_size - (len(data) % block_size)
    return data + b'\x00' * padding

def zero_unpad(data):
    return data.rstrip(b'\x00')
key = 'xxxxxxxxxxxxxxxx'.encode()
iv = 'xxxxxxxxxxxxxxxx'.encode()
data ='123456'
#加密
ci = AES.new(key=key ,mode=AES.MODE_CBC,iv=iv)
d = ci.encrypt(zero_pad(data.encode(),16))
print("加密结果:",d.hex())
#解密
ci = AES.new(key=key ,mode=AES.MODE_CBC,iv=iv)
b = zero_unpad(ci.decrypt(d))
print('解密结果:',b.decode())

3)Dart语言  _KEY和_OFFSET是xxxxxxxxxxxxxxxx的hex值

import 'dart:convert';
import 'dart:typed_data';
import 'package:encrypt/encrypt.dart';


class a {

  static String _KEY = "78787878787878787878787878787878";
  static String _OFFSET = "78787878787878787878787878787878";
  static Uint8List  aesEncode(List byteList) {
    final key = Key.fromBase16(_KEY);

    final iv = IV.fromBase16(_OFFSET);
    final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: null));
    Encrypted encrypted = encrypter.encryptBytes(byteList,iv:iv);
    return Uint8List.fromList(encrypted.bytes);
  }
  static Uint8List zeroPad(Uint8List data, int blockSize) {
    int padding = blockSize - (data.length % blockSize);
    Uint8List paddedData = Uint8List(data.length + padding);
    paddedData.setRange(0, data.length, data);
    paddedData.fillRange(data.length, paddedData.length, 0x00); // Fill with null bytes
    return paddedData;
  }

  static Uint8List zeroUnpad(Uint8List data) {
    int lastNonZeroIndex = data.lastIndexWhere((byte) => byte != 0x00);
    if (lastNonZeroIndex == -1) {
      return Uint8List.fromList([]); // No zero-padding found, return an empty list.
    } else {
      return Uint8List.sublistView(data, 0, lastNonZeroIndex + 1);
    }
  }


  /// 解密函数
  static Uint8List  aesDecrypted(Uint8List byteList){
    final key = Key.fromBase16(_KEY);
    final iv = IV.fromBase16(_OFFSET);
    final encrypter = Encrypter(AES(key ,mode:  AESMode.cbc, padding: null));
    var decrypted = encrypter.decryptBytes(Encrypted(byteList), iv: iv);
    return Uint8List.fromList(decrypted);
  }
  static Uint8List createUint8ListFromHexString(String hex) {
    if (hex == null) throw new ArgumentError("hex is null");

    var result = new Uint8List(hex.length ~/ 2);
    for (var i = 0; i < hex.length; i += 2) {
      var num = hex.substring(i, i + 2);
      var byte = int.parse(num, radix: 16);
      result[i ~/ 2] = byte;
    }

    return result;
  }
  static String formatBytesAsHexString(Uint8List bytes) {
    if (bytes == null) throw new ArgumentError("The list is null");

    var result = new StringBuffer();
    for (var i = 0; i < bytes.lengthInBytes; i++) {
      var part = bytes[i];
      result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}');
    }
    return result.toString();
  }
}




void main() {

  var s = '123456';
  Uint8List byteList = Uint8List.fromList(s.codeUnits);
  print("Uint8List(初始数组):${byteList}");
  Uint8List c= a.aesEncode(a.zeroPad(byteList,16));
  print("Uint8List(加密结果):${c}");
  print("hex(加密结果):${a.formatBytesAsHexString(c)}");
  print("string(加密结果):${utf8.decode(c,allowMalformed: true)}");
  c = a.zeroUnpad(a.aesDecrypted(c));
  print("Uint8List(解密结果):${c}");
  print("hex(解密结果):${a.formatBytesAsHexString(c)}");
  print("string(解密结果):${utf8.decode(c,allowMalformed: true)}");
}

你可能感兴趣的:(python,javascript,开发语言,dart,flutter,aes)