Ich versuche, Daten mit dem RSA-Verschlüsselungsalgorithmus in meiner Android-App zu verschlüsseln. Ich bin in der Lage, Daten zu verschlüsseln, aber wann immer ich die verschlüsselten Daten entschlüsseln werde, bekomme ich Exception.Entschlüsseln von Daten mit RSA-Algorithmus in Android
Ich habe in Google versucht und auch versucht, alle Lösungen anwenden, aber immer die gleiche Ausnahme wie unten.
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)
Verschlüsselungsmethode:
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;
}
}
Decryption Methode:
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;
}
}
Bitte helfen Sie mir.
Ist Ihre Eingabe unter 64 Bytes? – Shark
Ich benutze die selbe Methode encryptData() um den String zu verschlüsseln und dann den gleichen String zu entschlüsseln, den ich von der Methode encryptData() bekommen habe. Wie, Zeichenfolge encr = encryptData ("testData"); Zeichenfolge decr = decryptData (encr); – Ajay
Sie haben nicht einmal die Frage beantwortet ... – Shark