user3077162
user3077162

Reputation: 231

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: Detect premature EOF

i have this code :

// Turn the encoded key into a real RSA public key.
// Public keys are encoded in X.509.
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);

error:

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: Detect premature EOF

where is the problem?

Upvotes: 2

Views: 19939

Answers (1)

Katherine Y
Katherine Y

Reputation: 21

public static PublicKey getPublicKey(String key) throws Exception {
      byte[] keyBytes;  
      keyBytes = (new BASE64Decoder()).decodeBuffer(key); 
      X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); 
      KeyFactory keyFactory = KeyFactory.getInstance("RSA");
      PublicKey publicKey = keyFactory.generatePublic(keySpec); 
      return publicKey;
  }

this is how i turn "String key" into real rsa key(PublicKey publicKey). maybe helps.

Upvotes: 2

Related Questions