Pages

Android 4.4 problème encodage/décodage password sujet

vendredi 31 janvier 2014




Bonjour,

J'ai décidé de tester mon application sur Android 4.4 et je rencontre un problème avec de décodage d'un mot de passe.

J'ai un mot de passe que je crypte avec la méthode ci-dessous :


Code:


        public static String encryptToAES128(String content, String codePIN,
                        String codeIMEI) throws Exception {
                // Tableau de byte
                byte[] toConvert = content.getBytes(CHARSET);

                // Création du PBEKeySpec
                PBEKeySpec pbeKeySpec = new PBEKeySpec(codePIN.toCharArray(),
                                codeIMEI.getBytes(), iterationCount, keylength);

                // Aglo
                SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBE_ALGORITHM);
                Log.i("Cryptage", "SecreKeyFactory OK");

                // Génération de la clé secrete
                SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

                // Algo
                Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

                // Init de l'algo
                cipher.init(Cipher.ENCRYPT_MODE, pbeKey);

                // Cryptage
                byte[] result = cipher.doFinal(toConvert);

                // Retour de la valeur en base64
                return Base64.encodeToString(result, Base64.DEFAULT);
        }


Je stock ce mot de passe crypté dans mes sharedPreferences. Puis le récupère plus tard pour le décrypté:

Code:


public static String decryptFromAES128(String encrypted, String codePIN,
                        String codeIMEI) throws Exception {
                // Tableau de byte
                byte[] toConvert = Base64.decode(encrypted, Base64.DEFAULT);

                // Création du PBEKeySpec
                PBEKeySpec pbeKeySpec = new PBEKeySpec(codePIN.toCharArray(),
                                codeIMEI.getBytes(), iterationCount, keylength);
                SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBE_ALGORITHM);
                SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

                // Init de l'algo
                Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
                cipher.init(Cipher.DECRYPT_MODE, pbeKey);

                // Décryptage
                byte[] result = cipher.doFinal(toConvert);

                return new String(result, CHARSET);
        }


Voici la valeur des différentes variables :


Code:


        /**
        * Aglorthme de Hash HmacSHA1.
        */
        public final static String ALGO_HASHAGE = "HmacSHA1";

        /**
        * La constante PBE_ALGORITHM.
        */
        private static final String PBE_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";

        /**
        * La constante CIPHER_ALGORITHM.
        */
        private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";

        /**
        * Format.
        */
        public final static String CHARSET = "UTF-8";

        /**
        * Nombre d'itération.
        */
        public final static int iterationCount = 10;

        /**
        * Taille de la clé.
        */
        public final static int keylength = 128;


Mais lors du décryptage j'ai une exception de levée. Ce qui n'était pas le cas avec les versions précédentes d'Android.

Quelqu'un à t'il rencontré ce genre de soucis avec Android 4.4 ?

Merci d'avance.




Aucun commentaire:

Enregistrer un commentaire