package com.FitBank.crypt; import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher; import javax.crypto.CipherOutputStream; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class TripleDESPassword { private static final SecretKey key; private static final String algorithm = "DESede"; private static Cipher cipher; static { try { final byte[] digestOfPassword = "er48nsjhwlG593mjhgdb20ih".getBytes("utf-8"); key = new SecretKeySpec(digestOfPassword, algorithm); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } } public static String getEncodedPassword(String clearTextPassword) throws Exception { return HexString.byteArrayToHexString(encrypt(clearTextPassword)); } public static boolean testPassword(String clearTextTestPassword, String encodedActualPassword) throws Exception { String encodedTestPassword = TripleDESPassword.getEncodedPassword( clearTextTestPassword); return (encodedTestPassword.equals(encodedActualPassword)); } public String getWithOutCrypt(String clearTextPassword) { return clearTextPassword; } public static byte[] encrypt(String message) throws Exception { if ((message == null) || (message.compareTo("") == 0)) { return message.getBytes(); } init(Cipher.ENCRYPT_MODE); ByteArrayOutputStream bout = new ByteArrayOutputStream(); OutputStream out = new CipherOutputStream(bout,cipher); out.write(message.getBytes("utf-8")); out.flush(); out.close(); return bout.toByteArray(); } public static String decrypt(byte[] message) throws Exception { if (message == null) { return HexString.byteArrayToHexString(message); } init(Cipher.DECRYPT_MODE); ByteArrayOutputStream bout = new ByteArrayOutputStream(); OutputStream out = new CipherOutputStream(bout, cipher); out.write(message); out.flush(); out.close(); return new String(bout.toByteArray(), "UTF-8"); } /** * Inicializa la clase * * @param pType * @throws Exception */ private static void init(int pType) throws Exception { byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A }; AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); cipher = Cipher.getInstance(algorithm + "/CBC/PKCS5Padding"); cipher.init(pType, key, paramSpec); } }