Java tutorial
/* * 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 ar.gob.ambiente.servicios.gestionpersonas.entidades.util; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import javax.persistence.Id; import org.apache.commons.codec.binary.Base64; /** * * @author rodriguezn */ public class CriptPass { @Id private Long id; // Cadena para la creacin de contraseas aleatorias. public static String base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@!#$"; /** * Mtodo para crear las contraseas de manera aleatoria al crear un usuario por primera vez * @return */ public static String generar() { int longitud = base.length(); String contrasenia = ""; for (int i = 0; i < 8; i++) { int numero = (int) (Math.random() * (longitud)); String caracter = base.substring(numero, numero + 1); contrasenia = contrasenia + caracter; } return contrasenia; } /** * Mtodo para encriptar las contraseas * @param texto * @return */ public static String encriptar(String texto) { String secretKey = "zorbazorbas"; //llave para encriptar datos String base64EncryptedString = ""; try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); SecretKey key = new SecretKeySpec(keyBytes, "DESede"); Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] plainTextBytes = texto.getBytes("utf-8"); byte[] buf = cipher.doFinal(plainTextBytes); byte[] base64Bytes = Base64.encodeBase64(buf); base64EncryptedString = new String(base64Bytes); } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { System.out.println(ex.getMessage()); } return base64EncryptedString; } /** * Mtodo para desencriptar una contrasea encriptada * @param textoEncriptado * @return * @throws Exception */ public static String desencriptar(String textoEncriptado) throws Exception { String secretKey = "zorbazorbas"; //llave para encriptar datos String base64EncryptedString = ""; try { byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8")); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); SecretKey key = new SecretKeySpec(keyBytes, "DESede"); Cipher decipher = Cipher.getInstance("DESede"); decipher.init(Cipher.DECRYPT_MODE, key); byte[] plainText = decipher.doFinal(message); base64EncryptedString = new String(plainText, "UTF-8"); } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { System.out.println(ex.getMessage()); } return base64EncryptedString; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } }