I wanted to create a encryption logic in flutter with dependency encrypt 5.0.1 using method AESMode.cbc.

import 'package:encrypt/encrypt.dart';

final plainText = 'Hello';
final encrypter = Encrypter(AES(test as Key, mode: AESMode.cbc));
final iv_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
Uint8List ivbyte = Uint8List.fromList(iv_list);
final iv = IV(ivbyte);
final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
print(encrypted.base64);
print(decrypted.base64);

final encrypter = Encrypter(AES(key, mode: AESMode.cbc)); In Encrypter we need to pass the key. To create the key I have to use the PBKD2 class.

import 'package:cryptography/cryptography.dart';

Future<void> main() async {
   final pbkdf2 = Pbkdf2(
    macAlgorithm: Hmac.sha512(),
    iterations: 65536, // 20k iterations
    bits: 256, // 256 bits = 32 bytes output
  );
  final newSecretKey = await pbkdf2.deriveKeyFromPassword(
    // Password given by the user.
    password: 'password',
    // Nonce (also known as "salt") should be some random sequence of
    // bytes.
    //
    // You should have a different nonce for each user in the system
    // (which you store in the database along with the hash).
    // If you can't do that for some reason, choose a random value not
    // used by other applications.
    nonce: const [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
  );
  final secretKeyBytes = await newSecretKey.extractBytes();
  print('Result: $secretKeyBytes');
}

Now I wanted to pass the key generated by pbkdf2 into Encrypter(AES(key)). Here I am facing the issue casue the output of pbkdf2 is in type: secretKey but the input required for Encrypter(AES(key)); is in type:key.

I am not able to convert the secretKey into key to pass in Encrypter. And this is the main requirment need to create hash key using pbkdf2 and using it in Encrypter.

Need to pass the Hash key created by pbkdf2 into the Encrypter(AES(key)) function. I have tried converting the secret key created by pbkdf2 into key by using the available function, but seems like it was not working. Need help to find the way to convert the secrete key into key or another way to implement the specified requirement.

0

There are 0 best solutions below