Java 用XML私钥解密RSA
我从m团队获得了一个xml格式的私钥。我想解密他们发送给我的消息(应用程序内) XML: 我决定只取代码中的模块和D,因为我已经读过了,它足够解码了 当我运行这段代码时,我有一个错误:java.lang.ArrayIndexOutOfBoundsException:RSA块的数据太多 怎么了Java 用XML私钥解密RSA,java,android,xml,encryption,rsa,Java,Android,Xml,Encryption,Rsa,我从m团队获得了一个xml格式的私钥。我想解密他们发送给我的消息(应用程序内) XML: 我决定只取代码中的模块和D,因为我已经读过了,它足够解码了 当我运行这段代码时,我有一个错误:java.lang.ArrayIndexOutOfBoundsException:RSA块的数据太多 怎么了 static String decrypt(String s) throws Exception { String modulus = "0xp1ux1gDERsUyGUpl+UZB/MK2Tg
static String decrypt(String s) throws Exception
{
String modulus = "0xp1ux1gDERsUyGUpl+UZB/MK2TgZCDENQaM2cDsqiluTnW9jtTScLTrgyBhsCNVjDB7ZnJnWpMMdfFeJkxWSFEjFoKlJAqzs9VmHheLql+hUZavxY3q2x9whXc/IpXIvGXlWYzaLAuHEwbpSY8C0b93elkw1zN4GI3h19Yj+1WVgfPvpmweADocllZEIi26oBHNlcDlBGM/PE+YgownWbBCtr8kzaiZz9TUHjnbNEr8BkK/dKkv9BICBTF98A6c7gf/kiI0mqLAm5l3Eq8PL26kmjju5Bsa5ja4WywTT7CgFHBzlU/OzbHsiQYSKPVrFw7YyXfyZHy4qvtDXA7afQ==";
String dD = "hYkHUAWU7C2cGDn1vghX5b33eLum9a+EbcZm8peHHVx32knATslxFLpc/+VL5g9z3eoNJRDZMAI0r6au16sSKUyp1WNu8w2R/v/OSNq8DlnPwbyAE4diOJn6o3J7DXWSNRp/qdXfbF0eZHrKty0vq15iRZKFwptcLKwTYGSk/iZO951XuI1/hHr45fIxhz6QPBSMF5iWYShhI4zESYqjseytpzlk83npMnI4qghLVk6aQIls5AjWaD8oei4wNJ1S30U3rfQ2mnZrhbMi25G2be9nK/Gt+7/OKPNDsqh00VmKVn4v97Uy8cHZ4+zCQ5C5WtCtamhqmPrbeh7F8LzQQQ==";
byte[] modBytes = decodeBase64(modulus.trim());
byte[] dBytes = decodeBase64(dD);
BigInteger modules = new BigInteger(1, modBytes);
BigInteger d = new BigInteger(1, dBytes);
KeyFactory factory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA");
RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d);
PrivateKey privKey = factory.generatePrivate(privSpec);
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] decrypted = cipher.doFinal(s.getBytes());
return new String(decrypted);
}
方法将base64转换为字节
private static byte[] decodeBase64(String dataToDecode)
{
byte[] dataDecoded = Base64.decode(dataToDecode, Base64.DEFAULT);
return dataDecoded;
}
更新1:错误列表
字节[]decrypted=cipher.doFinal(s.getBytes())上发生错误
最终版本
private String byte[] decodeBase64(String dataToDecode)
{
byte[] dataDecoded = Base64.decode(dataToDecode, Base64.DEFAULT);
return dataDecoded;
}
public static void decrypt(String s) throws Exception
{
String modulus = "your modulus here";
String dD = "your D here";
byte[] modBytes = decodeBase64(modulus.trim());
byte[] dBytes = decodeBase64(dD.trim());
BigInteger modules = new BigInteger(1, modBytes);
BigInteger d = new BigInteger(1, dBytes);
KeyFactory factory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d);
PrivateKey privKey = factory.generatePrivate(privSpec);
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] decrypted = cipher.doFinal(decodeBase64(s));
return new String(decrypted);
}
Post完整堆栈跟踪和taht行导致异常是数据解密编码在base 64中?密文的大小是多少?(RSA受密钥长度限制)。您正在使用pkcs1p添加吗?尝试
Cipher.getInstance(“RSA/ECB/PKCS1Padding”)代码>@pedrofb已添加。当然,base64我在C#上有“相同”的代码,它与base64一起工作,然后从base64解码。Changebyte[]decrypted=cipher.doFinal(decodeBase64(s.getBytes())
@pedrofb much thx too u))byte[]decrypted=cipher.doFinal(decodeBase64(s))代码>工作正常
private String byte[] decodeBase64(String dataToDecode)
{
byte[] dataDecoded = Base64.decode(dataToDecode, Base64.DEFAULT);
return dataDecoded;
}
public static void decrypt(String s) throws Exception
{
String modulus = "your modulus here";
String dD = "your D here";
byte[] modBytes = decodeBase64(modulus.trim());
byte[] dBytes = decodeBase64(dD.trim());
BigInteger modules = new BigInteger(1, modBytes);
BigInteger d = new BigInteger(1, dBytes);
KeyFactory factory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d);
PrivateKey privKey = factory.generatePrivate(privSpec);
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] decrypted = cipher.doFinal(decodeBase64(s));
return new String(decrypted);
}