encrypt icon indicating copy to clipboard operation
encrypt copied to clipboard

Create and save random IVs

Open fiorcode opened this issue 4 years ago • 1 comments

Hi, i need to create a random IV and save it as String for using it later. Can someone put an example of how to do that?

fiorcode avatar Jan 25 '21 20:01 fiorcode

Hi! I'm new so I may not be fully correct or atleast suggesting the most secure solution, but I saved the IV object by copying it's base64 string and then invoking the same method with the same string.

`Future encr(String name) async { final encrKey = Key.fromBase64("AWnGXC/ba+1JSZPBvvkdUg==");

final encrypter = Encrypter(AES(encrKey, padding: null));
final IV iv = IV.fromBase64("srllQCNLB6/32leMbZMUUw==");
final baseString = iv.base64;
print(baseString);
final IV iv2 = IV.fromBase64(baseString);
final encryptedText = encrypter.encrypt(name, iv: iv);
final decText = encrypter.decrypt(encryptedText, iv: iv2);

print(decText);

}`

works and prints the same string each time. If i have to make it a bit stronger I would use secure random for the base64 strings and then pass the string somewhere, maybe to a different database or just send it with the encrypted text after encrypting it again with a private key . You get the idea, would love constructive feedback on the approach 🙌

cryoelite avatar Mar 03 '21 16:03 cryoelite