· 9 years ago · Dec 28, 2016, 06:05 PM
1package iss.encryptionUtils;
2
3import java.util.Scanner;
4import javax.crypto.Cipher;
5import javax.crypto.SecretKey;
6import javax.crypto.spec.SecretKeySpec;
7
8public class AESEncryptor {
9
10 public static final String ALGORITHM_AES="AES";
11
12 public static byte[] encryptByteArray(byte [] query, String sharedKey) {
13 byte[] result = null;
14 try {
15 SecretKeySpec secretKeySpec = new SecretKeySpec(sharedKey.getBytes(), ALGORITHM_AES);
16 Cipher cipher = Cipher.getInstance(ALGORITHM_AES);
17 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
18 result = cipher.doFinal(query);
19
20 } catch (Exception e) {
21 e.printStackTrace();
22 }
23
24 return result;
25
26 }
27
28 public static byte[] encryptByteArray(byte [] query, SecretKey sessionKey) {
29 byte[] result = null;
30 try {
31
32 Cipher cipher = Cipher.getInstance(ALGORITHM_AES);
33 cipher.init(Cipher.ENCRYPT_MODE, sessionKey);
34 result = cipher.doFinal(query);
35
36 } catch (Exception e) {
37 e.printStackTrace();
38 }
39
40 return result;
41
42 }
43 public static byte [] decryptByteArray(byte [] query, String sharedKey) {
44 byte[] result = null;
45 try {
46 SecretKeySpec secretKeySpec = new SecretKeySpec(sharedKey.getBytes(), ALGORITHM_AES);
47 Cipher cipher = Cipher.getInstance(ALGORITHM_AES);
48 cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
49 result = cipher.doFinal(query);
50
51
52 } catch (Exception e) {
53 e.printStackTrace();
54 }
55
56 return result;
57
58 }
59 public static byte [] decryptByteArray(byte [] query, SecretKey sessionKey) {
60 byte[] result = null;
61 try {
62// SecretKeySpec secretKeySpec = new SecretKeySpec(sharedKey.getBytes(), ALGORITHM_AES);
63 Cipher cipher = Cipher.getInstance(ALGORITHM_AES);
64 cipher.init(Cipher.DECRYPT_MODE, sessionKey);
65 result = cipher.doFinal(query);
66
67
68 } catch (Exception e) {
69 e.printStackTrace();
70 }
71
72 return result;
73
74 }
75
76
77}