Thursday, 15 August 2013

java - SecretKeyFactory generateSecret error when passing PBEKeySpec -



java - SecretKeyFactory generateSecret error when passing PBEKeySpec -

i'm trying encrypt , decrypt java string. purpose wrote 2 methods below :

public static string aesencryptstring(string clearstr) throws exception { string cipherstr = null; //génération de la clé de cryptage aes secretkeyfactory mill = secretkeyfactory.getinstance("pbkdf2withhmacsha1"); pbekeyspec spec = new pbekeyspec(key.tochararray()); log.d("test", ""+ spec); secretkey tmp = factory.generatesecret(spec); secretkey key = new secretkeyspec(tmp.getencoded(), "aes"); //cryptage du mot de passe cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding"); cipher.init(cipher.encrypt_mode, key); byte[] cipherbytearray = cipher.dofinal(clearstr.getbytes("utf-8")); //convertion du mot de passe en string pour l'enregistrement en base of operations cipherstr = new string(base64.encode(cipherbytearray, 0)); homecoming cipherstr; } public static string aesdecryptstring(string cipherstr) throws exception { string clearstr = null; //génération de la clé de cryptage aes secretkeyfactory mill = secretkeyfactory.getinstance("pbkdf2withhmacsha1"); keyspec spec = new pbekeyspec(key.tochararray()); secretkey tmp = factory.generatesecret(spec); secretkey key = new secretkeyspec(tmp.getencoded(), "aes"); //décryptage du mot de passe cipher decipher = cipher.getinstance("aes/cbc/pkcs5padding"); decipher.init(cipher.decrypt_mode, key); byte[] clearbytearray = decipher.dofinal(cipherstr.getbytes()); //convertion du mot de passe en string pour l'enregistrement en base of operations clearstr = new string(base64.encode(clearbytearray, 0)); homecoming clearstr; }

the thrown error "invalidkeyspec" during execution of factory.generatesecret -> know error due lack of salt but, if can create pbekeyspec password, should have way utilize it, can help find ?

i tried salt, testing and... doesn't work either error not same. in case error thrown on "cipher.init" , can't figure out error because debugger tells me ""

please help me because i'll going crazy !

when creating pbekeyspec have utilize constructor 4 arguments:

pbekeyspec(char[] password, byte[] salt, int iterationcount, int keylength)

note: can store unencrypted salt right before encrypted text. iterationcount can hard-coded within application.

byte[] salt = new byte[8]; new securerandom().nextbytes(salt); pbekeyspec spec = new pbekeyspec(key.tochararray(), salt, 10000, 128);

the illustration uses 128 aes128 sufficient.

java android aes

No comments:

Post a Comment