我一直在尝试复制用C#编写的代码 代码看起来像这样。
Pkcs1Encoding encryptor = new Pkcs1Encoding(new RsaEngine());
using(StringReader strReader = nee StringReader(publicKey)){
AsymmetricKeyParameter keyParameter= new PermReader(strReader).ReadObject() as AsymmetricKeyParameter;
encryptor.init(true, keyParameter);
}
byte data[]= Encoding.UTF8.GetBytes(text);
return Convert.ToBase64String(encryptor.ProcessBlock(data,0,data.length));
在查看了一些站点并尝试自己进行访问后,我进行了Java转换
{
String encryptedData = null;
try {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
PKCS1Encoding encryptor = new PKCS1Encoding(new RSAEngine());
PemReader pubReader = new PemReader(new StringReader(publicKeyVal));
PemObject ObjectRead=pubReader.readPemObject();
AsymmetricKeyParameter keyParameter = PublicKeyFactory.createKey(pubReader.readPemObject().getContent());
encryptor.init(true, keyParameter);
byte[] data = inputData.getBytes("UTF-8");
encryptedData= Base64.encodeBase64(encryptor.processBlock(data,0,data.length)).toString();
}
catch (Exception e) {
System.out.println(e);
}
return encryptedData;
}
当我执行PublicKeyFactory.createKey
例外是:
java.lang.NullPointerException:尝试调用虚拟方法 'null上的'byte [] org.bouncycastle.util.io.pem.PemObject.getContent()' 对象引用
sysno和AsymmetricKeyParameter行上的异常。
如何解决此问题,因为我需要将其强制转换为AsymmetricKeyParameter才能启动加密器。