I need help on encryption/decryption on Android application.
I explain the situation. I'm actually wrote an application that used content generated and encrypted by an iPhone application.
And for securisation, the user provide his own passphrase to correctly encrypt/decrypt data between different platform...
But, I encount a problem with the encryption/decryption of this passphrase on Android.
I have two functions:
public byte[] crypt(String pStringToCrypt) throws Exception{
byte[] key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
System.arraycopy(this.passphrase.getBytes(), 0, key, 0, this.passphrase.getBytes().length);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(pStringToCrypt.getBytes());
return encrypted;
}
for the crypting of String, and this function:
public String decrypt(byte[] pCryptedStringtoDecrypt) throws Exception{
byte[] key = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
System.arraycopy(this.passphrase.getBytes(), 0, key, 0, this.passphrase.getBytes().length);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
byte[] encrypted = pCryptedStringtoDecrypt;
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(encrypted);
String originalString = new String(original);
return originalString;
}
for the decrypting of String.
When I use the crypt method to encrypt data, I haven't error and the string was encrypted:
encrypted = [26, 119, -108, -24, 81, -128, 18, 35, -96, 10, -38, 69, 111, 40, 109, 107]
If I try to transform this byte into a string, I obtain this string:
encryptedString = "w��Q�#�\n�Eo(mk"
I think the crypting phase was good. Now when I try to decrypt this encrypted String, the application crashed:
javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:711)
at javax.crypto.Cipher.doFinal(Cipher.java:1090)
at org.vincentsaluzzo.lightrpc.common.security.AES256.decrypt(AES256.java:61)
at com.vincentsaluzzo.LoginBox.model.SettingsManager.getUserPassphrase(SettingsManager.java:67)
at com.vincentsaluzzo.LoginBox.mainActivity.onCreate(mainActivity.java:26)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3647)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
And I don't understand why this error appear...
Do you have some solutions ? or some explanation for me ?