2010-05-06 7 views
0

J'écris une application qui crypte et décrypte les notes de l'utilisateur en fonction du mot de passe défini par l'utilisateur. i utilisé les algorithmes suivants pour le chiffrement/déchiffrement 1. PBEWithSHA256And256BitAES-CBC-BC 2. PBEWithMD5And128BitAES-CBC-OpenSSLException - Taille de bloc illégale lors du décryptage (Android)

e_Cipher = Cipher.getInstance(PBEWithSHA256And256BitAES-CBC-BC); 
    d_Cipher = Cipher.getInstance(PBEWithSHA256And256BitAES-CBC-BC); 
    e_Cipher.init() 
    d_Cipher.init() 

cryptage fonctionne bien, mais en essayant de le déchiffrer donne

Exception - Taille de bloc illégale

Après le chiffrement, je convertis le texte chiffré en HEX et le stocke dans une base de données sqlite. Je récupère les valeurs correctes de la base de données sqlite lors du déchiffrement mais lors de l'appel d_Cipher.dofinal() il lance l'exception.

Je pensais que j'avais manqué de spécifier le remplissage et j'ai essayé de vérifier quels sont les autres algorithmes de chiffrement disponibles, mais je n'ai pas pu trouver.

alors demandez-nous s'il vous plaît donner quelques connaissances sur quels sont les algorithmes de chiffrement et de remplissage qui sont pris en charge par Android? Si l'algorithme que j'ai utilisé peut être utilisé pour le remplissage, comment devrais-je spécifier le mécanisme de remplissage? Je suis assez nouveau à Encryption donc essayé quelques algorithmes qui sont disponibles en BouncyCastle.java mais échoué.

Comme demandé ici est le code

public class CryptoHelper { 
private static final String TAG = "CryptoHelper"; 
//private static final String PBEWithSHA256And256BitAES = "PBEWithSHA256And256BitAES-CBC-BC"; 
//private static final String PBEWithSHA256And256BitAES = "PBEWithMD5And128BitAES-CBC-OpenSSL"; 
private static final String PBEWithSHA256And256BitAES = "PBEWithMD5And128BitAES-CBC-OpenSSLPBEWITHSHA1AND3-KEYTRIPLEDES-CB"; 
private static final String randomAlgorithm = "SHA1PRNG"; 
public static final int SALT_LENGTH = 8; 
public static final int SALT_GEN_ITER_COUNT = 20; 
private final static String HEX = "ABCDEF"; 

private Cipher e_Cipher; 
private Cipher d_Cipher; 
private SecretKey secretKey; 
private byte salt[]; 

public CryptoHelper(String password) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException { 
    char[] cPassword = password.toCharArray(); 
    PBEKeySpec pbeKeySpec = new PBEKeySpec(cPassword); 
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, SALT_GEN_ITER_COUNT); 
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBEWithSHA256And256BitAES); 
    secretKey = keyFac.generateSecret(pbeKeySpec); 

    SecureRandom saltGen = SecureRandom.getInstance(randomAlgorithm); 
    this.salt = new byte[SALT_LENGTH]; 
    saltGen.nextBytes(this.salt); 

    e_Cipher = Cipher.getInstance(PBEWithSHA256And256BitAES); 
    d_Cipher = Cipher.getInstance(PBEWithSHA256And256BitAES); 

    e_Cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParamSpec); 
    d_Cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParamSpec); 
} 

public String encrypt(String cleartext) throws IllegalBlockSizeException, BadPaddingException { 
    byte[] encrypted = e_Cipher.doFinal(cleartext.getBytes()); 

    return convertByteArrayToHex(encrypted); 
} 

public String decrypt(String cipherString) throws IllegalBlockSizeException { 
    byte[] plainText = decrypt(convertStringtobyte(cipherString)); 

    return(new String(plainText)); 
} 

public byte[] decrypt(byte[] ciphertext) throws IllegalBlockSizeException {   
    byte[] retVal = {(byte)0x00}; 
    try { 
     retVal = d_Cipher.doFinal(ciphertext); 
    } catch (BadPaddingException e) { 
     Log.e(TAG, e.toString()); 
    } 
    return retVal; 
} 


public String convertByteArrayToHex(byte[] buf) { 
    if (buf == null) 
     return ""; 
    StringBuffer result = new StringBuffer(2*buf.length); 

    for (int i = 0; i < buf.length; i++) { 
     appendHex(result, buf[i]); 
    } 
    return result.toString(); 
} 

private static void appendHex(StringBuffer sb, byte b) { 
    sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f)); 
} 

private static byte[] convertStringtobyte(String hexString) { 
    int len = hexString.length()/2; 
    byte[] result = new byte[len]; 
    for (int i = 0; i < len; i++) { 
     result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue(); 
    } 
    return result; 
} 

public byte[] getSalt() { 
    return salt; 
} 

public SecretKey getSecretKey() { 
    return secretKey; 
} 

public static SecretKey createSecretKey(char[] password) throws NoSuchAlgorithmException, InvalidKeySpecException { 
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password); 
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBEWithSHA256And256BitAES); 
    return keyFac.generateSecret(pbeKeySpec); 
} 

}

J'appellerai mCryptoHelper.decrypt(String str) alors cela se traduit par la taille du bloc illégal exception Mon Env: Android 1.6 sur Eclipse

+0

Veuillez couper et coller du code réel au lieu d'essayer de saisir quelques fragments. Ensuite, nous pouvons copier votre code dans notre environnement préféré (Eclipse est le mien) et l'exercer. –

Répondre

1

Dans le code, chaque fois que je génère le "sel",

SecureRandom saltGen = SecureRandom.getInstance(randomAlgorithm); 
this.salt = new byte[SALT_LENGTH]; 
saltGen.nextBytes(this.salt); 

il y a donc une différence entre le cryptage et le cryptage. il donne donc le bloc Bad Pad ou le bloc Padding corrompu. Si je déclare le sel à une certaine valeur connue, cela fonctionne bien.

1

@Vamsi est correct, il semble qu'un nouveau sel est généré. Cela devrait être généré une fois, et stocké comme un connu pour le programme. Si le sel change, les contrôles de chiffrement/déchiffrement ne vont pas correspondre.

Questions connexes