Skip to content

Instantly share code, notes, and snippets.

@brasizza
Created September 24, 2024 13:37
Show Gist options
  • Save brasizza/b51507d6fe6e6305b7e795ccd153a64d to your computer and use it in GitHub Desktop.
Save brasizza/b51507d6fe6e6305b7e795ccd153a64d to your computer and use it in GitHub Desktop.
static Future<String> asymmetricEncript(String text) async {
String key = '';
if (kIsWeb) {
PickedFile localFile = PickedFile('assets/key/pdv.pem');
key = (await localFile.readAsString());
} else {
key = await rootBundle.loadString('assets/key/pdv.pem');
}
final parser = RSAKeyParser();
final publicKey = parser.parse(key) as RSAPublicKey;
final encrypter = Encrypter(RSA(publicKey: publicKey));
final encrypted = encrypter.encrypt(text);
return (encrypted.base64);
}
static Future<String> asymmetricDencript(String text) async {
String key = '';
if (kIsWeb) {
PickedFile localFile = PickedFile('assets/key/pdv.key');
key = (await localFile.readAsString());
} else {
key = await rootBundle.loadString('assets/key/pdv.key');
}
final parser = RSAKeyParser();
final private = parser.parse(key) as RSAPrivateKey;
final encrypter = Encrypter(RSA(privateKey: private));
final decriptedString = encrypter.decrypt(Encrypted(base64Decode(text)));
return (decriptedString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment