java - Crypt information in a file -
i'm developing desktop application, , have login form user has fill in order access application.
in login form have jcheckbox
"remember me"
.
when user enters id , password , check "remember me"
jcheckbox
, want program crypt id , password save them in file.
my question is: how can crypt data? , if crypt them , want read them file, have decipher them?
the best solution not encrypt/decrypt username/password since can broken. anyway here java code example can use encrypt/decrypt data. use [aes][1] algorithm.
warning: code keep secret key inside java class not secure way store key, because have access class able view , use key decrypt encrypted information. key should stored in external file.
import javax.crypto.cipher; import javax.crypto.secretkey; import javax.crypto.spec.secretkeyspec; import org.apache.commons.codec.binary.base64; public class cipherutil { private static base64 coder; // linebreak private static byte[] linebreak = {}; // remove base64 encoder default private static string secret = "qhebp92ihc13g741"; // secret key length must // 16 private static secretkey key; private static cipher cipher; static { try { key = new secretkeyspec(secret.getbytes(), "aes"); cipher = cipher.getinstance("aes/ecb/pkcs5padding", "sunjce"); coder = new base64(32, linebreak, true); } catch (throwable t) { t.printstacktrace(); } } public static synchronized string encrypt(string plaintext) throws exception { cipher.init(cipher.encrypt_mode, key); byte[] ciphertext = cipher.dofinal(plaintext.getbytes()); return new string(coder.encode(ciphertext)); } public static synchronized string decrypt(string codedtext) throws exception { byte[] encypted = coder.decode(codedtext.getbytes()); cipher.init(cipher.decrypt_mode, key); byte[] decrypted = cipher.dofinal(encypted); return new string(decrypted); } } [1]: http://en.wikipedia.org/wiki/advanced_encryption_standard
Comments
Post a Comment