encrypt icon indicating copy to clipboard operation
encrypt copied to clipboard

Unable to parse key, invalid format.

Open Altamill opened this issue 4 years ago • 5 comments

I have only sting publicKey : MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCNicre4Igf0UEnfIailqZr3++zh1BMWOpuUXAA2Tjq+RbTobgN654ILTbKmGTj+KIc9CtqxoSbXCa4vUxK04aFl9yrWYtSYmHDn1EXQponUyV3ZwjBvH8tlHr3c3j+Aqbn0qig5RVco0ertktrpfgizlpb51QtbHpOF5PbALZp8QIDAQAB

RSAPublicKey publicKey = RSAKeyParser().parse(response.data['content']);

print: Unable to parse key, invalid format.

if i ...... RSAPublicKey publicKey = response.data['content'];

print...flutter: type 'String' is not a subtype of type 'RSAPublicKey'

Is my usage correct?

Altamill avatar Mar 05 '20 06:03 Altamill

The RSAKeyParser is meant for PEM files only. I'm sorry to say, but there is no way to parse Strings, yet. I'll left your issue open as a feature request.

leocavalcante avatar Mar 05 '20 13:03 leocavalcante

I think that's what most people who ask the same questions mean. They get string instead of PEM. So I used a seemingly stupid but effective way to hack him. I hope it can help others in the short term. :)

  splitStr(String str) {
    var begin = '-----BEGIN PUBLIC KEY-----\n';
    var end = '\n-----END PUBLIC KEY-----';
    int splitCount = str.length ~/ 64;
    List<String> strList=List();

    for (int i=0; i<splitCount; i++) {
      strList.add(str.substring(64*i, 64*(i+1)));
    }
    if (str.length%64 != 0) {
      strList.add(str.substring(64*splitCount));
    }

    return begin + strList.join('\n') + end;
  }
  _login() async {
    try {
      Response response = await Dio().get('xxxxxxxxxx');
      final parser = RSAKeyParser();

      String publicKeyString = splitStr(response.data['content']);
      RSAPublicKey publicKey = parser.parse(publicKeyString);
      final encrypter = Encrypter(RSA(publicKey: publicKey));

      rsaPasswd = encrypter.encrypt(password).base64;
      print(rsaPasswd);
    } catch (e) {
      print(e);
    }
  }

Altamill avatar Mar 05 '20 13:03 Altamill

what is the parser here?

rajatpathak avatar Mar 12 '20 13:03 rajatpathak

what is the parser here?

Not sure what you meant ...

leocavalcante avatar Mar 12 '20 14:03 leocavalcante

I try many ways to use RSA to sign str . But never success. code like follow

Future<String> getSignValue(String signStr) async {
  final publicPem = await rootBundle.loadString('assets/rsa/public.pem');
  final encrypted = Encrypter(RSA(publicKey: RSAKeyParser().parse(publicPem)));
  final sign_value = encrypted.encrypt(signStr);
  return sign_value.base64;
}

jacksonon avatar Apr 26 '21 11:04 jacksonon