· 9 years ago · Sep 27, 2016, 10:34 AM
1//
2// Source code recreated from a .class file by IntelliJ IDEA
3// (powered by Fernflower decompiler)
4//
5
6package com.mantiki.utils;
7
8import com.mantiki.utils.StringUtils;
9import java.security.AlgorithmParameters;
10import java.security.NoSuchAlgorithmException;
11import java.security.spec.InvalidKeySpecException;
12import java.security.spec.KeySpec;
13import javax.crypto.Cipher;
14import javax.crypto.NoSuchPaddingException;
15import javax.crypto.SecretKey;
16import javax.crypto.SecretKeyFactory;
17import javax.crypto.spec.IvParameterSpec;
18import javax.crypto.spec.PBEKeySpec;
19import javax.crypto.spec.SecretKeySpec;
20import org.springframework.security.crypto.codec.Base64;
21
22public class StringEncrypter2 {
23 private static final String UNICODE = "UTF8";
24 public static final String ENCRYPT_KEY = "A bear of very little brain ";
25 public static final String SALT = "this is the salt";
26 private KeySpec keySpec;
27 private SecretKeyFactory keyFactory;
28 private Cipher cipher;
29 private SecretKey secret;
30
31 public StringEncrypter2() throws StringEncrypter2.EncryptionException {
32 this("A bear of very little brain ", "this is the salt");
33 }
34
35 public StringEncrypter2(String encryptionKey, String salt) throws StringEncrypter2.EncryptionException {
36 if(encryptionKey != null && encryptionKey.length() != 0) {
37 if(encryptionKey.toCharArray().length > 128) {
38 throw new IllegalArgumentException("encryption key cannot be greater than 128");
39 } else if(salt != null && salt.length() != 0) {
40 try {
41 this.keySpec = new PBEKeySpec(encryptionKey.toCharArray(), salt.getBytes(), 65536, 128);
42 this.keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
43 this.cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
44 SecretKey e = this.keyFactory.generateSecret(this.keySpec);
45 this.secret = new SecretKeySpec(e.getEncoded(), "AES");
46 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException var4) {
47 throw new StringEncrypter2.EncryptionException(var4);
48 }
49 } else {
50 throw new IllegalArgumentException("salt cannot be null or 0 length");
51 }
52 } else {
53 throw new IllegalArgumentException("encryption key cannot be null or 0 length");
54 }
55 }
56
57 public String encrypt(String unencrypted) throws StringEncrypter2.EncryptionException {
58 if(!StringUtils.hasText(unencrypted)) {
59 throw new IllegalArgumentException("The unencrypted string is empty.");
60 } else {
61 try {
62 this.cipher.init(1, this.secret);
63 byte[] e = unencrypted.getBytes("UTF8");
64 byte[] ciphertext = this.cipher.doFinal(e);
65 return new String(Base64.encode(ciphertext));
66 } catch (Exception var4) {
67 throw new StringEncrypter2.EncryptionException(var4);
68 }
69 }
70 }
71
72 public String decrypt(String encrypted) throws StringEncrypter2.EncryptionException {
73 if(!StringUtils.hasText(encrypted)) {
74 throw new IllegalArgumentException("The encrypted string is empty.");
75 } else {
76 try {
77 AlgorithmParameters e = this.cipher.getParameters();
78 byte[] iv = ((IvParameterSpec)e.getParameterSpec(IvParameterSpec.class)).getIV();
79 this.cipher.init(2, this.secret, new IvParameterSpec(iv));
80 byte[] cleartext = Base64.decode(encrypted.getBytes("UTF8"));
81 byte[] ciphertext = this.cipher.doFinal(cleartext);
82 return new String(ciphertext);
83 } catch (Exception var6) {
84 throw new StringEncrypter2.EncryptionException(var6);
85 }
86 }
87 }
88
89 public static class EncryptionException extends Exception {
90 public EncryptionException(Throwable t) {
91 super(t);
92 }
93 }
94}