PointyCastle / pointycastle

Moved into the Bouncy Castle project: https://github.com/bcgit/pc-dart
MIT License
270 stars 76 forks source link

Decode PrivateKey #168

Closed 1AlexFix1 closed 5 years ago

1AlexFix1 commented 5 years ago

Hi, I have a problem, because I can not decrypt the information with a private key if it was encrypted somewhere else, swift or kotlin, the output is porridge. maybe i'm not getting my private key right?

 RSAPrivateKey pemToPrivKey(pemPrivKey) {
    try {
      ASN1Parser asn1Parser = ASN1Parser(_decodePEM(pemPrivKey));
      ASN1Sequence asn1sequence = asn1Parser.nextObject();
      var modulus = (asn1sequence.elements[1] as ASN1Integer).valueAsBigInteger;
      var exponent =
          (asn1sequence.elements[3] as ASN1Integer).valueAsBigInteger;
      var p = (asn1sequence.elements[4] as ASN1Integer).valueAsBigInteger;
      var q = (asn1sequence.elements[5] as ASN1Integer).valueAsBigInteger;
      return RSAPrivateKey(modulus, exponent, p, q);
    } catch (e) {
      print(e);
      return null;
    }
  }

List<int> decodeWithPrivKey(List<int> data, RSAPrivateKey privateKey) {
    return (RSAEngine()
          ..init(false, PrivateKeyParameter<RSAPrivateKey>(privateKey)))
        .process(Uint8List.fromList(data))
        .toList();
  }