encrypt method

Future<String> encrypt(
  1. Object data,
  2. List<int> secret
)

Implementation

Future<String> encrypt(Object data, List<int> secret) async{
  final plainText = json.encode(data);

  List<int> plaintext  = utf8.encode(plainText);
  List<int> ivx = cry.AesGcm.with256bits().newNonce();
  cry.SecretKey secretKey = new cry.SecretKey(secret);


  cry.SecretBox secretBox = await cry.AesGcm.with256bits().encrypt(plaintext, nonce: ivx, secretKey: secretKey);
  String ivCiphertextMacB64 = base64.encode(secretBox.concatenation()); // Base64 encoding of: IV | ciphertext | MAC

  // print("secretbox => " + base64.encode(secretBox.concatenation(mac: false)));
  // print("ivCiphertextMacB64 : " + ivCiphertextMacB64);
  // print("iv length => " + secretBox.nonce.length.toString());
  // print("ivCiphertextMacB64 : " + ivCiphertextMacB64);

  var combine = ivx + secretBox.cipherText + secretBox.mac.bytes;


  // Uint8List ivCiphertextMac = base64.decode(ivCiphertextMacB64); // from the Java code
  // Uint8List ivy = ivCiphertextMac.sublist(0, 12);
  // Uint8List ciphertext  = ivCiphertextMac.sublist(12, ivCiphertextMac.length - 16);
  // Uint8List mac = ivCiphertextMac.sublist(ivCiphertextMac.length - 16);
  //
  //
  // cry.SecretBox secretBoxx = new cry.SecretBox(ciphertext, nonce: ivy, mac: new cry.Mac(mac));
  //
  // List<int> decrypteds = await cry.AesGcm.with256bits().decrypt(secretBoxx, secretKey: secretKey);
  // String dec = utf8.decode(decrypteds);
  // print("Decrypted text : " + dec);


  return base64.encode(combine);
}