2016-03-30 5 views
-1

私はアンドロイドアプリでRSA暗号化アルゴリズムを使用してデータを暗号化しようとしています。私はデータを暗号化することができますが、暗号化されたデータを復号化するときはいつでも例外が発生します。AndroidでRSAアルゴリズムを使用してデータを復号する

私はGoogleで試してみましたが、すべてのソリューションを適用しても同じような例外が発生しました。

javax.crypto.IllegalBlockSizeException: input must be under 64 bytes 
    at com.android.org.conscrypt.OpenSSLCipherRSA.engineDoFinal(OpenSSLCipherRSA.java:245) 
    at javax.crypto.Cipher.doFinal(Cipher.java:1340) 
    at eu.agilers.util.Utility.decryptData(Utility.java:193) 
    at eu.agilers.supersail.SuperSail.onCreate(SuperSail.java:46) 
    at android.app.Activity.performCreate(Activity.java:6283) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
    at android.app.ActivityThread.access$900(ActivityThread.java:177) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:5942) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

暗号化方式:

public static String encryptData(String textToEncrypt) { 

    String modulus = "sAyRG6mbVY1XoPGZ9Yh+ZJvI40wxiq4LzoSbLlIdrYLelvzeQZD6Y6eG9XIALpEvnL3ZECf1Emnv17yELrcQ5w=="; 
    String exponent = "AQAB"; 

    try { 

     byte[] modulusBytes = Base64.decode(modulus.getBytes("UTF-8"), Base64.DEFAULT); 
     byte[] exponentBytes = Base64.decode(exponent.getBytes("UTF-8"), Base64.DEFAULT); 

     BigInteger modulusInt = new BigInteger(1, modulusBytes); 
     BigInteger exponentInt = new BigInteger(1, exponentBytes); 

     /* RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulusInt, exponentInt); 
     KeyFactory fact = KeyFactory.getInstance("RSA"); 
     PrivateKey privKey = fact.generatePrivate(rsaPrivKey);*/ 

     RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulusInt, exponentInt); 
     KeyFactory fact = KeyFactory.getInstance("RSA"); 
     PublicKey pubKey = fact.generatePublic(rsaPubKey); 

     Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); 
     cipher.init(Cipher.ENCRYPT_MODE, pubKey); 

     byte[] encryptedByteData = cipher.doFinal(textToEncrypt.getBytes()); 

     return Base64.encodeToString(encryptedByteData, Base64.NO_WRAP); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

復号方法:

public static String decryptData(String data) { 

    try { 

     String modulus = "sAyRG6mbVY1XoPGZ9Yh+ZJvI40wxiq4LzoSbLlIdrYLelvzeQZD6Y6eG9XIALpEvnL3ZECf1Emnv17yELrcQ5w=="; 
     String exponent = "AQAB"; 

     byte[] modulusBytes = Base64.decode(modulus.getBytes("UTF-8"), Base64.DEFAULT); 
     byte[] exponentBytes = Base64.decode(exponent.getBytes("UTF-8"), Base64.DEFAULT); 

     BigInteger modulusInt = new BigInteger(1, modulusBytes); 
     BigInteger exponentInt = new BigInteger(1, exponentBytes); 

     /* RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulusInt, exponentInt); 
     KeyFactory fact = KeyFactory.getInstance("RSA"); 
     PrivateKey privKey = fact.generatePrivate(rsaPrivKey);*/ 

     RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulusInt, exponentInt); 
     KeyFactory fact = KeyFactory.getInstance("RSA"); 
     PublicKey pubKey = fact.generatePublic(rsaPubKey); 

     Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); 
     cipher.init(Cipher.DECRYPT_MODE, pubKey); 

     byte[] base64String = Base64.decode(data, Base64.DEFAULT); 

     byte[] plainBytes = new String(base64String).getBytes("UTF-8"); 

     plainBytes = cipher.update(plainBytes); 

     byte[] values = cipher.doFinal(plainBytes); 

     return new String(values, "UTF-8"); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

私を助けてください。

+1

入力が64バイト未満ですか? – Shark

+0

私は同じメソッドencryptData()を使って文字列を暗号化し、encryptData()メソッドから得た同じ文字列を復号化しています。同様に、 文字列encr = encryptData( "testData"); 文字列decr = decryptData(encr); – Ajay

+1

あなたは私の質問に答えることさえ気にしませんでした... – Shark

答えて

2

まず、公開鍵ではなく秘密鍵で復号化を試みてください。 RSA公開鍵を使用してRSA公開鍵で暗号化されたデータを復号化する方法はまったくありません。

RSA鍵は、公開鍵と秘密鍵のペアになります。解読するには秘密鍵が必要です。公開鍵が公開鍵で暗号化された暗号文を実際に復号することができれば、公開鍵暗号はすべて破壊されます。

関連する問題