Repository navigation
Expand file tree
/
Copy pathPubnubCrypto.java
More file actions
251 lines (221 loc) · 7.74 KB
/
Copy pathPubnubCrypto.java
File metadata and controls
251 lines (221 loc) · 7.74 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.pubnub.api;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.json.*;
class PubnubCrypto extends PubnubCryptoCore {
public PubnubCrypto(String CIPHER_KEY) {
super(CIPHER_KEY);
}
public PubnubCrypto(String CIPHER_KEY, String IV) {
super(CIPHER_KEY, IV);
}
/**
* Encrypt
*
* @param JSONObject Message to encrypt
* @return JSONObject as Encrypted message
*/
@SuppressWarnings("unchecked")
public JSONObject encrypt31(JSONObject message) {
try {
JSONObject message_encrypted = new JSONObject();
Iterator<String> it = message.keys();
while(it.hasNext()) {
String key = it.next();
String val = message.getString(key);
message_encrypted.put(key, encrypt31(val));
}
return message_encrypted;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Decrypt
*
* @param JSONObject Encrypted message
* @return JSONObject Message decrypted
*/
@SuppressWarnings("unchecked")
public JSONObject decrypt31(JSONObject message_encrypted) {
try {
JSONObject message_decrypted = new JSONObject();
Iterator<String> it = message_encrypted.keys();
while(it.hasNext()) {
String key = it.next();
String encrypted_str = message_encrypted.getString(key);
String decrypted_str = decrypt31(encrypted_str);
message_decrypted.put(key, decrypted_str);
}
return message_decrypted;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
/**
* Encrypt JSONArray
*
* @param JSONArray - Encrypted JSONArray
* @return JSONArray - Decrypted JSONArray
*/
public JSONArray encryptJSONArray31(JSONArray jsona_arry) {
try {
JSONArray jsona_decrypted = new JSONArray();
for (int i = 0; i < jsona_arry.length(); i++) {
Object o = jsona_arry.get(i);
if(o != null) {
if(o instanceof JSONObject) {
jsona_decrypted.put(i, encrypt31((JSONObject)o));
} else if(o instanceof JSONArray) {
jsona_decrypted.put(i, encryptJSONArray31((JSONArray)o));
} else if(o instanceof String) {
jsona_decrypted.put(i, encrypt31(o.toString()));
}
}
}
return jsona_decrypted;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
/**
* Decrypt JSONArray
*
* @param JSONArray - Encrypted JSONArray
* @return JSONArray - Decrypted JSONArray
*/
public JSONArray decryptJSONArray31(JSONArray jsona_encrypted) {
try {
JSONArray jsona_decrypted = new JSONArray();
for (int i = 0; i < jsona_encrypted.length(); i++) {
Object o = jsona_encrypted.get(i);
if(o != null) {
if(o instanceof JSONObject) {
jsona_decrypted.put(i, decrypt31((JSONObject)o));
} else if(o instanceof JSONArray) {
jsona_decrypted.put(i, decryptJSONArray31((JSONArray)o));
} else if(o instanceof String) {
jsona_decrypted.put(i, decrypt31(o.toString()));
}
}
}
return jsona_decrypted;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
/**
* Encrypt
*
* @param String plain text to encrypt
* @return String cipher text
* @throws Exception
*/
public String encrypt31(String plain_text) throws Exception {
byte[] out = transform(true, plain_text.getBytes());
return new String(Base64Encoder.encode(out));
}
/**
* Decrypt
*
* @param String cipherText
* @return String
* @throws Exception
*/
public String decrypt31(String cipher_text) throws Exception {
byte[] out = transform(false, Base64Encoder.decode(cipher_text));
return new String(out).trim();
}
/**
* AES Encryption
*
* @param boolean encrypt_or_decrypt ENCRYPT/DECRYPT mode
* @param ByteArray input_bytes
* @return ByteArray
* @throws Exception
*/
private byte[] transform(boolean encrypt_or_decrypt, byte[] input_bytes) throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] iv_bytes = "0123456789012345".getBytes();
byte[] key_bytes = md5(this.CIPHER_KEY);
SecretKeySpec key = new SecretKeySpec(key_bytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv_bytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
if(encrypt_or_decrypt) {
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
ByteArrayInputStream b_in = new ByteArrayInputStream(input_bytes);
CipherInputStream c_in = new CipherInputStream(b_in, cipher);
int ch;
while ((ch = c_in.read()) >= 0) {
output.write(ch);
}
} else {
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
CipherOutputStream c_out = new CipherOutputStream(output, cipher);
c_out.write(input_bytes);
c_out.close();
}
return output.toByteArray();
}
/**
* Sign Message
*
* @param String input
* @return String as HashText
*/
public static String getHMacSHA256(String secret_key, String input) {
try {
Key KEY = new SecretKeySpec(input.getBytes("UTF-8"), "HmacSHA256");
Mac sha256_HMAC = Mac.getInstance("HMACSHA256");
sha256_HMAC.init(KEY);
byte[] mac_data = sha256_HMAC.doFinal(secret_key.getBytes());
BigInteger number = new BigInteger(1, mac_data);
String hashtext = number.toString(16);
return hashtext;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Get MD5
* @param string
* @return
*/
public static byte[] md5(String string) {
byte[] hash;
try {
hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 should be supported!", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 should be supported!", e);
}
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hexStringToByteArray(hex.toString());
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}