java.security.spec.InvalidKeySpecException:java.security.InvalidKeyException:IOException:检测过早的EOF

时间:2013-12-14 17:39:26

标签: public-key-encryption x509

我有这段代码:

// 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);

错误:

  

java.security.spec.InvalidKeySpecException:   java.security.InvalidKeyException:IOException:检测过早的EOF

问题出在哪里?

1 个答案:

答案 0 :(得分:1)

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;
  }

这就是我转向" String key"进入真正的rsa密钥(PublicKey publicKey)。 也许有帮助。