leocavalcante / encrypt

🔒 A set of high-level APIs over PointyCastle for two-way cryptography.
BSD 3-Clause "New" or "Revised" License
348 stars 141 forks source link

Invalid argument(s): Invalid or corrupted pad block #272

Closed johnzhu12 closed 2 years ago

johnzhu12 commented 2 years ago

test.dart

_endeTest() {
  Map keyPair1 = crypto.generateKeyPair();
  Map keyPair2 = crypto.generateKeyPair();
  // alice的私钥和bob的公钥加密
  var enc = crypto.enByPubkey(keyPair2['base64Pub'], keyPair1['base64Priv'], 'hello');
  print('密文:$enc');
  // bob拿自己的私钥和alice的公钥解密
  var decode = crypto.deByPrivKey(keyPair2['base64Priv'], keyPair1['base64Pub'], enc);
  print('明文:$decode');
}

aescbc.dart

aesEncrypt(cipher, txt) {
  final key = Key(cipher);
  // final iv = IV(seed(16));
 // I set this to fixed to reduce complex, will change to above line code when in prod
  final iv = IV.fromBase64('Qj7t3X7hmoMU+UuS8vccjQ==');
  final encrypter = Encrypter(AES(key));

  final encrypted = encrypter.encrypt(txt, iv: iv);
  Uint8List ctbuf = encrypted.bytes;
  print('keybuf:$cipher');
  print('ctbuf:$ctbuf');

  Uint8List ivBuf = iv.bytes;
  print('ivBuf的base64编码:${base64Encode(ivBuf)}');
  print('ivBuf:$ivBuf');
  String bytesAll = base64Encode(NanoHelpers.concat([ivBuf, ctbuf]));
  // print('dart端加密出的密文:$bytesAll');
  return bytesAll;
}
aesDecrypt(cipher, enc) {
  final key = Key(cipher);
  Uint8List bufAll = base64Decode(enc);
  final iv = IV(bufAll.sublist(0, 16));
  final encrypter = Encrypter(AES(key));
  final decrypted = encrypter.decrypt(Encrypted(bufAll.sublist(16)), iv: iv);
  // print(decrypted);
  return decrypted;
}
newcypher: [175, 13, 200, 154, 89, 82, 10, 24, 176, 248, 205, 87, 247, 164, 203, 115, 108, 227, 151, 180, 10, 170, 125, 90, 30, 155, 59, 63, 121, 28, 192, 115]
keybuf:[175, 13, 200, 154, 89, 82, 10, 24, 176, 248, 205, 87, 247, 164, 203, 115, 108, 227, 151, 180, 10, 170, 125, 90, 30, 155, 59, 63, 121, 28, 192, 115]
ctbuf:[79, 186, 217, 81, 170, 77, 142, 142, 74, 58, 78, 145, 48, 138, 112, 63]
ivBuf的base64编码:Qj7t3X7hmoMU+UuS8vccjQ==
ivBuf:[66, 62, 237, 221, 126, 225, 154, 131, 20, 249, 75, 146, 242, 247, 28, 141]
密文:Qj7t3X7hmoMU+UuS8vccjU+62VGqTY6OSjpOkTCKcD8=
newcypher: [175, 13, 200, 154, 89, 82, 10, 24, 176, 248, 205, 87, 247, 164, 203, 115, 108, 227, 151, 180, 10, 170, 125, 90, 30, 155, 59, 63, 121, 28, 192, 115]
明文:hello

if I change final encrypter = Encrypter(AES(key)) to final encrypter = Encrypter(AES(key, mode: AESMode.cbc));

the title error will occur

Invalid argument(s): Invalid or corrupted pad block
#0      PKCS7Padding.padCount (package:pointycastle/paddings/pkcs7.dart:42:7)
#1      PaddedBlockCipherImpl.doFinal (package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart:112:30)
#2      PaddedBlockCipherImpl.process (package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart:74:25)
#3      AES.decrypt (package:encrypt/src/algorithms/aes.dart:63:22)
#4      Encrypter.decryptBytes (package:encrypt/src/encrypter.dart:25:17)
#5      Encrypter.decrypt (package:encrypt/src/encrypter.dart:31:17)
#6      aesDecrypt (file:///Users/johnzhu/Desktop/gitlabRepos/%E8%82%89%E7%89%9B/chicago-app/lib/crypto/aescbc.dart:33:31)
#7      Crypto.deByPrivKey (file:///Users/johnzhu/Desktop/gitlabRepos/%E8%82%89%E7%89%9B/chicago-app/lib/crypto/crypto.dart:154:18)
#8      _endeTest (file:///Users/johnzhu/Desktop/gitlabRepos/%E8%82%89%E7%89%9B/chicago-app/test/crypto_test.dart:29:23)
#9      main (file:///Users/johnzhu/Desktop/gitlabRepos/%E8%82%89%E7%89%9B/chicago-app/test/crypto_test.dart:17:3)
#10     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

image

johnzhu12 commented 2 years ago

all right,I found I mistaken the param here

aesDecrypt(cipher, enc) {
  // also should change to AESMode.cbc mode
  final encrypter = Encrypter(AES(key));
  }

(⊙︿⊙)