-
Notifications
You must be signed in to change notification settings - Fork 31
/
EncryptionHelper.java
120 lines (99 loc) · 2.68 KB
/
EncryptionHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package me.anon.lib.helper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* // TODO: Add class description
*
* @author 7LPdWcaW
* @documentation // TODO Reference flow doc
* @project GrowTracker
*/
public class EncryptionHelper
{
public static SecretKey generateKey(String key)
{
try
{
key = key == null ? "" : key;
int keyLength = 128;
byte[] keyBytes = new byte[keyLength / 8];
Arrays.fill(keyBytes, (byte)0x0);
byte[] passwordBytes = key.getBytes("UTF-8");
int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length;
System.arraycopy(passwordBytes, 0, keyBytes, 0, length);
return new SecretKeySpec(keyBytes, "AES");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return null;
}
public static byte[] encrypt(String key, String input)
{
try
{
SecretKey secretKey = generateKey(key);
input = input == null ? "" : input;
if (secretKey != null)
{
ByteArrayOutputStream fos = new ByteArrayOutputStream(8192);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
byte[] bytes = input.getBytes("UTF-8");
for (byte b : bytes)
{
cos.write(b);
}
cos.flush();
cos.close();
return fos.toByteArray();
}
}
catch (NoSuchAlgorithmException | IOException | InvalidKeyException | NoSuchPaddingException e)
{
e.printStackTrace();
}
return null;
}
public static String decrypt(String key, byte[] input)
{
try
{
SecretKey secretKey = generateKey(key);
if (secretKey != null)
{
ByteArrayInputStream fis = new ByteArrayInputStream(input);
StringBuilder output = new StringBuilder();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int len;
byte[] b = new byte[8192];
while ((len = cis.read(b)) != -1)
{
output.append(new String(b, 0, len, "UTF-8"));
}
cis.close();
return output.toString();
}
}
catch (NoSuchAlgorithmException | NoSuchPaddingException | IOException | InvalidKeyException e)
{
e.printStackTrace();
}
return null;
}
}