· 9 years ago · Nov 18, 2016, 09:46 AM
1private String encryptPacket(String packet, String pubKey)
2{
3 PublicKey clientPub = KeyFunctions.stringToKey(pubKey);
4 String aesEncryptedData = null;
5 byte[] rsaEncryptedData = null;
6 String temp = null;
7
8 try
9 {
10 // AES
11 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
12 final SecretKeySpec secretKey = new SecretKeySpec(Constants.KEY.getBytes(), "AES");
13 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
14 aesEncryptedData = Base64.encodeToString(cipher.doFinal(packet.getBytes()), Base64.NO_PADDING|Base64.NO_WRAP); //base64 the aes
15
16 // RSA
17 Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
18 c.init(Cipher.ENCRYPT_MODE, clientPub);
19 rsaEncryptedData = c.doFinal(aesEncryptedData.getBytes());
20 temp = Base64.encodeToString(rsaEncryptedData, Base64.NO_PADDING|Base64.NO_WRAP); // base 64 the rsa
21 Log.d("ENC SEND", temp);
22 } catch (Exception e)
23 {
24 e.printStackTrace();
25 }
26 return temp;
27}
28
29public String decryptPacket(String encryptedData, Context context)
30{
31 // get the keys
32 PrivateKey pri = KeyFunctions.getPrivateKey(context);
33 byte[] packet = null;
34 byte[] decrypted = null;
35 String temp = null;
36
37 try
38 {
39 //RSA
40 Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
41 c.init(Cipher.DECRYPT_MODE, pri);
42 byte[] rsaTempArray = Base64.decode(encryptedData, Base64.NO_PADDING|Base64.NO_WRAP);
43 packet = c.doFinal(rsaTempArray);
44
45 // AES
46 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
47 final SecretKeySpec secretKey = new SecretKeySpec(Constants.KEY.getBytes(), "AES");
48 cipher.init(Cipher.DECRYPT_MODE, secretKey);
49 final String decryptedString = new String(cipher.doFinal(Base64.decode(packet, Base64.NO_PADDING|Base64.NO_WRAP)));
50 temp = decryptedString;
51 } catch (Exception e)
52 {
53 e.printStackTrace();
54 }
55 Log.d("ENC REC", temp);
56 return temp;
57}
58
59private byte[] encryptPacket(Packet packet, String pubKey)
60{
61 PublicKey clientPub = KeyFunctions.stringToKey(pubKey);
62 byte[] aesEncryptedData = null;
63 byte[] rsaEncryptedData = null;
64 byte[] temp = null;
65
66 Log.d("START", "==========ENCRYPT==========");
67 try
68 {
69 // AES
70 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
71 final SecretKeySpec secretKey = new SecretKeySpec(Constants.KEY.getBytes(), "AES");
72 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
73 byte aesTempArray[] = cipher.doFinal(packet.getBytes());
74 Log.d("ENC AES TEMP", new String(aesTempArray, "UTF-8"));
75 aesEncryptedData = Base64.encode(aesTempArray, Base64.NO_PADDING | Base64.NO_WRAP); //base64 the aes
76 Log.d("ENC AES ENCR", new String(aesEncryptedData, "UTF-8"));
77
78 // RSA
79 Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
80 c.init(Cipher.ENCRYPT_MODE, clientPub);
81 rsaEncryptedData = c.doFinal(aesEncryptedData);
82 Log.d("ENC RSA ENCR", new String(rsaEncryptedData, "UTF-8"));
83 temp = Base64.encode(rsaEncryptedData, Base64.NO_PADDING | Base64.NO_WRAP); // base 64 the rsa
84 Log.d("ENC RSA TEMP", new String(temp, "UTF-8"));
85 } catch (Exception e)
86 {
87 e.printStackTrace();
88 }
89 return temp;
90}
91
92public Packet decryptPacket(byte[] encryptedData, Context context)
93{
94 // get the keys
95 PrivateKey pri = KeyFunctions.getPrivateKey(context);
96 Packet p = null;
97 byte[] aesDecryptedData = null;
98 byte[] rsaDecryptedData = null;
99
100
101 Log.d("START", "==========DECRYPT==========");
102 try
103 {
104 //RSA
105 Log.d("DEC INIT", new String(encryptedData, "UTF-8"));
106 Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
107 c.init(Cipher.DECRYPT_MODE, pri);
108 byte[] rsaTempArray = Base64.decode(encryptedData, Base64.NO_PADDING | Base64.NO_WRAP);
109 Log.d("DEC RSA TEMP", new String(rsaTempArray, "UTF-8"));
110 rsaDecryptedData = c.doFinal(rsaTempArray);
111 Log.d("DEC RSA ENCR", new String(rsaDecryptedData, "UTF-8"));
112
113 // AES
114 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
115 final SecretKeySpec secretKey = new SecretKeySpec(Constants.KEY.getBytes(), "AES");
116 cipher.init(Cipher.DECRYPT_MODE, secretKey);
117 byte[] aesTempArray = Base64.decode(rsaDecryptedData, Base64.NO_PADDING | Base64.NO_WRAP);
118 Log.d("DEC AES TEMP", new String(aesTempArray, "UTF-8"));
119 aesDecryptedData = cipher.doFinal(aesTempArray);
120 Log.d("DEC AES DEC", new String(aesDecryptedData, "UTF-8"));
121 p = new Packet(aesDecryptedData);
122 } catch (Exception e)
123 {
124 e.printStackTrace();
125 }
126 return p;
127}