Java RSA加密不适用于从XML文件获取的密钥
我一直在尝试使用给定指数和模的RSA加密AES密钥。 我从一个C#restful Web服务获取关键XML文件。 XML键看起来有点像这样:Java RSA加密不适用于从XML文件获取的密钥,java,android,android-studio,cryptography,rsa,Java,Android,Android Studio,Cryptography,Rsa,我一直在尝试使用给定指数和模的RSA加密AES密钥。 我从一个C#restful Web服务获取关键XML文件。 XML键看起来有点像这样: <RSAKeyValue><Modulus>vJaqEtwrfG...LFF7XACWCb6lQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue> 我遇到了以下例外情况,在过去的一周里,我一直在处理这个问题。有什么线索可以帮我解决吗
<RSAKeyValue><Modulus>vJaqEtwrfG...LFF7XACWCb6lQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
我遇到了以下例外情况,在过去的一周里,我一直在处理这个问题。有什么线索可以帮我解决吗
W/System.err:java.lang.RuntimeException:error:0400065:RSA例程:OPENSSL\u内部:错误值
这可能与UTF-16编码有关吗?
StandardCharsets.UTF_16.encode(strModulusBytes.array();
非常感谢。模数和指数是Base64编码的,因此只需使用Base64.getDecoder()对字符串进行解码,即可获得用于获取大整数的字节数组。第二:你不需要utf16,utf8可以工作。
public void initPublicKeyFromServer(Context context) {
String strModulusBytes = "";
String strExpBytes = "";
Log.i("rsaEncryptSecretKey", "pos1");
try {
InputStream inputStream = context.openFileInput("puk.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_16);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = bufferedReader.readLine();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(receiveString));
int eventType = xpp.getEventType();
xpp.next();
String xmlTag = xpp.getName();
while (eventType != XmlPullParser.END_DOCUMENT) {
eventType = xpp.next();
xmlTag = xpp.getName();
if(eventType == XmlPullParser.START_TAG) {
if (xmlTag.equals("RSAKeyValue")) {
continue;
}
}
if(eventType == XmlPullParser.START_TAG) {
if (xmlTag.equals("Modulus")) {
eventType = xpp.next();
xmlTag = xpp.getName();
strModulusBytes = xpp.getText();
eventType = xpp.next();
xmlTag = xpp.getName();
}
}
if(eventType == XmlPullParser.START_TAG) {
if (xmlTag.equals("Exponent")) {
eventType = xpp.next();
xmlTag = xpp.getName();
strExpBytes = xpp.getText();
eventType = xpp.next();
xmlTag = xpp.getName();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
byte[] modBytes = StandardCharsets.UTF_16.encode(strModulusBytes).array();
byte[] expBytes = StandardCharsets.UTF_16.encode(strExpBytes).array();
BigInteger modules = new BigInteger(1, modBytes);
BigInteger exponent = new BigInteger(1, expBytes);
try {
KeyFactory factory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modules, exponent);
publicKey = factory.generatePublic(pubSpec);
rsaCipher = Cipher.getInstance(KeyPairInstanceType);
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
} catch (Exception e) {
e.printStackTrace();
}
}
public byte[] rsaEncrypt(final byte[] plain) throws Exception {
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = rsaCipher.doFinal(plain);
return encryptedBytes;
}
public byte[] rsaEncrypt(final String plain) throws Exception {
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = rsaCipher.doFinal(plain.getBytes());
return encryptedBytes;
}