我試圖通過使用java.security.spec.RSAPublicKeySpec
和java.security.KeyFactory
實例化java.security.PublicKey
。使用RSAPublicKeySpec時出現InvalidKeySpecException
但運行以下線路時:
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
return KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
我總是從org.bouncycastle
包得到一個異常。
java.security.spec.InvalidKeySpecException: key spec not recognised
at org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi.engineGeneratePublic(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePublic(Unknown Source)
at java.security.KeyFactory.generatePublic(KeyFactory.java:315)
... (25 more)
這讓我困惑,因爲RSAPublicKeySpec應該有任何關係充氣城堡密碼庫?有人可以告訴我我的代碼有什麼問題嗎?
你如何實例化'keyFactory'? –
啊,對不起。我正在實例化它:'keyFactory = KeyFactory.getInstance(「RSA」);' – miho