/
SecurityFacade.java
468 lines (386 loc) · 11.8 KB
/
SecurityFacade.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package com.example.locus.security;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import android.util.Base64;
import android.util.Log;
public class SecurityFacade implements ISecurity{
private static SecurityFacade instance = null ;
private static KeyPairGenerator kpg = null ;
private static KeyPair keyPair = null ;
private static Key publicKey = null ;
private static Key privateKey = null ;
private static int keyLength = 1024 ;
/* Here initialize any variables that will be necessary later */
private SecurityFacade() {
try {
kpg = KeyPairGenerator.getInstance("RSA") ;
kpg.initialize(keyLength) ;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static SecurityFacade getInstance() {
if (instance == null ) {
instance = new SecurityFacade() ;
}
return instance ;
}
private byte[] getByteArray (Object obj) {
byte[] bytes = null;
ByteArrayOutputStream baos;
ObjectOutputStream oos;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.flush();
oos.close();
baos.close();
bytes = baos.toByteArray();
}
catch (IOException e) {
System.out.println("error in toByteArray()");
}
return bytes;
}
private String getSerializedString (Object obj) {
String serializedString = null ;
ByteArrayOutputStream baos;
ObjectOutputStream oos;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.flush();
oos.close();
baos.close();
//serializedString = baos.toString("UTF-8");
serializedString = baos.toString();
}
catch (IOException e) {
System.out.println("error in toByteArray()");
}
return serializedString;
}
/* This will only work for public keys */
private String[] convertKey(Key secKey) {
KeyFactory kFact;
/* These are the components of the Key */
BigInteger modulus = null ;
BigInteger exp = null ;
RSAPublicKeySpec pubKSpec = null ;
String[] keyComponents = new String[2] ;
try {
kFact = KeyFactory.getInstance("RSA/ECB/PKCS1Padding");
} catch (NoSuchAlgorithmException e) {
//e.printStackTrace();
return null ;
}
try {
pubKSpec = kFact.getKeySpec(secKey, RSAPublicKeySpec.class);
} catch (InvalidKeySpecException e) {
return null ;
//e.printStackTrace();
}
modulus = pubKSpec.getModulus() ;
exp = pubKSpec.getPublicExponent() ;
byte[] modulusBArray = getByteArray(modulus);
byte[] expBArray = getByteArray(exp);
String modString = null ;
String expString = null ;
try {
//modString = new String(modulusBArray,"UTF-8");
modString = new String(modulusBArray);
} catch (Exception e) {
//e.printStackTrace();
return null ;
}
try {
//expString = new String(expBArray,"UTF-8");
expString = new String(expBArray);
} catch (Exception e) {
//e.printStackTrace();
return null ;
}
Log.d("# modulus #",modString);
Log.d("# exp #",expString);
keyComponents[0] = modString ;
keyComponents[1] = expString ;
/* Again trying to get the strings using different way */
String mstring = getSerializedString(modulus);
String estring = getSerializedString(exp);
if ( modString.equals(mstring)) {
Log.e("modulus_match","Both are matching");
} else {
Log.e("modulus_match","Both are not matching");
}
if ( modString.equals(mstring)) {
Log.e("exp_match","Both are matching");
} else {
Log.e("exp_match","Both are not matching");
}
keyComponents[0] = mstring ;
keyComponents[1] = estring ;
return keyComponents ;
}
private PublicKey buildKey(String[] keyComps) {
if ( keyComps == null )
return null ;
byte[] modBArray = null ;
byte[] expBArray = null ;
try {
//modBArray = keyComps[0].getBytes("UTF-8") ;
modBArray = keyComps[0].getBytes() ;
} catch (Exception e) {
Log.e("DEBUG","error in modBARray");
// TODO Auto-generated catch block
//e.printStackTrace();
return null ;
}
try {
//expBArray = keyComps[1].getBytes("UTF-8");
expBArray = keyComps[1].getBytes();
} catch (Exception e) {
Log.e("DEBUG","expBArray");
//e.printStackTrace();
return null ;
}
ObjectInputStream mois;
ObjectInputStream eois ;
PublicKey pubKey = null ;
try {
ByteArrayInputStream bais = null ;
bais = new ByteArrayInputStream(modBArray) ;
//mois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(modBArray)));
mois = new ObjectInputStream(bais);
eois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(expBArray)));
BigInteger m = (BigInteger)mois.readObject() ;
BigInteger e = (BigInteger)eois.readObject() ;
RSAPublicKeySpec pubKSpec = new RSAPublicKeySpec(m, e);
KeyFactory kFactory = KeyFactory.getInstance("RSA") ;
pubKey = kFactory.generatePublic(pubKSpec);
mois.close() ;
eois.close() ;
} catch (Exception e ) {
e.printStackTrace();
return null ;
}
return pubKey ;
}
/* This function should return the public key preferably in the String format
* to store in the User object. Here this will return a string array that contains the
* modulus and exponent of the key. i can't concatenate these and return the concatenated because
* these strings formed from the byte array can contain any character and hence none of the characters
* can be used as seperation delimeters
*/
public String generate_keypair() {
if ( kpg == null )
return null ;
keyPair = kpg.generateKeyPair() ;
if ( keyPair == null)
return null ;
publicKey = keyPair.getPublic() ;
privateKey = keyPair.getPrivate() ;
if ( (publicKey == null) || (privateKey == null)) {
return null ;
}
return Base64.encodeToString(publicKey.getEncoded(),0);
}
public String encrypt_data(String data, String pKey) {
/* First generate the Public Key object from the public key in string format */
byte[] publicKeyBytes = Base64.decode(pKey,0);
PublicKey pubKey = null ;
KeyFactory keyFactory = null;
Cipher cipher = null;
try {
keyFactory = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null ;
}
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
try {
pubKey = keyFactory.generatePublic(publicKeySpec);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
return null ;
}
/* Here got the public key .. Now use it to encrypt the given data */
try {
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null ;
} catch (NoSuchPaddingException e) {
e.printStackTrace();
return null ;
}
try {
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
} catch (InvalidKeyException e) {
e.printStackTrace();
return null ;
}
InputStream fis = null;
try {
fis = new ByteArrayInputStream(data.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ByteArrayOutputStream fos = null ;
fos = new ByteArrayOutputStream();
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
byte[] block = new byte[32];
int i;
try {
while ((i = fis.read(block)) != -1) {
try {
cos.write(block, 0, i);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
cos.close();
} catch (IOException e) {
e.printStackTrace();
return null ;
}
/* Get the encoded data in a byte array */
byte[] encBArray = fos.toByteArray() ;
String encrypted_data = null;
/* converting the encrypted data into string using Base64 encoding since
* we will be transferring this data over the network.
*/
encrypted_data = Base64.encodeToString(encBArray, 0);
Log.e("Encrypted Message",encrypted_data);
/* WORKING CODE for the ISO-8859-1 encoding .. This encoding works
* but other encodings like UTF-8 etc fails here
try {
encrypted_data = new String(encBArray,"ISO-8859-1");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Log.e("Encrypted Message",encrypted_data);
byte[] encodedBArray = null ;
try {
encodedBArray = encrypted_data.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
*/
return encrypted_data ;
}
public String decrypt_data(String enc_data) {
Cipher cipher = null;
InputStream fis = null;
byte[] encodedBArray = null ;
ByteArrayOutputStream fos = null ;
byte[] block = new byte[32];
int i;
encodedBArray = Base64.decode(enc_data, 0);
try {
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
try {
cipher.init(Cipher.DECRYPT_MODE, privateKey);
} catch (InvalidKeyException e1) {
e1.printStackTrace();
}
fis = new ByteArrayInputStream(encodedBArray) ;
CipherInputStream cis = new CipherInputStream(fis, cipher);
try {
fos = new ByteArrayOutputStream();
} catch (Exception e) {
e.printStackTrace();
return null ;
}
try {
while ((i = cis.read(block)) != -1) {
fos.write(block, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
return null ;
}
String decrypted_text = fos.toString() ;
/* Now get the decrypted byte array and try to print the string */
System.out.println("The decrypted text is : " + decrypted_text );
return decrypted_text ;
}
/*
public String[] generate_keypair() {
if ( kpg == null )
return null ;
keyPair = kpg.generateKeyPair() ;
if ( keyPair == null)
return null ;
publicKey = keyPair.getPublic() ;
privateKey = keyPair.getPrivate() ;
if ( (publicKey == null) || (privateKey == null)) {
return null ;
}
return convertKey(publicKey);
}
*/
/* Given a pub key and the data this function will return the encrypted version of the data */
/*
public String encrypt_data(String data, String[] pubKeyComps) {
PublicKey pKey = buildKey(pubKeyComps);
if ( pKey == null ) {
Log.e("recreated pKey","Error occured");
}
Cipher cipher = null ;
byte[] encryptedBytes = null ;
String encString = null ;
try {
cipher = Cipher.getInstance("RSA") ;
cipher.init(Cipher.ENCRYPT_MODE,pKey);
//encryptedBytes = cipher.doFinal(data.getBytes("UTF-8"));
//encString = new String(encryptedBytes,"UTF-8") ;
encryptedBytes = cipher.doFinal(data.getBytes());
encString = new String(encryptedBytes) ;
} catch (Exception e) {
return null ;
}
return encString;
}
*/
}