使用XML私钥解密RSA

时间:2017-03-16 14:51:42

标签: java android xml encryption rsa

我从m团队获得了xml格式的私钥。我想解密他们发给我的消息(在应用程序中)。

XML: enter image description here

我决定只从代码中获取模块和D,因为我已经阅读了足以解码的内容 当我运行此代码时,我有一个错误:java.lang.ArrayIndexOutOfBoundsException:RSA块的数据太多

有什么问题?

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:错误列表

在byte [] decrypted = cipher.doFinal(s.getBytes());

上发生错误
W/System.err: java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
W/System.err:     at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(CipherSpi.java:459)
W/System.err:     at javax.crypto.Cipher.doFinal(Cipher.java:1502)
W/System.err:     at com.divofmod.cryprography.MainActivity.decrypt(MainActivity.java:114)
W/System.err:     at com.divofmod.cryprography.MainActivity.onCreate(MainActivity.java:72)
W/System.err:     at android.app.Activity.performCreate(Activity.java:6309)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
W/System.err:     at android.app.ActivityThread.access$900(ActivityThread.java:175)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:111)
W/System.err:     at android.os.Looper.loop(Looper.java:207)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5728)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

1 个答案:

答案 0 :(得分:1)

最终版

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);
}