· 9 years ago · Nov 07, 2016, 11:28 AM
1package uk.co.egress.software.egressfm.utils;
2
3import sun.misc.BASE64Decoder;
4import sun.misc.BASE64Encoder;
5
6import javax.crypto.Cipher;
7import javax.crypto.SecretKey;
8import javax.crypto.SecretKeyFactory;
9import javax.crypto.spec.DESedeKeySpec;
10import java.security.spec.KeySpec;
11
12/**
13 * User: martyn
14 * Date: 01/07/2011
15 * Time: 05:54
16 */
17public class DESedeEncryption {
18
19 private static final String UNICODE_FORMAT = "UTF8";
20
21 public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
22
23 private Cipher cipher;
24
25 byte[] keyAsBytes;
26
27 SecretKey key;
28
29 public DESedeEncryption() throws Exception {
30 String myEncryptionKey = "ThisIsSecretEncryptionKey";
31 String myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
32 keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
33 KeySpec myKeySpec = new DESedeKeySpec(keyAsBytes);
34 SecretKeyFactory mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
35 cipher = Cipher.getInstance(myEncryptionScheme);
36 key = mySecretKeyFactory.generateSecret(myKeySpec);
37 }
38
39 /**
40 * Method To Encrypt The String
41 *
42 * @param unencryptedString value to encrypt
43 * @return encrypted string value
44 */
45 public String encrypt(String unencryptedString) {
46 String encryptedString = null;
47 try {
48 cipher.init(Cipher.ENCRYPT_MODE, key);
49 byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
50 byte[] encryptedText = cipher.doFinal(plainText);
51 BASE64Encoder base64encoder = new BASE64Encoder();
52 encryptedString = base64encoder.encode(encryptedText);
53 } catch (Exception e) {
54 e.printStackTrace();
55 }
56 return encryptedString;
57 }
58
59 /**
60 * Method To Decrypt An Encrypted String
61 *
62 * @param encryptedString String to decipher
63 * @return String decrypted String value.
64 */
65 public String decrypt(String encryptedString) {
66 String decryptedText = null;
67 try {
68 cipher.init(Cipher.DECRYPT_MODE, key);
69 BASE64Decoder base64decoder = new BASE64Decoder();
70 byte[] encryptedText = base64decoder.decodeBuffer(encryptedString);
71 byte[] plainText = cipher.doFinal(encryptedText);
72 decryptedText = bytes2String(plainText);
73 } catch (Exception e) {
74 e.printStackTrace();
75 }
76 return decryptedText;
77 }
78
79 /**
80 * Returns String From An Array Of Bytes
81 *
82 * @param bytes bytes passed in from string value
83 * @return String converted
84 */
85 private static String bytes2String(byte[] bytes) {
86 StringBuilder stringBuffer = new StringBuilder();
87 for (byte aByte : bytes) {
88 stringBuffer.append((char) aByte);
89 }
90 return stringBuffer.toString();
91 }
92
93 /**
94 * Testing The DESede Encryption And Decryption Technique
95 *
96 * @param args if any
97 * @throws Exception e
98 */
99 public static void main(String args[]) throws Exception {
100 DESedeEncryption myEncryptor = new DESedeEncryption();
101 String encrypted = myEncryptor.encrypt("mcsklj688");
102 String nomalised = myEncryptor.decrypt(encrypted);
103 System.out.println("OLD: " + "dXeiJjUPR6bnP2bYMs9ltw==");
104 System.out.println("NEW: " + encrypted);
105 System.out.println("Decrypted: " + nomalised);
106
107 }
108}