1 package com.favorites.utils;
  2 
  3 import javax.crypto.Cipher;
  4 import javax.crypto.SecretKey;
  5 import javax.crypto.SecretKeyFactory;
  6 import javax.crypto.spec.DESedeKeySpec;
  7 import javax.crypto.spec.IvParameterSpec;
  8 import javax.crypto.spec.SecretKeySpec;
  9 import java.security.Key;
 10 
 11 public class Des3EncryptionUtil {
 12     public static final String CHAR_ENCODING = "UTF-8";
 13 
 14     public static byte[] encode(byte[] key, byte[] data) throws Exception {
 15         final String Algorithm = "DESede";
 16 
 17         SecretKey deskey = new SecretKeySpec(key, Algorithm);
 18 
 19         Cipher c1 = Cipher.getInstance(Algorithm);
 20         c1.init(Cipher.ENCRYPT_MODE, deskey);
 21         return c1.doFinal(data);
 22     }
 23 
 24     public static byte[] decode(byte[] key, byte[] value) throws Exception {
 25         final String Algorithm = "DESede";
 26 
 27         SecretKey deskey = new SecretKeySpec(key, Algorithm);
 28 
 29         Cipher c1 = Cipher.getInstance(Algorithm);
 30         c1.init(Cipher.DECRYPT_MODE, deskey);
 31         return c1.doFinal(value);
 32     }
 33 
 34     public static String encode(String key, String data) {
 35         try {
 36             byte[] keyByte = key.getBytes(CHAR_ENCODING);
 37             byte[] dataByte = data.getBytes(CHAR_ENCODING);
 38             byte[] valueByte = encode(keyByte, dataByte);
 39             String value = new String(Base64.encode(valueByte), CHAR_ENCODING);
 40             return value;
 41         } catch (Exception e) {
 42             e.printStackTrace();
 43             return null;
 44         }
 45     }
 46 
 47     public static String decode(String key, String value) {
 48         try {
 49             byte[] keyByte = key.getBytes(CHAR_ENCODING);
 50             byte[] valueByte = Base64.decode(value.getBytes(CHAR_ENCODING));
 51             byte[] dataByte = decode(keyByte,valueByte);
 52             String data = new String(dataByte, CHAR_ENCODING);
 53             return data;
 54         } catch (Exception e) {
 55             e.printStackTrace();
 56             return null;
 57         }
 58     }
 59 
 60     public static String encryptToHex(String key, String data) {
 61         try {
 62             byte[] keyByte = key.getBytes(CHAR_ENCODING);
 63             byte[] dataByte = data.getBytes(CHAR_ENCODING);
 64             byte[] valueByte = encode(keyByte, dataByte);
 65             String value = toHex(valueByte);
 66             return value;
 67         } catch (Exception e) {
 68             e.printStackTrace();
 69             return null;
 70         }
 71     }
 72 
 73     public static String decryptFromHex(String key, String value) {
 74         try {
 75             byte[] keyByte = key.getBytes(CHAR_ENCODING);
 76             byte[] valueByte = fromHex(value);
 77             byte[] dataByte = decode(keyByte,valueByte);
 78             String data = new String(dataByte, CHAR_ENCODING);
 79             return data;
 80         } catch (Exception e) {
 81             e.printStackTrace();
 82             return null;
 83         }
 84     }
 85 
 86     public static String udpEncrypt(String key, String data) {
 87         try {
 88             Key k = updGenerateKey(key);
 89             IvParameterSpec IVSpec = new IvParameterSpec(new byte[8]);
 90             Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
 91             c.init(1, k, ((IVSpec)));
 92             byte output[] = c.doFinal(data.getBytes("UTF-8"));
 93             return new String(Base64.encode(output), "UTF-8");
 94         } catch (Exception e) {
 95             e.printStackTrace();
 96             return null;
 97         }
 98     }
 99 
100     public static Key updGenerateKey(String key) {
101         try {
102             DESedeKeySpec KeySpec = new DESedeKeySpec(UdpHexDecode(key));
103             SecretKeyFactory KeyFactory = SecretKeyFactory
104                     .getInstance("DESede");
105             Key k = ((KeyFactory.generateSecret(((KeySpec)))));
106             return k;
107         } catch (Exception e) {
108             e.printStackTrace();
109             return null;
110         }
111     }
112 
113     public static String udpDecrypt(String key, String data) {
114         try {
115             byte[] input = Base64.decode(data.getBytes("UTF-8"));
116             Key k = updGenerateKey(key);
117             IvParameterSpec IVSpec = new IvParameterSpec(new byte[8]);
118             Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
119             c.init(2, k, ((IVSpec)));
120             byte output[] = c.doFinal(input);
121             return new String(output, "UTF-8");
122         } catch (Exception e) {
123             e.printStackTrace();
124             return null;
125         }
126     }
127 
128     public static byte[] UdpHexDecode(String s) {
129         byte abyte0[] = new byte[s.length() / 2];
130         String s1 = s.toLowerCase();
131         for (int i = 0; i < s1.length(); i += 2) {
132             char c = s1.charAt(i);
133             char c1 = s1.charAt(i + 1);
134             int j = i / 2;
135             if (c < 'a')
136                 abyte0[j] = (byte) (c - 48 << 4);
137             else
138                 abyte0[j] = (byte) ((c - 97) + 10 << 4);
139             if (c1 < 'a')
140                 abyte0[j] += (byte) (c1 - 48);
141             else
142                 abyte0[j] += (byte) ((c1 - 97) + 10);
143         }
144         return abyte0;
145     }
146 
147     public static String toHex(byte input[]) {
148         if (input == null)
149             return null;
150         StringBuffer output = new StringBuffer(input.length * 2);
151         for (int i = 0; i < input.length; i++) {
152             int current = input[i] & 0xff;
153             if (current < 16)
154                 output.append("0");
155             output.append(Integer.toString(current, 16));
156         }
157 
158         return output.toString();
159     }
160     
161     public static byte[] fromHex(String input) {
162         if (input == null)
163             return null;
164         byte output[] = new byte[input.length() / 2];
165         for (int i = 0; i < output.length; i++)
166             output[i] = (byte) Integer.parseInt(
167                     input.substring(i * 2, (i + 1) * 2), 16);
168 
169         return output;
170     }
171 }