-
Notifications
You must be signed in to change notification settings - Fork 135
/
rsa.dart
54 lines (47 loc) · 1.48 KB
/
rsa.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import 'package:encrypt/encrypt.dart';
import 'package:encrypt/encrypt_io.dart';
import 'package:pointycastle/asymmetric/api.dart';
void main() async {
final publicKey = await parseKeyFromFile<RSAPublicKey>('test/public.pem');
final privKey = await parseKeyFromFile<RSAPrivateKey>('test/private.pem');
final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
Encrypter encrypter;
Encrypted encrypted;
String decrypted;
// PKCS1 (Default)
encrypter = Encrypter(RSA(publicKey: publicKey, privateKey: privKey));
encrypted = encrypter.encrypt(plainText);
decrypted = encrypter.decrypt(encrypted);
print('PKCS1 (Default)');
print(decrypted);
print(encrypted.bytes);
print(encrypted.base16);
print(encrypted.base64);
// OAEP (SHA1)
encrypter = Encrypter(
RSA(publicKey: publicKey, privateKey: privKey, encoding: RSAEncoding.OAEP),
);
encrypted = encrypter.encrypt(plainText);
decrypted = encrypter.decrypt(encrypted);
print('\nOAEP (SHA1)');
print(decrypted);
print(encrypted.bytes);
print(encrypted.base16);
print(encrypted.base64);
// OAEP (SHA256)
encrypter = Encrypter(
RSA(
publicKey: publicKey,
privateKey: privKey,
encoding: RSAEncoding.OAEP,
digest: RSADigest.SHA256,
)
);
encrypted = encrypter.encrypt(plainText);
decrypted = encrypter.decrypt(encrypted);
print('\nOAEP (SHA256)');
print(decrypted);
print(encrypted.bytes);
print(encrypted.base16);
print(encrypted.base64);
}