· 9 years ago · Sep 28, 2016, 10:04 AM
1package uk.co.virginwines.migration;
2
3import com.mantiki.utils.StringUtils;
4import org.springframework.security.crypto.codec.Base64;
5
6import javax.crypto.Cipher;
7import javax.crypto.NoSuchPaddingException;
8import javax.crypto.SecretKey;
9import javax.crypto.SecretKeyFactory;
10import javax.crypto.spec.IvParameterSpec;
11import javax.crypto.spec.PBEKeySpec;
12import javax.crypto.spec.SecretKeySpec;
13import java.security.AlgorithmParameters;
14import java.security.NoSuchAlgorithmException;
15import java.security.spec.InvalidKeySpecException;
16import java.security.spec.KeySpec;
17
18public class VwStringEncrypter {
19
20 private static final String UNICODE = "UTF8";
21
22 /*
23 * Do not change any of the below constants as they are used on Blue Martini
24 * as well.
25 */
26 public static final String ENCRYPT_KEY =
27 "a1dgedbQEbg3we8EEasd3HKj1a1dgedbQEbg3we8EEasd3HKj1vH2HKdwedv208s9a123KJasdaq208s9a123KJMasdasdfasdf12341wasdfasdfasHLHLHasdasdff";
28 public static final String SALT = "qwecasdgvASDF12345ASDGASDGasdfgs";
29 public static final byte[] IV = {28, -26, -34, 61, -53, 51, -120, 49,
30 -112, -48, 96, 109, -28, -86, -86, 45};
31
32 private KeySpec keySpec;
33 private SecretKeyFactory keyFactory;
34 private Cipher cipher;
35 private SecretKey secret;
36
37 public VwStringEncrypter() throws EncryptionException {
38 this(ENCRYPT_KEY, SALT);
39 }
40
41 public VwStringEncrypter(String encryptionKey,
42 String salt) throws EncryptionException {
43 if (encryptionKey == null || encryptionKey.length() == 0) {
44 throw new IllegalArgumentException(
45 "encryption key cannot be null or 0 length");
46 }
47
48 if (encryptionKey.toCharArray().length > 128) {
49 throw new IllegalArgumentException(
50 "encryption key cannot be greater than 128");
51 }
52
53 if (salt == null || salt.length() == 0) {
54 throw new IllegalArgumentException(
55 "salt cannot be null or 0 length");
56 }
57
58 try {
59
60 keySpec = new PBEKeySpec(encryptionKey.toCharArray(),
61 salt.getBytes(), 65536, 128);
62 keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
63 cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
64 SecretKey key = keyFactory.generateSecret(keySpec);
65 secret = new SecretKeySpec(key.getEncoded(), "AES");
66
67 } catch (InvalidKeySpecException | NoSuchAlgorithmException |
68 NoSuchPaddingException e) {
69 throw new EncryptionException(e);
70 }
71 }
72
73 public String encrypt(String unencrypted) throws EncryptionException {
74 if (!StringUtils.hasText(unencrypted))
75 throw new IllegalArgumentException(
76 "The unencrypted string is empty.");
77
78 try {
79 cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(IV));
80 byte[] cleartext = unencrypted.getBytes(UNICODE);
81 byte[] ciphertext = cipher.doFinal(cleartext);
82 return new String(Base64.encode(ciphertext));
83 } catch (Exception e) {
84 throw new EncryptionException(e);
85 }
86 }
87
88 public String decrypt(String encrypted) throws EncryptionException {
89 if (!StringUtils.hasText(encrypted)) throw new IllegalArgumentException(
90 "The encrypted string is empty.");
91 try {
92 cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(IV));
93 byte[] cleartext = Base64.decode(encrypted.getBytes(UNICODE));
94 byte[] ciphertext = cipher.doFinal(cleartext);
95 return new String(ciphertext);
96 } catch (Exception e) {
97 throw new EncryptionException(e);
98 }
99 }
100
101 public static class EncryptionException extends Exception {
102
103 public EncryptionException(Throwable t) {
104 super(t);
105 }
106 }
107}