-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrypto.java
More file actions
186 lines (169 loc) · 5.6 KB
/
Crypto.java
File metadata and controls
186 lines (169 loc) · 5.6 KB
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
package Chat;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.*;
import org.apache.commons.codec.binary.Hex;
import static org.apache.commons.codec.binary.Hex.decodeHex;
/**
*The class containing the encryption/decryption methods
* @author joar
*/
public class Crypto {
private String type;
private Object myKey;
private Cipher aesCipher;
/**
*Creates a new crypto of the given type
* @param type the type of the Crypto
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
public Crypto(String type) throws NoSuchAlgorithmException,
NoSuchPaddingException {
if(type.equals("AES") || type.equals("CAESAR")) {
this.type = type;
if(type.equals("AES")) {
aesCipher = Cipher.getInstance("AES");
KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
KeyGen.init(128);
myKey = KeyGen.generateKey();
}
else{
myKey = (int)(Math.random()*20+1);
}
}
else {
throw new NoSuchAlgorithmException();
}
}
/**
*Gets the type
* @return the type of the crypto
*/
public String getType() {
return type;
}
/**
*Gets the key of the crypto
* @return The key
*/
public String getKey() {
if(type.equals("AES")) {
byte[] data = ((SecretKey) myKey).getEncoded();
String key = Hex.encodeHexString(data);
return key;
}
else {
myKey = (int)(Math.random()*20+1);
String key = Integer.toString((int) myKey);
return key;
}
}
/**
*Sets a key which must correspond with the type of the Crypto
* @param key the key to be set
* @throws DecoderException
*/
public void setKey(String key) throws DecoderException {
if( type.equals("AES")) {
byte[] data = decodeHex(key.toCharArray());
myKey = new SecretKeySpec(data, "AES");
}
else {
myKey = Integer.parseInt(key);
}
}
/**
*Gets a encryption stream
* @param output the plaintext output stream
* @return the encrypted output stream
* @throws Exception
*/
public CipherOutputStream getEncryptStream(OutputStream output) throws Exception {
aesCipher.init(Cipher.ENCRYPT_MODE, (SecretKey) myKey);
CipherOutputStream encryptStream = new CipherOutputStream(output, aesCipher);
return encryptStream;
}
/**
*Gets a decryption stream
* @param input the encrypted input stream
* @return the plaintext input stream
* @throws Exception
*/
public CipherInputStream getDecryptStream(InputStream input) throws Exception {
aesCipher.init(Cipher.DECRYPT_MODE, (SecretKey) myKey);
CipherInputStream decryptStream = new CipherInputStream(input, aesCipher);
return decryptStream;
}
/**
*Decodes an encrypted message
* @param hexMessage the encrypted message as hex
* @return the plaintext
* @throws Exception
*/
public String decodeMessage(String hexMessage) throws Exception {
hexMessage = hexMessage.toLowerCase();
byte[] data = decodeHex(hexMessage.toCharArray());
if(type.equals("AES")) {
return decodeAES(data);
}
else{
return decodeCAESAR(data);
}
}
/**
*Encodes a plaintext message
* @param message the message
* @return the encrypted message as hex
* @throws Exception
*/
public String encodeMessage(String message) throws Exception {
if(type.equals("AES")) {
byte[] data = message.getBytes("UTF-8");
return encodeAES(data);
}
else{
return encodeCAESAR(message);
}
}
private String encodeAES(byte[] data) throws Exception {
aesCipher.init(Cipher.ENCRYPT_MODE, (SecretKey)myKey);
byte[] byteEncoded = aesCipher.doFinal(data);
String encodedHex = Hex.encodeHexString(byteEncoded);
return encodedHex.toUpperCase();
}
private String decodeAES(byte[] data) throws Exception {
aesCipher.init(Cipher.DECRYPT_MODE, (SecretKey) myKey);
byte[] bytesDecoded = aesCipher.doFinal(data);
Charset UTF8_CHARSET = Charset.forName("UTF-8");
return new String(bytesDecoded, UTF8_CHARSET);
}
private String encodeCAESAR(String message) {
StringBuilder stringBuilder = new StringBuilder();
int length = message.length();
for(int i = 0; i < length; i++) {
char c = (char)(message.charAt(i) + (int)myKey);
stringBuilder.append(c);
}
String encodedHex =
Hex.encodeHexString(stringBuilder.toString().getBytes());
return encodedHex.toUpperCase();
}
private String decodeCAESAR(byte[] data) throws Exception{
String encoded = new String(data, "UTF-8");
StringBuilder stringBuilder = new StringBuilder();
int length = encoded.length();
for(int i = 0; i < length; i++) {
char c = (char)(encoded.charAt(i) - (int)myKey);
stringBuilder.append(c);
}
return stringBuilder.toString();
}
}