ec.edu.uce.medicina.seguimiento.util.EncryptionUtility.java Source code

Java tutorial

Introduction

Here is the source code for ec.edu.uce.medicina.seguimiento.util.EncryptionUtility.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ec.edu.uce.medicina.seguimiento.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import static org.apache.commons.codec.binary.Base64.decodeBase64;
import static org.apache.commons.codec.binary.Base64.encodeBase64;

/**
 * <b>
 * Clase con la descripcin con mtodos de encriptacin y desencriptacin de una
 * contrasea.
 * </b>
 *
 * @author MORETA DIANA
 * @version 1.0, 1/08/2016
 * @since JDK1.8
 */
public class EncryptionUtility {

    // Definicin del tipo de algoritmo a utilizar (AES, DES, RSA)
    private final static String alg = "AES";
    // Definicin del modo de cifrado a utilizar
    private final static String cI = "AES/CBC/PKCS5Padding";

    /**
     *Mtodo que permite la encriptacin de la contrasea.
     * @param cleartext
     * @return
     * @throws java.lang.Exception
     */
    public static String encrypt(String cleartext) throws Exception {
        String key = "02AE31B79CCCB2A3"; //llave
        String iv = "0123456789ABCDEF";
        Cipher cipher = Cipher.getInstance(cI);
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
        byte[] encrypted = cipher.doFinal(cleartext.getBytes());
        return new String(encodeBase64(encrypted));
    }

    /**
     *Mtodo que permite la desencriptacin de la contrasea
     * @param encrypted
     * @return
     * @throws Exception
     */
    public static String decrypt(String encrypted) throws Exception {
        String key = "02AE31B79CCCB2A3"; //llave
        String iv = "0123456789ABCDEF";
        Cipher cipher = Cipher.getInstance(cI);
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        byte[] enc = decodeBase64(encrypted);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
        byte[] decrypted = cipher.doFinal(enc);
        return new String(decrypted);
    }
}