Line data Source code
1 : import 'dart:typed_data';
2 :
3 : import 'package:crypton/crypton.dart';
4 : import 'package:encrypt/encrypt.dart';
5 :
6 : class EncryptionUtil {
7 2 : static String generateAESKey() {
8 4 : var aesKey = AES(Key.fromSecureRandom(32));
9 4 : var keyString = aesKey.key.base64;
10 : return keyString;
11 : }
12 :
13 1 : static String encryptValue(String value, String encryptionKey) {
14 3 : var aesEncrypter = Encrypter(AES(Key.fromBase64(encryptionKey)));
15 1 : var initializationVector = IV.fromLength(16);
16 1 : var encryptedValue = aesEncrypter.encrypt(value, iv: initializationVector);
17 1 : return encryptedValue.base64;
18 : }
19 :
20 1 : static String decryptValue(String encryptedValue, String decryptionKey) {
21 2 : var aesKey = AES(Key.fromBase64(decryptionKey));
22 1 : var decrypter = Encrypter(aesKey);
23 1 : var iv2 = IV.fromLength(16);
24 1 : return decrypter.decrypt64(encryptedValue, iv: iv2);
25 : }
26 :
27 1 : static String encryptKey(String aesKey, String publicKey) {
28 1 : var rsaPublicKey = RSAPublicKey.fromString(publicKey);
29 1 : return rsaPublicKey.encrypt(aesKey);
30 : }
31 :
32 1 : static String decryptKey(String aesKey, String privateKey) {
33 1 : var rsaPrivateKey = RSAPrivateKey.fromString(privateKey);
34 1 : return rsaPrivateKey.decrypt(aesKey);
35 : }
36 :
37 0 : static List<int> encryptBytes(List<int> value, String encryptionKey) {
38 0 : var aesEncrypter = Encrypter(AES(Key.fromBase64(encryptionKey)));
39 0 : var initializationVector = IV.fromLength(16);
40 : var encryptedValue =
41 0 : aesEncrypter.encryptBytes(value, iv: initializationVector);
42 0 : return encryptedValue.bytes;
43 : }
44 :
45 0 : static List<int> decryptBytes(
46 : List<int> encryptedValue, String decryptionKey) {
47 0 : var aesKey = AES(Key.fromBase64(decryptionKey));
48 0 : var decrypter = Encrypter(aesKey);
49 0 : var iv2 = IV.fromLength(16);
50 0 : return decrypter.decryptBytes(Encrypted(encryptedValue as Uint8List),
51 : iv: iv2);
52 : }
53 : }
|