forked from jflester/AES_and_DES_encrypter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncrypter.java
209 lines (198 loc) · 6.36 KB
/
Encrypter.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
package encrypter.p;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.*;
/* Encrypter uses Base64Coder class to do conversions in encrypt and decrypt.
Encrypter supports AES and DES cipher algorithms.
Is currently in ECB mode with NoPadding as padding.
Does CBC manually in encrypt and decrypt methods.
Strings must be in multiples of 64-bits or 128-bits
for DES and AES, respectively.
*/
public class Encrypter {
private String encryptionType;
private byte[] ivAES = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
private byte[] ivDES = {0, 0, 0, 0, 0, 0, 0, 0};
private IvParameterSpec ipsAES = new IvParameterSpec(ivAES);
private IvParameterSpec ipsDES = new IvParameterSpec(ivDES);
private SecretKey key;
//Basic Constructor.
public Encrypter(String encryptionType) {
this.encryptionType = encryptionType;
this.key = this.generateSecretKey();
}
//Generates a secret key.
public SecretKey generateSecretKey() {
KeyGenerator keyGen = null;
SecretKey key = null;
try {
keyGen = KeyGenerator.getInstance(encryptionType);
key = keyGen.generateKey();
} catch (NoSuchAlgorithmException e) {
System.out.println("No such algorithm.");
}
return key;
}
//Xors two byte arrays (byte[]) and returns a single byte[].
//Does not modify either of the arrays passed in.
private byte[] xorba(byte[] one, byte[] two){
byte[] res = new byte[one.length];
for (int i = 0; i < one.length; i++){
res[i] = one[i];
res[i] ^= two[i];
}
return res;
}
//Encrypts a String plaintext with the given secretKey using AES/DES.
public String encrypt(String plainText, SecretKey secretKey) {
byte[] encryptThis = null;
byte[] ciphertext = null;
String res = "";
Cipher cipher = null;
int pos = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
encryptThis = plainText.getBytes("UTF-8");
cipher = Cipher.getInstance(encryptionType.toString()+"/ECB/NoPadding");
if ((encryptThis.length)%8 == 0 && encryptionType.equals("DES")) {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] curr = new byte[8];
byte[] prev = new byte[8];
while (pos < encryptThis.length){
for (int i = 0; i < 8; i++){
curr[i] = encryptThis[pos+i];
if (pos == 0){
prev[i] = ivDES[i];
}
}
prev = xorba(prev, curr);
prev = cipher.doFinal(prev);
baos.write(prev);
pos += 8;
}
res = new String(Base64Coder.encode(baos.toByteArray()));
} else if ((encryptThis.length)%16 == 0 && encryptionType.equals("AES")) {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] curr = new byte[16];
byte[] prev = new byte[16];
while (pos < encryptThis.length){
for (int i = 0; i < 16; i++){
curr[i] = encryptThis[pos+i];
if (pos == 0){
prev[i] = ivAES[i];
}
}
prev = xorba(prev, curr);
prev = cipher.doFinal(prev);
baos.write(prev);
pos += 16;
}
res = new String(Base64Coder.encode(baos.toByteArray()));
} else {
return null;
}
} catch (NoSuchAlgorithmException e) {
System.out.println("No such algorithm.");
} catch (NoSuchPaddingException e) {
System.out.println("No such padding.");
} catch (InvalidKeyException e) {
System.out.println("Invalid key.");
} catch (IllegalBlockSizeException e) {
System.out.println("Illegal block size.");
} catch (BadPaddingException e) {
System.out.println("Bad padding.");
} catch (UnsupportedEncodingException e) {
System.out.println("Unsupported Encoding.");
} catch (IOException e) {
System.out.println("IO Exception.");
}
return res;
}
//Decrypts the given cipherText using the given secretKey and AES/DES.
public String decrypt(String cipherText, SecretKey secretKey) {
String plaintext = "";
Cipher cipher = null;
char[] decrypt = null;
byte[] decryptThis = null;
Stack<byte[]> s = new Stack<byte[]>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
decrypt = cipherText.toCharArray();
decryptThis = Base64Coder.decode(decrypt);
int pos = decryptThis.length;
cipher = Cipher.getInstance(encryptionType.toString()+"/ECB/NoPadding");
if ((decryptThis.length)%8 == 0 && encryptionType.equals("DES")){
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] curr = new byte[8];
byte[] prev = new byte[8];
byte[] block = new byte[8];
for (int i = 0; i < 8; i++){
prev[i] = decryptThis[pos-8+i];
}
pos -= 8;
while (pos > 0) {
for (int i = 0; i < 8; i++){
curr[i] = decryptThis[pos-8+i];
}
prev = cipher.doFinal(prev);
block = xorba(prev, curr);
s.push(block);
System.arraycopy(curr, 0, prev, 0, curr.length);
pos -= 8;
}
s.push(xorba(cipher.doFinal(prev), ivDES));
while (!s.isEmpty()) {
baos.write(s.pop());
}
plaintext = baos.toString("UTF-8");
} else if ((decryptThis.length)%16 == 0 && encryptionType.equals("AES")) {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] curr = new byte[16];
byte[] prev = new byte[16];
byte[] block = new byte[16];
for (int i = 0; i < 16; i++){
prev[i] = decryptThis[pos-16+i];
}
pos -= 16;
while (pos > 0) {
for (int i = 0; i < 16; i++){
curr[i] = decryptThis[pos-16+i];
}
prev = cipher.doFinal(prev);
block = xorba(prev, curr);
s.push(block);
System.arraycopy(curr, 0, prev, 0, curr.length);
pos -= 16;
}
s.push(xorba(cipher.doFinal(prev), ivAES));
while (!s.isEmpty()) {
baos.write(s.pop());
}
plaintext = baos.toString("UTF-8");
} else {
return null;
}
} catch (NoSuchAlgorithmException e) {
System.out.println("No such algorithm.");
} catch (NoSuchPaddingException e) {
System.out.println("No such padding.");
} catch (InvalidKeyException e) {
System.out.println("Invalid key.");
} catch (IllegalBlockSizeException e) {
System.out.println("Illegal block size.");
} catch (BadPaddingException e) {
System.out.println("Bad padding.");
} catch (UnsupportedEncodingException e) {
System.out.println("Unsupported encoding.");
} catch (IOException e) {
System.out.println("IO Exception.");
}
return plaintext;
}
}