1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.util;
17
18 import java.io.UnsupportedEncodingException;
19 import java.security.spec.KeySpec;
20
21 import javax.crypto.Cipher;
22 import javax.crypto.SecretKey;
23 import javax.crypto.SecretKeyFactory;
24 import javax.crypto.spec.DESedeKeySpec;
25
26 import org.acegisecurity.AcegiSecurityException;
27 import org.apache.commons.codec.binary.Base64;
28 import org.springframework.util.Assert;
29
30
31
32
33
34
35
36
37
38
39
40
41 public final class EncryptionUtils {
42
43
44
45
46 private EncryptionUtils() {}
47
48
49
50
51
52
53
54
55 public static byte[] stringToByteArray(String input) {
56 Assert.hasLength(input, "Input required");
57 try {
58 return input.getBytes("UTF-8");
59 } catch (UnsupportedEncodingException fallbackToDefault) {
60 return input.getBytes();
61 }
62 }
63
64
65
66
67
68
69
70
71 public static String byteArrayToString(byte[] byteArray) {
72 Assert.notNull(byteArray, "ByteArray required");
73 Assert.isTrue(byteArray.length > 0, "ByteArray cannot be empty");
74 try {
75 return new String(byteArray, "UTF8");
76 } catch (final UnsupportedEncodingException e) {
77 return new String(byteArray);
78 }
79 }
80
81 private static byte[] cipher(String key, byte[] passedBytes, int cipherMode) throws EncryptionException {
82 try {
83 final KeySpec keySpec = new DESedeKeySpec(stringToByteArray(key));
84 final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
85 final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
86 final SecretKey secretKey = keyFactory.generateSecret(keySpec);
87 cipher.init(cipherMode, secretKey);
88 return cipher.doFinal(passedBytes);
89 } catch (final Exception e) {
90 throw new EncryptionException(e.getMessage(), e);
91 }
92 }
93
94
95
96
97
98
99
100
101
102 public static String encrypt(String key, String inputString) throws EncryptionException {
103 isValidKey(key);
104 final byte[] cipherText = cipher(key, stringToByteArray(inputString), Cipher.ENCRYPT_MODE);
105 return byteArrayToString(Base64.encodeBase64(cipherText));
106 }
107
108
109
110
111
112
113
114
115
116 public static byte[] encrypt(String key, byte[] inputBytes) throws EncryptionException {
117 isValidKey(key);
118 return Base64.encodeBase64(cipher(key, inputBytes, Cipher.ENCRYPT_MODE));
119 }
120
121
122
123
124
125
126
127
128
129 public static String decrypt(String key, String inputString) throws EncryptionException {
130 Assert.hasText(key, "A key is required to attempt decryption");
131 final byte[] cipherText = cipher(key, Base64.decodeBase64(stringToByteArray(inputString)), Cipher.DECRYPT_MODE);
132 return byteArrayToString(cipherText);
133 }
134
135
136
137
138
139
140
141
142
143 public static byte[] decrypt(String key, byte[] inputBytes) throws EncryptionException {
144 Assert.hasText(key, "A key is required to attempt decryption");
145 return cipher(key, Base64.decodeBase64(inputBytes), Cipher.DECRYPT_MODE);
146 }
147
148 private static void isValidKey(String key) {
149 Assert.hasText(key, "A key to perform the encryption is required");
150 Assert.isTrue(key.length() >= 24, "Key must be at least 24 characters long");
151 }
152
153 public static class EncryptionException extends AcegiSecurityException {
154 private static final long serialVersionUID = 1L;
155
156 public EncryptionException(String message, Throwable t) {
157 super(message, t);
158 }
159
160 public EncryptionException(String message) {
161 super(message);
162 }
163 }
164 }