javax.crypto.IllegalBlockSizeException: dernier bloc incomplet dans le déchiffrement d'exception
Je suis en train de décrypter une chaîne dans android. Je reçois l'exception suivante:
08-21 03:56:56.700: W/System.err(4208): javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
08-21 03:56:56.700: W/System.err(4208): at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:697)
08-21 03:56:56.700: W/System.err(4208): at javax.crypto.Cipher.doFinal(Cipher.java:1106)
08-21 03:56:56.700: W/System.err(4208): at com.dharani.android.legalplex.BusinessLayer.BLCommonOperations.decrypt(BLCommonOperations.java:284)
08-21 03:56:56.700: W/System.err(4208): at com.dharani.android.legalplex.BusinessLayer.BLCommonOperations.decryptAndgetFailCountFromPreferences(BLCommonOperations.java:144)
08-21 03:56:56.700: W/System.err(4208): at com.dharani.android.legalplex.PresentationLayer.TransparentActivity.onCreate(TransparentActivity.java:112)
08-21 03:56:56.700: W/System.err(4208): at android.app.Activity.performCreate(Activity.java:4465)
08-21 03:56:56.700: W/System.err(4208): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-21 03:56:56.700: W/System.err(4208): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
08-21 03:56:56.700: W/System.err(4208): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
08-21 03:56:56.700: W/System.err(4208): at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-21 03:56:56.700: W/System.err(4208): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
08-21 03:56:56.700: W/System.err(4208): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 03:56:56.700: W/System.err(4208): at android.os.Looper.loop(Looper.java:137)
08-21 03:56:56.700: W/System.err(4208): at android.app.ActivityThread.main(ActivityThread.java:4507)
08-21 03:56:56.700: W/System.err(4208): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 03:56:56.700: W/System.err(4208): at java.lang.reflect.Method.invoke(Method.java:511)
08-21 03:56:56.700: W/System.err(4208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-21 03:56:56.700: W/System.err(4208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-21 03:56:56.700: W/System.err(4208): at dalvik.system.NativeStart.main(Native Method)
Le chiffrer et de déchiffrer les méthodes sont les suivantes:
public String encrypt(String message) throws Exception
{
String salt = SharedVariables.globalContext.getString(R.string.EncryptionKey);
SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(message.getBytes());
String encrypted=Base64.encodeToString(encVal, Base64.DEFAULT);
return encrypted;
}
public String decrypt(String message) throws Exception
{
String salt = SharedVariables.globalContext.getString(R.string.EncryptionKey);
Cipher c = Cipher.getInstance("AES");
SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = Base64.decode(message.getBytes(), Base64.DEFAULT);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
Pouvez vous s'il vous plaît fournir des données de test qui provoque cette erreur lorsque vous souhaitez appeler comme ceci:
Bon d'entendre que vous avez trouvé la réponse à votre question. Vous devriez poster une réponse, avec une explication de ce qui doit être changé, et puis vous pouvez l'accepter. Cela permet aux autres utilisateurs de trouver dans l'avenir, et leur permet de savoir ce qui a fonctionné pour vous.
decrypt(encrypt(testData))
. Également inclure la valeur de clé de chiffrement que vous avez utilisé.Bon d'entendre que vous avez trouvé la réponse à votre question. Vous devriez poster une réponse, avec une explication de ce qui doit être changé, et puis vous pouvez l'accepter. Cela permet aux autres utilisateurs de trouver dans l'avenir, et leur permet de savoir ce qui a fonctionné pour vous.
OriginalL'auteur sravanalakshmi.sunkara | 2013-08-21
Vous devez vous connecter pour publier un commentaire.
J'ai eu la solution.Voici le nouveau code qui a fonctionné pour moi
il n'y a pas Base64.NO_OPTIONS disponibles: - no_padding, no_wrap, no_close seulement
Attention: ce code utilise la BCE, à l'aide d'un caché, chaîne par défaut ("AES" est "AES/ECB/PKCS5Padding" pour la plupart des fournisseurs). La BCE est en mode pas sûr.
OriginalL'auteur sravanalakshmi.sunkara
salt.getBytes()
devrait êtresalt.getBytes(UNICODE_FORMAT)
OriginalL'auteur votiethuy