security - Files Encryption Java -
i'm making system has data in it, , want take data (in xml format) , save encrypted string in txt file, , later on when software opens again, decrypt file , read normal. have code convert xml string, have code save it, need encryption/decryption code?
note: did find code encrypt/decrypt, seems can't split code 2 methods.
here attempt:
public class aesencrdec { public static string encrypt(string data) { byte[] byteciphertext = null; try { string plaindata=data,ciphertext,decryptedtext; keygenerator keygen = keygenerator.getinstance("aes"); keygen.init(128); securerandom rnd = new securerandom(); secretkey secretkey = keygen.generatekey(); ivparameterspec iv; iv = new ivparameterspec(rnd.generateseed(16)); cipher aescipher = cipher.getinstance("aes"); aescipher.init(cipher.encrypt_mode,secretkey,iv); byte[] bytedatatoencrypt = plaindata.getbytes(); byteciphertext = aescipher.dofinal(bytedatatoencrypt); ciphertext = new base64encoder().encode(byteciphertext); return new string(byteciphertext); } catch (invalidkeyexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } catch (nosuchalgorithmexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } catch (nosuchpaddingexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } catch (illegalblocksizeexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } catch (badpaddingexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } catch (invalidalgorithmparameterexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } return new string(byteciphertext); } public static string dencrypt(string data) { byte[] bytedecryptedtext = null; try { keygenerator keygen = keygenerator.getinstance("aes"); keygen.init(128); ivparameterspec iv; securerandom rnd = new securerandom(); iv = new ivparameterspec(rnd.generateseed(16)); cipher aescipher = cipher.getinstance("aes"); secretkey secretkey = keygen.generatekey(); aescipher.init(cipher.encrypt_mode,secretkey,iv); bytedecryptedtext = aescipher.dofinal(data.getbytes()); } catch (nosuchalgorithmexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } catch (nosuchpaddingexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } catch (invalidkeyexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } catch (invalidalgorithmparameterexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } catch (illegalblocksizeexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } catch (badpaddingexception ex) { logger.getlogger(aesencrdec.class.getname()).log(level.severe, null, ex); } return new string(bytedecryptedtext); } }
edit : in responce @libin here errors
mar 24, 2014 6:27:42 pm prefsreadandwrite.aesencrdec decrypt severe: null javax.crypto.badpaddingexception: given final block not padded @ com.sun.crypto.provider.ciphercore.dofinal(ciphercore.java:966) @ com.sun.crypto.provider.ciphercore.dofinal(ciphercore.java:824) @ com.sun.crypto.provider.aescipher.enginedofinal(aescipher.java:436) @ javax.crypto.cipher.dofinal(cipher.java:2121) @ prefsreadandwrite.aesencrdec.decrypt(aesencrdec.java:61) @ prefsreadandwrite.aesencrdec.decryptedstring(aesencrdec.java:104) @ smarthouse.smarthouse.main(smarthouse.java:12) exception in thread "main" java.lang.nullpointerexception @ java.lang.string.<init>(string.java:554) @ prefsreadandwrite.aesencrdec.decryptedstring(aesencrdec.java:105) @ smarthouse.smarthouse.main(smarthouse.java:12) java result: 1
you have generate key once , use encrypt , decrypt.. use code ...
appsecurity class should used generate new key , encrypt/decrypt
public class appsecurity{ private appsecurity() {} public static byte[] encrypt(byte[] key , byte[] data) { secretkeyspec keyspec = new secretkeyspec(key, "aes"); try { cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.encrypt_mode,keyspec); return cipher.dofinal(data); } catch (nosuchalgorithmexception e){ } catch (nosuchpaddingexception e){ } catch (invalidkeyexception e){ } catch (badpaddingexception e){ } catch (illegalblocksizeexception e) {} return null; } public static byte[] decrypt(byte[] key , byte[] encrypteddata) { secretkeyspec keyspec = new secretkeyspec(key ,"aes"); try { cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.decrypt_mode, keyspec); return cipher.dofinal(encrypteddata); } catch (nosuchalgorithmexception e) {} catch (nosuchpaddingexception e) { } catch (invalidkeyexception e) { } catch (badpaddingexception e) {} catch (illegalblocksizeexception e) {} return null; } /** * method generate secure key. call when app starts * @return */ public static byte[] generatekey(){ try{ // create aes algorithm instance. keygenerator keygenerator = keygenerator.getinstance("aes"); securerandom securerandom = securerandom.getinstance("sha1prng"); keygenerator.init(128,securerandom); secretkey secretkey = keygenerator.generatekey(); return secretkey.getencoded(); } catch (nosuchalgorithmexception e){ return null; } } }
method encrypt string.
private static byte[] encryptedbyte(string s) { return appsecurity.encrypt(yourapplication.getsecretkey(),tobytes(s)); }
method decrypt byte.
private static string decryptedstring(byte[] blob) { // here getsecretkey() should 1 used on encryption byte[] decrypted = appsecurity.decrypt(yourapplication.getsecretkey(),blob); return tostring(decrypted);
}
method convert byte string
public static string tostring(byte[] bytes) { try { string s = new string(bytes ,"utf-8"); return s; } catch (unsupportedencodingexception e) { return null; } }
method convert string byte
public static byte[] tobytes(string s) { try { return s.getbytes("utf-8"); } catch (unsupportedencodingexception e) {return null;} }
detail on how use in application : initialize initsecurity() method on application/applet class , store return key variable , make use of on runtime.
assuming class name yourapplication.java
// initialize on app startup string msecretkey = initsecurity() // call method when encrypt /decrypt public static byte[] getsecretkey() { return msecretkey; }
//add method read file or generate 1 new key
private void initsecurity() { final string secretfile = "secure_file"; boolean keyexists = false; //check if secret key exist in secure file try { fileinputstream inputstream = openfileinput(secretfile); msecretkey = new byte[16]; int result = inputstream.read(msecretkey); if(result >0) { keyexists = true; } inputstream.close(); } catch (filenotfoundexception e) {} catch (ioexception e){} if(!keyexists) { // generate key msecretkey = appsecurity.generatekey(); if(msecretkey != null) { // write in secure file inside app try { // mode_private create file (or replace file of same name) // , make private application. fileoutputstream outputstream = openfileoutput(secretfile,context.mode_private); outputstream.write(msecretkey); outputstream.close(); } catch (filenotfoundexception e){} catch (ioexception e) {} } } }
Comments
Post a Comment