package com.fitbank.common.crypto; import lombok.extern.slf4j.Slf4j; import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.Serializable; 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; import com.fitbank.common.properties.PropertiesHandler; @Slf4j public class Decrypt implements Serializable { /** * */ private static final long serialVersionUID = 1L; public static void main(String[] args) { try { Decrypt de = new Decrypt("FIT-2008"); String cadena = "DD5523DC177D2A4BA28377FA14DE1A84"; // cadena = de.encrypt(cadena); cadena = de.decrypt(cadena); } catch (Exception e) { log.error("", e); } } private Cipher cipher; private SecretKey key; private boolean encrypt; private boolean webencrypt; private PropertiesHandler properties; private String algorithm = "DES"; /** * Constructor * * @throws Exception */ public Decrypt() { try { this.properties = new PropertiesHandler("security"); String phrase = this.properties.getStringValue("phrase"); this.encrypt = this.properties.getBooleanValue("encrypt"); this.webencrypt = this.properties.getBooleanValue("webencrypt"); this.algorithm = this.properties.getStringValue("algorithm"); this.key = new SecretKeySpec(phrase.getBytes(), this.algorithm);// "DESede");// // "DES"); } catch (Exception ex) { log.error("", ex); } } public Decrypt(String pPhrase) { try { String phrase = pPhrase; this.encrypt = true; this.webencrypt = true; this.properties = new PropertiesHandler("security"); this.algorithm = this.properties.getStringValue("algorithm"); this.key = new SecretKeySpec(phrase.getBytes(), this.algorithm);// "DESede");// // "DES");"DES"); } catch (Exception ex) { log.error("", ex); } } /** * Byte a Hexadecimal * * @param b * @return */ private String byteArrayToHexString(byte[] b) { StringBuffer sb = new StringBuffer(b.length * 2); for (byte element : b) { int v = element & 0xff; if (v < 16) { sb.append('0'); } sb.append(Integer.toHexString(v)); } return sb.toString().toUpperCase(); } /** * Desencripta un texto * * @param pText * @return * @throws Exception */ public String decrypt(String pText) throws Exception { if (!this.encrypt) { return pText; } if ((pText == null) || (pText.compareTo("") == 0)) { return pText; } this.init(Cipher.DECRYPT_MODE); ByteArrayOutputStream bout = new ByteArrayOutputStream(); OutputStream out = new CipherOutputStream(bout, this.cipher); out.write(this.hexStringToByteArray(pText)); out.flush(); out.close(); return new String(bout.toByteArray()); } /** * Encripta un texto * * @param pText * @return * @throws Exception */ public String encrypt(String pText) throws Exception { if (!this.encrypt) { return pText; } if ((pText == null) || (pText.compareTo("") == 0)) { return pText; } this.init(Cipher.ENCRYPT_MODE); ByteArrayOutputStream bout = new ByteArrayOutputStream(); OutputStream out = new CipherOutputStream(bout, this.cipher); out.write(pText.getBytes()); out.flush(); out.close(); return this.byteArrayToHexString(bout.toByteArray()); } /** * Hexadecimal a String * * @param s * @return */ private byte[] hexStringToByteArray(String s) { byte[] b = new byte[s.length() / 2]; for (int i = 0; i < b.length; i++) { int index = i * 2; int v = Integer.parseInt(s.substring(index, index + 2), 16); b[i] = (byte) v; } return b; } /** * Inicializa la clase * * @param pType * @throws Exception */ private 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); this.cipher = Cipher.getInstance(this.algorithm + "/CBC/PKCS5Padding");// "DESede/CBC/PKCS5Padding");// // ("DES/CBC/PKCS5Padding"); this.cipher.init(pType, this.key, paramSpec); } /** * Habilita la encriptacion * * @return */ public boolean isEncrypt() { return this.encrypt; } /** * Obtiene las transacciones a enctyptar * * @return */ public boolean isEncryptTransaction(String subsystem, String transaction) { boolean encryptTransaction = false; try { String trn = this.properties.getStringValue(subsystem); String[] transacciones = trn.split(","); for (String transaccione : transacciones) { if (transaccione.equals(transaction)) { encryptTransaction = true; } } } catch (Exception ex) { encryptTransaction = false; } return encryptTransaction; } /** * Habilita la encriptacion en Web * * @return */ public boolean isWebencrypt() { return this.webencrypt; } }