How to do AES Decryption in android, without writing decrypted file in SDcard storage? -
how aes decryption in android, without writing decrypted file in sdcard storage, directly use android application? or if other way file encryption offline data storage?
i used aes in android once, method use encrypt:
public static byte[] encryptaes(secretkey key, byte[] clear) { try { secretkeyspec skeyspec = new secretkeyspec(key.getencoded(), "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.encrypt_mode, skeyspec); byte[] encrypted = cipher.dofinal(clear); return encrypted; } catch (exception e) { e.printstacktrace(); } return null; }
this decrypting method:
public static byte[] decryptaes(secretkey key, byte[] encrypted) { try { secretkeyspec skeyspec = new secretkeyspec(key.getencoded(), "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.decrypt_mode, skeyspec); byte[] decrypted = cipher.dofinal(encrypted); return decrypted; } catch (exception e) { e.printstacktrace(); } return null; }
the following method generates random key aes:
public secretkey newaeskey() { try { string s_key = new biginteger(130, random).tostring(32); keygenerator kgen = keygenerator.getinstance("aes"); securerandom sr = securerandom.getinstance("sha1prng"); sr.setseed(s_key.getbytes()); kgen.init(128, sr); // 192 , 256 bits may not available secretkey skey = kgen.generatekey(); return skey; } catch (exception e) { e.printstacktrace(); } return null; }
so first generate random aes key , encrypt , decrypt bytes of want, in case used string
data.
secretkey key = newaeskey(); .... string params = "...."; byte[] encrypted_params = encryptaes(key, params.getbytes());
Comments
Post a Comment