· 6 years ago · Sep 16, 2019, 04:36 PM
1import java.io.UnsupportedEncodingException;
2import java.security.InvalidKeyException;
3import java.security.MessageDigest;
4import java.security.NoSuchAlgorithmException;
5import java.util.Arrays;
6import javax.crypto.BadPaddingException;
7import javax.crypto.Cipher;
8import javax.crypto.IllegalBlockSizeException;
9import javax.crypto.NoSuchPaddingException;
10import javax.crypto.SecretKey;
11import javax.crypto.spec.SecretKeySpec;
12import org.apache.commons.codec.binary.Base64;
13
14/**
15 *
16 * @author josepholaoye
17 */
18public class TripleDES {
19
20 String key;
21
22 public TripleDES(String myEncryptionKey) {
23 key = myEncryptionKey;
24 }
25
26 /**
27 * Method To Encrypt The String
28 *
29 * @param unencryptedString
30 * @return encrpted string
31 * @throws java.security.NoSuchAlgorithmException
32 * @throws java.io.UnsupportedEncodingException
33 * @throws javax.crypto.NoSuchPaddingException
34 * @throws java.security.InvalidKeyException
35 * @throws javax.crypto.IllegalBlockSizeException
36 * @throws javax.crypto.BadPaddingException
37 */
38 public String harden(String unencryptedString) throws NoSuchAlgorithmException, UnsupportedEncodingException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
39 MessageDigest md = MessageDigest.getInstance("md5");
40 byte[] digestOfPassword = md.digest(key.getBytes("utf-8"));
41 byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
42
43 for (int j = 0, k = 16; j < 8;) {
44 keyBytes[k++] = keyBytes[j++];
45 }
46
47 SecretKey secretKey = new SecretKeySpec(keyBytes, "DESede");
48 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
49 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
50
51 byte[] plainTextBytes = unencryptedString.getBytes("utf-8");
52 byte[] buf = cipher.doFinal(plainTextBytes);
53 byte[] base64Bytes = Base64.encodeBase64(buf);
54 String base64EncryptedString = new String(base64Bytes);
55
56 return base64EncryptedString;
57 }
58
59 /**
60 * Method To Decrypt An Ecrypted String
61 *
62 * @param encryptedString
63 * @return
64 * @throws java.io.UnsupportedEncodingException
65 * @throws java.security.NoSuchAlgorithmException
66 * @throws javax.crypto.NoSuchPaddingException
67 * @throws java.security.InvalidKeyException
68 * @throws javax.crypto.IllegalBlockSizeException
69 * @throws javax.crypto.BadPaddingException
70 */
71 public String soften(String encryptedString) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
72 if(encryptedString == null)
73 {
74 return "";
75 }
76 byte[] message = Base64.decodeBase64(encryptedString.getBytes("utf-8"));
77
78 MessageDigest md = MessageDigest.getInstance("MD5");
79 byte[] digestOfPassword = md.digest(key.getBytes("utf-8"));
80 byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
81
82 for (int j = 0, k = 16; j < 8;) {
83 keyBytes[k++] = keyBytes[j++];
84 }
85
86 SecretKey secretKey = new SecretKeySpec(keyBytes, "DESede");
87
88 Cipher decipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
89 decipher.init(Cipher.DECRYPT_MODE, secretKey);
90
91 byte[] plainText = decipher.doFinal(message);
92
93 return new String(plainText, "UTF-8");
94
95 }
96
97}