-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAESCipher.java
executable file
·346 lines (310 loc) · 10.5 KB
/
AESCipher.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
import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidParameterSpecException;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.CipherSpi;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
public class AESCipher extends CipherSpi {
Cipher aesCipher;
AES cipher;
byte[] iv = new byte[16];
byte[] encrypted;
byte[] temp;
byte[] buffer;
boolean do_pad;
boolean do_cbc;
int opMode;
int bufferLen = 0;
protected void engineSetMode(String mode)
throws NoSuchAlgorithmException {
if (mode.equals("CBC")) {
do_cbc = true;
} else if (mode.equals("ECB")) {
do_cbc = false;
} else {
throw new NoSuchAlgorithmException();
}
}
protected void engineSetPadding(String padding)
throws NoSuchPaddingException {
if (padding.equals("NoPadding")) {
do_pad = false;
} else if (padding.equals("PKCS5Padding")) {
do_pad = true;
} else {
throw new NoSuchPaddingException();
}
}
protected int engineGetBlockSize() {
return 16; // This is constant for AES.
}
protected int engineGetOutputSize(int inputLen) {
int outputSize = 0;
int totalLength = bufferLen + inputLen;
int bytesInLastBlock = totalLength%16;
if (opMode != Cipher.DECRYPT_MODE && do_pad) {
outputSize = totalLength + (16 - bytesInLastBlock);
} else {
outputSize = totalLength - bytesInLastBlock;
}
return outputSize;
}
protected byte[] engineGetIV() {
byte[] retiv = new byte[16];
System.arraycopy(iv, 0, retiv, 0, 16);
return retiv;
}
protected AlgorithmParameters engineGetParameters() {
AlgorithmParameters ap = null;
try {
ap = AlgorithmParameters.getInstance("AES");
ap.init(new IvParameterSpec(engineGetIV()));
} catch (NoSuchAlgorithmException e) {
System.err.println("Internal Error: " + e);
} catch (InvalidParameterSpecException e) {
System.err.println("Internal Error: " + e);
}
return ap;
}
protected void engineInit(int opmode, Key key, SecureRandom random)
throws InvalidKeyException {
try {
engineInit(opmode, key, (AlgorithmParameterSpec)null, random);
} catch (InvalidAlgorithmParameterException e) {
System.err.println("Internal Error: " + e);
}
}
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)
throws InvalidKeyException {
try {
engineInit(opmode, key, params.getParameterSpec(IvParameterSpec.class), random);
} catch (InvalidParameterSpecException e) {
System.err.println("Internal Error: " + e);
} catch (InvalidAlgorithmParameterException e) {
System.err.println("Internal Error: " + e);
}
}
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException {
buffer = new byte[iv.length];
bufferLen = 0;
if (opmode == Cipher.DECRYPT_MODE || opmode == Cipher.ENCRYPT_MODE) {
opMode = opmode;
if (key.getEncoded().length == 16 || key.getEncoded().length == 24 || key.getEncoded().length == 32) {
cipher = new AES(key.getEncoded());
if (do_cbc) {
if (params == null) {
if (opMode == Cipher.ENCRYPT_MODE) {
byte[] tempIV = new byte[iv.length];
random.nextBytes(tempIV);
encrypted = Arrays.copyOf(tempIV, tempIV.length);
temp = Arrays.copyOf(tempIV, tempIV.length);
} else {//decrypt
throw new InvalidKeyException("Must provide IV for decryption mode");
}
} else {
if (params instanceof AlgorithmParameterSpec) {
if (params instanceof IvParameterSpec) {
if(((IvParameterSpec)params).getIV().length == 16) {
encrypted = Arrays.copyOf(((IvParameterSpec)params).getIV(), ((IvParameterSpec)params).getIV().length);
temp = Arrays.copyOf(((IvParameterSpec)params).getIV(),((IvParameterSpec)params).getIV().length);
iv = Arrays.copyOf(((IvParameterSpec)params).getIV(),((IvParameterSpec)params).getIV().length);
} else {
throw new InvalidAlgorithmParameterException("Incorrect IV length. IV length must be 16 bytes.");
}
} else {
throw new InvalidAlgorithmParameterException("IV is not present");
}
} else {
throw new InvalidAlgorithmParameterException("The specs of the IV are incorrect");
}
}
} else {//ECB
if (params == null) {
encrypted = new byte[16];
temp = new byte[16];
} else {
throw new InvalidAlgorithmParameterException("ECB does not allow an IV");
}
}
} else {
throw new InvalidKeyException("Key lenth must be 16,24, or 32 bytes");
}
} else {
opMode = -1;
throw new InvalidAlgorithmParameterException("Must choose encrypt or decrypt mode");
}
}
private int allocateSize(int inputLen) {
int outputSize = 0;
int totalLength = bufferLen + inputLen;
int bytesInLastBlock = totalLength%16;
if (opMode != Cipher.DECRYPT_MODE && do_pad) {
outputSize = totalLength + (16 - bytesInLastBlock);
} else {
outputSize = totalLength - bytesInLastBlock;
}
return outputSize;
}
protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) {
byte[] output = new byte[allocateSize(inputLen)];
int size = 0;
try {
size = engineUpdate(input, inputOffset, inputLen, output, 0);
} catch (ShortBufferException e) {
System.err.println("Internal Error: " + e);
}
return Arrays.copyOf(output, size);
}
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
throws ShortBufferException {
int totalLength = bufferLen + inputLen;
int blocks = totalLength/16;
int length = blocks * 16;
int bytesInLastBlock = totalLength%16;
int leftOut = inputLen - bytesInLastBlock;
int offsetCount = 0;
int outputCalc = outputOffset;
int buffMeasure = output.length - outputOffset;
if (buffMeasure < length) {
throw new ShortBufferException("Not enough space in the ouptu buffer");
}
if (blocks == 0) {
for (int i = 0; i < inputLen; i++)
{
buffer[bufferLen + i] = input[inputOffset + i];
}
bufferLen += inputLen;
return 0;
}
for (int j = 0; j < blocks; j++)
{
for (int i = bufferLen; i < 16; i++)
{
buffer[i] = input[inputOffset + i + offsetCount - bufferLen];
}
offsetCount += 16 - bufferLen;
bufferLen = 0;
if (opMode == Cipher.ENCRYPT_MODE) {
if (do_cbc) {
for(int k = 0; k < 16; k++)
{
buffer[k] = (byte) (encrypted[k] ^ buffer[k]);
}
}
buffer = cipher.encrypt(buffer);
encrypted = Arrays.copyOf(buffer, 16);
} else if (opMode == Cipher.DECRYPT_MODE) {
if (do_cbc) {
temp = Arrays.copyOf(buffer, 16);
buffer = cipher.decrypt(buffer);
for (int k = 0; k < 16; k++)
{
buffer[k] = (byte) (encrypted[k] ^ buffer[k]);
}
encrypted = Arrays.copyOf(temp, 16);
}
} else {
buffer = cipher.decrypt(buffer);
}
System.arraycopy(buffer, 0, output, outputOffset + (16 * j), 16);
outputCalc += 16;
}
for (int i = 0; i < bytesInLastBlock; i++)
{
buffer[bufferLen + i] = input[leftOut + i];
}
bufferLen += bytesInLastBlock;
return length;
}
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
throws IllegalBlockSizeException, BadPaddingException {
try {
byte[] temp = new byte[engineGetOutputSize(inputLen)];
int len = engineDoFinal(input, inputOffset, inputLen, temp, 0);
return Arrays.copyOf(temp, len);
} catch (ShortBufferException e) {
System.err.println("Internal Error: " + e);
return null;
}
}
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
int outLen = 0;
int multOf16 = (inputLen + bufferLen) % 16;
if (!do_pad && multOf16 != 0) {
throw new IllegalBlockSizeException("Total length must be a multiple of 16 when no padding is specified");
}
if (do_pad && multOf16 != 0 && opMode == Cipher.DECRYPT_MODE) {
throw new IllegalBlockSizeException("The total length must be a multiple of 16 for decryption");
}
if (input != null) {
outLen = engineUpdate(input, inputOffset, inputLen, output, outputOffset);
}
if (do_pad) {
if (opMode == Cipher.ENCRYPT_MODE) {
int paddingLen = 16 - bufferLen;
byte paddingVal = (byte) paddingLen;
for (int i = bufferLen; i < 16; i++)
{
buffer[i] = paddingVal;
}
if (do_cbc){
for (int j = 0; j < 16; j++)
{
buffer[j] = (byte) (encrypted[j] ^ buffer[j]);
}
}
buffer = cipher.encrypt(buffer);
int lastBlock = output.length - 16;
for (int h = 0; h < 16; h++)
{
output[lastBlock + h] = buffer[h];
}
outLen += 16;
} else {
//Decrypt mode
int padIndex = 0;
if (output.length - 1 != 0) {
padIndex = output.length - 1;
}
byte padValue = output[padIndex];
int numPadBytes = padValue;
if (numPadBytes <= 0 || numPadBytes > 16) {
throw new BadPaddingException("There must be between 0 and 16 padding bytes");
}
for (int i = 0; i < numPadBytes; i++)
{
if (output[padIndex - i] != padValue) {
throw new BadPaddingException("The value of each padding byte must be equal to the total number of padding bytes");
}
}
outLen -= padValue;
}
} else {
buffer = cipher.decrypt(buffer);
}
bufferLen = 0;
buffer = new byte[16];
if (!do_cbc) {
encrypted = new byte[16];
temp = new byte[16];
} else {
encrypted = Arrays.copyOf(engineGetIV(), 16);
temp = Arrays.copyOf(engineGetIV(), 16);
}
return outLen;
}
}