简体   繁体   English

如何将字符串从 base64 解码为二进制文件,以便在 flutter 中使用 RSA 对其进行解密?

[英]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:我有一个带有 RSA 的加密字符串,我在 base64 中对其进行了编码,并将其放入 QR 码中:

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.然后我扫描二维码并将 base64 编码(和 RSA 加密)的字符串发送到 flutter 应用程序。 Now I'm trying to decrypt it using the encrypt package and I just can't get it to work.现在我正在尝试使用加密 package解密它,但我无法让它工作。 It just won't take my encrypted base64 string and don't get it decoded back to binary to decrypt it with RSA.它不会接受我加密的 base64 字符串,也不会将其解码回二进制以使用 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.我在最后一行代码中收到错误ArgumentError (Invalid argument(s): Unsupported block type for private key: 153)

I also tried final encryptedLoginID = utf8.decode(base64.decode(base64encryptedLoginID));我也试过final encryptedLoginID = utf8.decode(base64.decode(base64encryptedLoginID)); which gave me the error FormatException (FormatException: Bad UTF-8 encoding 0x70 (at offset 2))这给了我错误FormatException (FormatException: Bad UTF-8 encoding 0x70 (at offset 2))

Could anyone point me to a solution?谁能指出我的解决方案?

Try this code试试这个代码

    final decrypted = encrypter.decrypt(Encrypted.fromBase64(encrypted_string), iv: iv);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM