2017-03-16 184 views
2

我從m團隊獲得了xml格式的私鑰。我想解密他們發送給我的消息(在應用程序中)。使用XML專用密鑰解密RSA

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

UPDATE 1: Errror列表

在字節[]解密= cipher.doFinal誤差occures(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) 
+0

郵政完整的堆棧跟蹤和線路taht導致異常 – pedrofb

+0

是在基地64解密編碼的數據?密文的大小是多少?(RSA受密鑰長度限制)。你在使用pkcs1padding嗎?嘗試'Cipher.getInstance(「RSA/ECB/PKCS1Padding」);' – pedrofb

+0

@pedrofb添加。當然,base 64我在C#上有「相同」的代碼,它可以與base64一起使用 –

回答

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