'How can I decode a string from base64 to binary to decrypt it with RSA in flutter?
I have an encrypted string with RSA that I have encoded in base64 to put it in a QR-code:
echo "thesearesecretlogininfos"|openssl rsautl -encrypt -pubin -inkey public.pem |openssl base64
Then I scan the Qr Code and get the base64 encoded (and RSA encrypted) string to a flutter app. Now I'm trying to decrypt it using the encrypt package and I just can't get it to work. It just won't take my encrypted base64 string and don't get it decoded back to binary to decrypt it with RSA.
Here is the code of what I've tried:
import 'package:encrypt/encrypt.dart';
import 'package:encrypt/encrypt_io.dart';
import 'package:pointycastle/asymmetric/api.dart';
(...)
String base64encryptedLoginID;
String newloginID;
void decrypt(BuildContext context) async {
final myPrivateKey = await rootBundle.loadString('assets/private.pem');
final myPublicKey = await rootBundle.loadString('assets/public.pem');
final privateKey = RSAKeyParser().parse(myPrivateKey) as RSAPrivateKey;
final publicKey = RSAKeyParser().parse(myPublicKey) as RSAPublicKey;
final encrypter = Encrypter(RSA(publicKey: publicKey, privateKey: privateKey));
final newloginID = encrypter.decrypt(Encrypted.fromBase64(base64encryptedLoginID));
I get the Error ArgumentError (Invalid argument(s): Unsupported block type for private key: 153)
in the last line of code.
I also tried final encryptedLoginID = utf8.decode(base64.decode(base64encryptedLoginID));
which gave me the error FormatException (FormatException: Bad UTF-8 encoding 0x70 (at offset 2))
Could anyone point me to a solution?
Solution 1:[1]
Try this code
final decrypted = encrypter.decrypt(Encrypted.fromBase64(encrypted_string), iv: iv);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | shyam jyothi |