· 7 years ago · Jun 16, 2018, 01:46 PM
1import org.bouncycastle.crypto.digests.SHA256Digest;
2import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator;
3import org.bouncycastle.crypto.params.KeyParameter;
4
5import javax.crypto.Cipher;
6import javax.crypto.SecretKey;
7import javax.crypto.spec.IvParameterSpec;
8import javax.crypto.spec.SecretKeySpec;
9import java.nio.charset.Charset;
10import java.security.GeneralSecurityException;
11import java.security.spec.KeySpec;
12
13
14public class SimpleEncryptor {
15
16 public static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
17
18 private static final Charset characterSetUtf8 = Charset.forName("UTF-8");
19
20 private static final Charset characterSetAscii = Charset.forName("US-ASCII");
21
22 private static final byte[] IV = {10, 2, 3, -4, 20, 23, 47, -38, 27, -22, 14, -20, -22, 37, 36, 54};
23 private static final byte[] KeyValue = new byte[]{82, 26, -72, 65, -36, 122, -35, 0, -87, 41 - 39, 42, 27, 44, 6 - 97, 86, -36, 124, 115, 82, -97, 115, -85, 40, 107, -17, 120, -10, -107, 13, -48};
24 private Cipher encryptor;
25 private Cipher decryptor;
26
27 public SimpleEncryptor(byte[] salt) {
28 this(DEFAULT_CIPHER_ALGORITHM, IV, KeyValue, salt);
29 }
30
31 public SimpleEncryptor(byte[] IV, byte[] keyValue, byte[] salt) {
32 this(DEFAULT_CIPHER_ALGORITHM, IV, keyValue, salt);
33 }
34
35 public SimpleEncryptor(String cipherAlgorithm, byte[] IV, byte[] keyValue, byte[] salt) {
36 try {
37
38
39 //PBKDF2WithHmacSHA256
40 final PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
41 gen.init(keyValue, salt, 4096);
42 final byte[] dk = ((KeyParameter) gen.generateDerivedParameters(256)).getKey();
43
44 SecretKey secret = new SecretKeySpec(dk, "AES");
45 encryptor = Cipher.getInstance(cipherAlgorithm);
46 encryptor.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(IV));
47 decryptor = Cipher.getInstance(cipherAlgorithm);
48 decryptor.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(IV));
49 } catch (GeneralSecurityException e) {
50 throw new RuntimeException(e);
51 }
52 }
53
54 public byte[] encrypt(byte[] source) {
55 if (source == null) {
56 return null;
57 }
58 try {
59 return encryptor.doFinal(source);
60 } catch (Exception e) {
61 e.printStackTrace();
62 return null;
63 }
64 }
65
66 public byte[] decrypt(byte[] source) {
67 if (source == null) {
68 return null;
69 }
70 try {
71 return decryptor.doFinal(source);
72 } catch (Exception e) {
73 e.printStackTrace();
74 return null;
75 }
76 }
77
78 public String encrypt(String source) {
79 if (source == null) {
80 return null;
81 }
82 try {
83 byte[] payload = encrypt(source.getBytes(characterSetAscii));
84 if (payload == null) {
85 return null;
86 }
87 return new String(Base64.encode(payload));
88 } catch (Exception e) {
89 e.printStackTrace();
90 return null;
91 }
92 }
93
94 public String decrypt(String source) {
95 if (source == null) {
96 return null;
97 }
98 try {
99
100 byte[] payload = decrypt(Base64.decode(source));
101 if (payload == null) {
102 return null;
103 }
104 return new String(payload, characterSetUtf8);
105 } catch (Exception e) {
106 e.printStackTrace();
107 return null;
108 }
109 }
110
111}