· 7 years ago · Apr 01, 2018, 10:42 PM
1package ccmp_new;
2import java.*;
3import javax.xml.bind.DatatypeConverter;
4import java.lang.*;
5import java.security.*;
6import javax.crypto.Cipher;
7import javax.crypto.spec.*;
8import sun.misc.BASE64Encoder;
9import sun.misc.BASE64Decoder;
10import javax.crypto.spec.SecretKeySpec;
11import java.math.*;
12import javax.crypto.KeyGenerator;
13import javax.crypto.SecretKey;
14
15class Header {
16 String Preamble;
17 String DestinationAddress;
18 String SourceAddress;
19 String QoS;
20 int PN;
21
22 public Header (String p, String da, String sa, String t, int pn)
23 {
24 Preamble = p;
25 DestinationAddress = da;
26 SourceAddress = sa;
27 QoS = t;
28 PN = pn;
29 }
30}
31
32class Frame {
33
34 Header header;
35 String data;
36
37 public Frame(Header h, String d)
38 {
39 header = h;
40 data = d;
41 }
42
43 public String getData()
44 {
45 return data;
46 }
47
48 public Header getHeader()
49 {
50 return header;
51 }
52
53 public String getHeaderString()
54 {
55 return header.Preamble + header.DestinationAddress + header.SourceAddress + header.QoS + Integer.toString(header.PN);
56 }
57}
58
59class EncryptedFrame extends Frame {
60 String MIC;
61 public EncryptedFrame(Header h, String d, String m)
62 {
63 super(h, d);
64 MIC = m;
65 }
66
67}
68
69public class CCMP_new {
70
71
72 public static KeyGenerator key;
73 public static SecretKey secretKey;
74
75
76 public static void printFrame(Frame f)
77 {
78 System.out.println("Header: " + f.header.Preamble + " " + f.header.DestinationAddress + " " + f.header.SourceAddress + " " +
79 f.header.QoS + " " + f.header.PN + " \nData: " + f.data);
80 }
81
82 public static void printEncryptedFrame(EncryptedFrame f)
83 {
84 System.out.println("Header: " + f.header.Preamble + " " + f.header.DestinationAddress + " " + f.header.SourceAddress + " " +
85 f.header.QoS + " " + f.header.PN + " \nData: " + f.data + " \nMIC: " + f.MIC);
86 }
87 public static byte [] generateNonce(Header h)
88 {
89 int pn = h.PN;
90 byte[] result = new byte[16];
91 result[0] = (byte) (0);
92 result[1] = (byte) (0);
93 result[2] = (byte) (0);
94 result[3] = (byte) (0);
95 result[4] = (byte) (0);
96
97 result[5] = (byte) (pn >> 24);
98 result[6] = (byte) (pn >> 16);
99 result[7] = (byte) (pn >> 8);
100 result[8] = (byte) (pn /*>> 0*/);
101 String source = h.SourceAddress;
102 String[] sourceParts = source.split("-");
103
104
105 for(int i=9, j=0; i<15; i++, j++)
106 {
107 Integer hex = Integer.parseInt(sourceParts[j], 16);
108 result[i] = hex.byteValue();
109 }
110 Integer qos = Integer.parseInt(h.QoS, 10);
111 result[15] = qos.byteValue();
112
113
114 return result;
115 }
116
117 public static String calculateMIC(Frame f) throws Exception
118 {
119
120 byte[] bs = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
121
122 IvParameterSpec iv = new IvParameterSpec(bs);
123
124 Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING");
125
126 c.init(Cipher.ENCRYPT_MODE, secretKey, iv);
127 Header header = f.getHeader();
128 String data = f.getData();
129 String headerString = f.getHeaderString();
130 String frameString = headerString + data;
131 // System.out.println("Frame: " + frameString + "\n");
132 byte[] encVal = c.doFinal(frameString.getBytes());
133 int l = encVal.length;
134 byte [] MICbytes = {encVal[l-15], encVal[l-14], encVal[l-13], encVal[l-12], encVal[l-11], encVal[l-10], encVal[l-9], encVal[l-8]};
135 String MIC = new BASE64Encoder().encode(MICbytes);
136
137 // System.out.println("MIC: " + MIC);
138
139 return MIC;
140
141 }
142
143 public static EncryptedFrame encrypt(Frame f, String MIC) throws Exception
144 {
145 byte[] nonce = generateNonce(f.header);
146
147 IvParameterSpec iv = new IvParameterSpec(nonce);
148 Cipher c = Cipher.getInstance("AES/CTR/PKCS5Padding");
149 c.init(Cipher.ENCRYPT_MODE, secretKey, iv);
150
151
152 Header header = f.getHeader();
153 String data = f.getData();
154
155
156 String encData = new BASE64Encoder().encode(c.doFinal(data.getBytes()));
157
158
159 EncryptedFrame encFrame = new EncryptedFrame(f.header, encData, MIC);
160
161 return encFrame;
162 }
163
164 public static boolean checkMIC(EncryptedFrame f, String MIC) throws Exception
165 {
166 byte[] bs = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
167
168 IvParameterSpec iv = new IvParameterSpec(bs);
169 Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING");
170
171 c.init(Cipher.ENCRYPT_MODE, secretKey, iv);
172 String encFrameMIC = f.MIC;
173
174 System.out.println("\n\nReceived frame MIC: " + encFrameMIC + " \nCalculated MIC: " + MIC);
175
176 return MIC.equals(encFrameMIC);
177
178 }
179
180 public static Frame decrypt(EncryptedFrame f) throws Exception
181 {
182 byte[] nonce = generateNonce(f.header);
183
184 IvParameterSpec iv = new IvParameterSpec(nonce);
185 Cipher c = Cipher.getInstance("AES/CTR/PKCS5Padding");
186
187
188 c.init(Cipher.DECRYPT_MODE, secretKey, iv);
189 String data = f.getData();
190 byte [] outputDecoder = c.doFinal(new BASE64Decoder().decodeBuffer(data));
191 Header header = f.getHeader();
192
193 //System.out.println("Decrypted Data: " + new String(outputDecoder));
194
195 return new Frame(header, new String(outputDecoder));
196 }
197
198
199 public static void main(String[] args) throws Exception {
200
201 key = KeyGenerator.getInstance("AES");
202 key.init(128);
203
204 secretKey = key.generateKey();
205
206 Header h = new Header("1010101010101010101010101010101010101010101010101010101010101011",
207 "00-14-22-01-23-45", "00-14-22-01-23-62", "1", 2);
208 String data = "Ova se nekoi podatoci shto se prakjaat preku ethernetOva se nekoi podatoci shto se prakjaat preku ethernetOva se nekoi podatoci shto se prakjaat preku ethernetOva se nekoi podatoci shto se prakjaat preku ethernet";
209
210
211 Frame f = new Frame(h, data);
212 System.out.println("Original Frame:");
213 printFrame(f);
214 String MIC = calculateMIC(f);
215 System.out.println("Encrypted Frame:");
216 EncryptedFrame encFrame = encrypt(f, MIC);
217 printEncryptedFrame(encFrame);
218 Frame decFrame = decrypt(encFrame);
219 System.out.println("Decrypted Frame:");
220 printFrame(decFrame);
221 String decMIC = calculateMIC(decFrame);
222 if(checkMIC(encFrame, decMIC))
223 System.out.println("MIC vrednosta e tochna!");
224 else
225 System.out.println("MIC vrednosta NE e tochna!");
226
227 }
228
229}