在我的程序中,我正在尝试使用以下代码使用RSA加密某些明文:
static String RSAEncrypt(String pubkey, String plain){
return encrypt(pubkey,plain,"RSA");
}
static String encrypt(String stringKey, String plain, String algo){
String enc="failed";
try{
byte[] byteKey = new BASE64Decoder().decodeBuffer(stringKey);
Key key = new SecretKeySpec(byteKey,algo);
byte[] data = plain.getBytes();
Cipher c = Cipher.getInstance(algo);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(data);
enc = new BASE64Encoder().encode(encVal);
}catch(Exception e){e.printStackTrace();}
return enc;
}
Run Code Online (Sandbox Code Playgroud)
但是,当它运行时,它会显示以下错误:
java.security.InvalidKeyException: No installed provider supports this key: javax.crypto.spec.SecretKeySpec
at javax.crypto.Cipher.chooseProvider(Cipher.java:877)
at javax.crypto.Cipher.init(Cipher.java:1212)
at javax.crypto.Cipher.init(Cipher.java:1152)
at Crypto.encrypt(Crypto.java:37)
at Crypto.RSAEncrypt(Crypto.java:62)
Run Code Online (Sandbox Code Playgroud)
我已经尝试将其更改为RSA/None/PKCS1Padding和RSA/ECB/PKCS1Padding无济于事.我知道安装BouncyCastle可能有所帮助,但我想避免它(我想避免更多的依赖,我已经无论如何都要安装它的一些问题).提前感谢任何想法.