encryptWithAgreement static method

String encryptWithAgreement(
  1. String message,
  2. ECDHBasicAgreement agreement,
  3. String publicKey
)

Implementation

static String encryptWithAgreement(
    String message, ECDHBasicAgreement agreement, String publicKey) {
  var pubKey = getPubKey(publicKey);
  var agreementD0 = agreement.calculateAgreement(pubKey);
  var encryptKey = agreementD0.toRadixString(16).padLeft(64, "0");

  final random = Random.secure();
  var ivData =
      Uint8List.fromList(List<int>.generate(16, (i) => random.nextInt(256)));
  // var iv = "UeAMaJl5Hj6IZcot7zLfmQ==";
  // var ivData = base64.decode(iv);

  final cipherCbc =
      PaddedBlockCipherImpl(PKCS7Padding(), CBCBlockCipher(AESEngine()));
  final paramsCbc = PaddedBlockCipherParameters(
      ParametersWithIV(
          KeyParameter(Uint8List.fromList(hex.decode(encryptKey))), ivData),
      null);
  cipherCbc.init(true, paramsCbc);

  // print(cipherCbc.algorithmName);

  var result = cipherCbc.process(Uint8List.fromList(utf8.encode(message)));

  return "${base64.encode(result)}?iv=${base64.encode(ivData)}";
}