· 6 years ago · Apr 16, 2019, 10:10 PM
1package com.santander.us.utilities;
2
3import java.security.MessageDigest;
4import java.util.Arrays;
5
6import javax.crypto.Cipher;
7import javax.crypto.SecretKey;
8import javax.crypto.spec.SecretKeySpec;
9
10import org.apache.tomcat.util.codec.binary.Base64;
11
12
13public class Utilities {
14
15
16
17 public static String Encriptar(String texto) {
18
19 String secretKey = "qualityinfosolutions"; //llave para encriptar datos
20 String base64EncryptedString = "";
21
22 try {
23
24 MessageDigest md = MessageDigest.getInstance("MD5");
25 byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
26 byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
27
28 SecretKey key = new SecretKeySpec(keyBytes, "DESede");
29 Cipher cipher = Cipher.getInstance("DESede");
30 cipher.init(Cipher.ENCRYPT_MODE, key);
31
32 byte[] plainTextBytes = texto.getBytes("utf-8");
33 byte[] buf = cipher.doFinal(plainTextBytes);
34 byte[] base64Bytes = Base64.encodeBase64(buf);
35 base64EncryptedString = new String(base64Bytes);
36
37 } catch (Exception ex) {
38 }
39 return base64EncryptedString;
40 }
41
42 public static String Desencriptar(String textoEncriptado) throws Exception {
43
44 String secretKey = "qualityinfosolutions"; //llave para encriptar datos
45 String base64EncryptedString = "";
46
47 try {
48 byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
49 MessageDigest md = MessageDigest.getInstance("MD5");
50 byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
51 byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
52 SecretKey key = new SecretKeySpec(keyBytes, "DESede");
53
54 Cipher decipher = Cipher.getInstance("DESede");
55 decipher.init(Cipher.DECRYPT_MODE, key);
56
57 byte[] plainText = decipher.doFinal(message);
58
59 base64EncryptedString = new String(plainText, "UTF-8");
60
61 } catch (Exception ex) {
62 }
63 return base64EncryptedString;
64 }
65
66
67
68// public static void main(String[] args) {
69//// System.err.println("start");
70// String t = "texto";
71// String e = Encriptar(t);
72// System.err.println(e);
73// try {
74// String d = Desencriptar("1mCujclg07E3MQI7UpBG0g==");
75// System.err.println("here"+d);
76// } catch (Exception e1) {
77// e1.printStackTrace();
78// }
79//
80// }
81
82
83}