我使用以下代码创建密钥,但是当我尝试使用KeyGeneration.getPublicKey()
返回时null
。
public KeyGeneration() throws Exception,(more but cleared to make easier to read)
{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
}
public static PublicKey getPublicKey() { return publicKey; }
错误信息如下:
java.security.InvalidKeyException: No installed provider supports this key: (null)
at javax.crypto.Cipher.chooseProvider(Cipher.java:878)
at javax.crypto.Cipher.init(Cipher.java:1213)
at javax.crypto.Cipher.init(Cipher.java:1153)
at RSAHashEncryption.RSAHashCipher(RSAHashEncryption.java:41)
at RSAHashEncryption.exportEHash(RSAHashEncryption.java:21)
at Main.main(Main.java:28)
如果您想查看完整的代码,我可以在这里发布。