The. properties file is used in the program as a parameter configuration document, with the benefit of flexible configuration of parameters
Once some parameters of the database are configured, it is bound to involve the database IP, port, user name and password
Properties files are all Unicode encoded plaintext storage, after the program is packaged and delivered, others can easily open the jar using the decompression software to view your. properties file
So some sensitive variables require cryptographic processing
First you need to understand some basic cryptographic algorithms, such as MD5, Des and RSA
MD5 is an irreversible encryption algorithm, using the pattern after the hash pattern to express the need to encrypt characters or files, commonly used in the system login password alignment
The MD5 code is stored in the database, and when the user logs in, the characters entered by the user are hashed into MD5 and the ciphertext in the database is compared.
An irreversible cryptographic algorithm has the advantage that even if the background database is compromised, the other side to take these MD5 scattered Lemivin can not find clear text
Both Des and RSA are reversible cryptographic algorithms that can be solved by keys and ciphertext to get plaintext, the most common 64-bit rotary des algorithm
Provides congenital, good encryption support in Java's JDK, including the famous des
The following Deshelper class shows how to unlock ciphertext for clear text
Package Com.newflypig.des;import Java.security.securerandom;import Javax.crypto.cipher;import Javax.crypto.secretkey;import Javax.crypto.secretkeyfactory;import Javax.crypto.spec.deskeyspec;import sun.misc.base64decoder;/** * Processing Database Password ciphertext to clear text class * @author newflypig *time:2015 October 30 *todo * */public class Deshelper {/** * Description decryption based on key value * @param data * @param key Encryption key byte array * @return * @throws Exception */pri vate Static byte[] Decrypt (byte[] data, byte[] key) throws Exception {//Generate a trustworthy random number source SecureRandom sr = new SecureRandom (); Create a Deskeyspec object from the original key data deskeyspec DKs = new Deskeyspec (key); Create a key factory and use it to convert Deskeyspec to Secretkey object secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES"); Secretkey SecureKey = Keyfactory.generatesecret (DKS); The Cipher object actually completes the decryption operation Cipher Cipher = cipher.getinstance ("DES"); Initialize the Cipher object with a key Cipher.init (Cipher.decrypt_mode, SecureKey, sr); return cipher.dofinal (data); }public static string decrypt (string data, String key) throws Exception {if (data = null) return null; Base64decoder decoder = new Base64decoder () byte[] buf = decoder.decodebuffer (data); byte[] bt = Decrypt (buf, key.getbytes ()); return new String (BT);}}
If you need to have a deeper understanding of the encryption algorithm, you can study the implementation of the principle, for agile development here only need to superficial understanding the use of the line, remember to protect your key is the key
At the beginning of the need to remove the parameters in the properties file to be clear, presumably you should already know which encryption algorithm to use it?
By the way, the DES encryption algorithm above provides only decryption, as there is no need to provide cryptographic functions in the program
You can write your own cryptographic functions yourself, or you can write encrypted statements in your JUnit tests to encrypt your database passwords.
Java encrypts decryption using DES encryption algorithm