Comment gérer "dernier bloc incomplet dans le décryptage"

J'ai une classe simple pour essayer et enveloppez-les de chiffrement à utiliser ailleurs dans mon programme.

import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
public final class StupidSimpleEncrypter
{
public static String encrypt(String key, String plaintext)
{
byte[] keyBytes = key.getBytes();
byte[] plaintextBytes = plaintext.getBytes();
byte[] ciphertextBytes = encrypt(keyBytes, plaintextBytes);
return new String(ciphertextBytes);
}
public static byte[] encrypt(byte[] key, byte[] plaintext)
{
try
{
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec spec = new SecretKeySpec(getRawKey(key), "AES");
cipher.init(Cipher.ENCRYPT_MODE, spec);
return cipher.doFinal(plaintext);
}
catch(Exception e)
{
//some sort of problem, return null because we can't encrypt it.
Utility.writeError(e);
return null;
}
}
public static String decrypt(String key, String ciphertext)
{
byte[] keyBytes = key.getBytes();
byte[] ciphertextBytes = ciphertext.getBytes();
byte[] plaintextBytes = decrypt(keyBytes, ciphertextBytes);
return new String(plaintextBytes);
}
public static byte[] decrypt(byte[] key, byte[] ciphertext)
{
try
{
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec spec = new SecretKeySpec(getRawKey(key), "AES");
cipher.init(Cipher.DECRYPT_MODE, spec);
return cipher.doFinal(ciphertext);
}
catch(Exception e)
{
//some sort of problem, return null because we can't encrypt it.
Utility.writeError(e);
return null;
}
}
private static byte[] getRawKey(byte[] key)
{
try
{
KeyGenerator gen = KeyGenerator.getInstance("AES");
SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
rand.setSeed(key);
gen.init(256, rand);
return gen.generateKey().getEncoded();
}
catch(Exception e)
{
return null;
}
}
}

Il semble gérer le chiffrement correctement, mais pas tellement quand décryptage, qui jette un javax.crypto.IllegalBlockSizeException "dernier bloc incomplet dans le déchiffrement" à la ligne en surbrillance. Voici la trace de la pile:

Emplacement:com.xxxxxx.android.StupidSimpleEncrypter.décrypter ln:49 
le dernier bloc incomplet dans le déchiffrement 
javax.crypto.IllegalBlockSizeException: dernier bloc incomplet dans le déchiffrement 
au org.bouncycastle.la jce.fournisseur de.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:711) 
au javax.crypto.Algorithme de chiffrement.doFinal(Cipher.java:1090) 
au com.xxxxxx.android.StupidSimpleEncrypter.décrypter(StupidSimpleEncrypter.java:44) 
au com.xxxxxx.android.StupidSimpleEncrypter.décrypter(StupidSimpleEncrypter.java:34) 

J'ai fait une bonne quantité de cogner ma tête contre mon bureau pour essayer de le comprendre, mais si je reçois partout, il finit par être une autre exception. Aussi je n'arrive pas à trouver beaucoup par la recherche.

Ce qui me manque? Je vous serais reconnaissant de toute aide.

source d'informationauteur Jake Basile | 2011-01-04