1

I'm trying to create public key from string in this way:

byte[] keyBytes = Base64.decode(publicKeyStr.getBytes("utf-8"), Base64.DEFAULT);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
PublicKey publicKey = keyFactory.generatePublic(spec);

but

keyFactory.generatePublic(spec);

causes error:

java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long

Any other solutions and examples of creating PublicKey form String ?

The String is:

87Avu4YRFh25mG/5/vW7tFXORD3WYqByKZDHpcqstXKDyLYFtm8arbwXDUxcDRSJUKdZx07L1fGvExfslShssJ22nHA4bScQ7yhgrtsxNUa5NPPP/YlR9DLAnjFzf3AEbW26fLO5luQtDIDmjSvwNE6ZwAIjhRJ3HXpqw2hFMvc=AQAB

resueman
  • 10,572
  • 10
  • 31
  • 45
Ferdezo
  • 191
  • 1
  • 2
  • 8

1 Answers1

0

I've found solution here RSA. Java Encryption .NET Decryption

The problem was that my string was Modulus + Exponential in one string. When i split it to two diferent Strings and generate like there. The key was generated successfuly. Also there was a problem with encoding UTF-16LE.

Community
  • 1
  • 1
Ferdezo
  • 191
  • 1
  • 2
  • 8