Studyon Minte9.com
Java SE




Encrypt password 1.5




package testexample; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * Criptor example * @arguments //Arguments: encrypt mypassword mysecretkey */ public class Testexample { private static String javaVersion = "1.5"; private static String secretKey = "mykey_1234567890"; private static final char[] PASSWORD = secretKey. toCharArray(); private static final byte[] SALT = { (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12, (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12, }; private final static String CIPHER_MODE = "PBEWithMD5AndDES"; private static String cmd; private static String original; private static String encrypted; private static String decripted; public static void main(String[] args) throws Exception { Testexample c = new Testexample(args); if (cmd.equals("encrypt")) { original = args[1]; encrypt(original); System.out.println("Original: " + original); System.out.println("Encrypted: " + encrypted); } if (cmd.equals("decrypt")) { encrypted = args[1]; decrypt(encrypted); System.out.println("Encrypted: " + encrypted); System.out.println("Decripted: " + decripted); } } public Testexample(String args[]) throws Exception { String[] commands = new String[]{"help", "encrypt", "decrypt"}; if (args.length == 0) help(); cmd = args[0]; if (! Arrays.asList(commands).contains(cmd)) throw new Exception("Error: Invalid command '" + cmd + "'!"); if (args.length != 3 && !cmd.equals("help")) throw new Exception("Error: Wrong params number ("+ args.length +"). Must be 3!"); if (args.length == 3) { if(! secretKey.equals(args[2])) throw new Exception("Error: Wrong secret key!"); if (cmd.equals("encrypt")) { original = args[1]; encrypt(original); } if (cmd.equals("decrypt")) { encrypted = args[1]; decrypt(encrypted); } } } private static void help() { System.out.println("--------------------"); System.out.println("Cryptor for Java " + javaVersion); System.out.println("--------------------"); System.out.println("Encrypt: java -jar cryptor.jar encrypt <password> <secretkey>"); System.out.println("Decrypt: java -jar cryptor.jar decrypt <encrypted> <secretkey>"); System.exit(0); } private static String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(CIPHER_MODE); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance(CIPHER_MODE); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); encrypted = base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8"))); return encrypted; } public static String decrypt(String property) throws GeneralSecurityException, IOException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); decripted = new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8"); return decripted; } private static String base64Encode(byte[] bytes) { return new BASE64Encoder().encode(bytes); } private static byte[] base64Decode(String property) throws IOException { return new BASE64Decoder().decodeBuffer(property); } }
 


http://stackoverflow.com/questions/1132567/encrypt-password-in-configuration-files