-
Notifications
You must be signed in to change notification settings - Fork 195
/
CcAes.java
233 lines (218 loc) · 7.12 KB
/
CcAes.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.facets.auth.codecs;
import java.io.IOException;
import java.nio.charset.Charset;
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 javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import lombok.EqualsAndHashCode;
import org.takes.facets.auth.Identity;
/**
* AES codec which supports 128 bits key.
*
* <p>It's recommended to use it in conjunction with {@link CcSigned} codec,
* which can be applied
* <a href="https://crypto.stackexchange.com/a/205">before or after</a>
* encryption.
* <p>The class is immutable and thread-safe.
* @since 0.13.8
*/
@EqualsAndHashCode
public final class CcAes implements Codec {
/**
* Secure random instance.
*/
private static final SecureRandom RANDOM = new SecureRandom();
/**
* The block size constant.
*/
private static final int BLOCK = 16;
/**
* Original codec.
*/
private final Codec origin;
/**
* The encryption key.
*/
private final Key key;
/**
* Random.
*/
private final SecureRandom random;
/**
* Constructor for the class.
*
* @param codec Original codec
* @param key The encryption key
* @since 0.22
*/
public CcAes(final Codec codec, final String key) {
this(codec, key.getBytes(Charset.defaultCharset()));
}
/**
* Constructor for the class.
*
* @param codec Original codec
* @param key The encryption key
*/
public CcAes(final Codec codec, final byte[] key) {
this(
codec,
CcAes.RANDOM,
new SecretKeySpec(
CcAes.withCorrectBlockSize(key.clone()), "AES"
)
);
}
/**
* Constructor for the class.
*
* @param codec Original codec
* @param random Random generator
* @param key The encryption key
*/
public CcAes(
final Codec codec,
final SecureRandom random,
final Key key
) {
this.origin = codec;
this.key = key;
this.random = random;
}
@Override
public byte[] encode(final Identity identity) throws IOException {
return this.encrypt(this.origin.encode(identity));
}
@Override
public Identity decode(final byte[] bytes) throws IOException {
return this.origin.decode(this.decrypt(bytes));
}
/**
* Encrypt the given bytes using AES.
*
* @param bytes Bytes to encrypt
* @return Encrypted byte using AES algorithm
* @throws IOException for all unexpected exceptions
*/
private byte[] encrypt(final byte[] bytes) throws IOException {
try {
final byte[] vector = new byte[CcAes.BLOCK];
this.random.nextBytes(vector);
final byte[] message = this.cipher(
Cipher.ENCRYPT_MODE,
new IvParameterSpec(vector)
).doFinal(bytes);
final byte[] res = new byte[vector.length + message.length];
System.arraycopy(vector, 0, res, 0, vector.length);
System.arraycopy(
message,
0,
res,
vector.length,
message.length
);
return res;
} catch (final BadPaddingException | IllegalBlockSizeException ex) {
throw new IOException(ex);
}
}
/**
* Check the block size of the key.
*
* @param key The encryption key
* @return The verified encryption key
*/
private static byte[] withCorrectBlockSize(final byte[] key) {
if (key.length != CcAes.BLOCK) {
throw new IllegalArgumentException(
String.format(
"the length of the AES key must be exactly %d bytes",
CcAes.BLOCK
)
);
}
return key;
}
/**
* Decrypt the given bytes using AES.
*
* @param bytes Bytes to decrypt
* @return Decrypted bytes
* @throws IOException for all unexpected exceptions
*/
private byte[] decrypt(final byte[] bytes) throws IOException {
if (bytes.length < CcAes.BLOCK << 1) {
throw new DecodingException("Invalid encrypted message format");
}
try {
final byte[] vector = new byte[CcAes.BLOCK];
final byte[] message = new byte[bytes.length - vector.length];
System.arraycopy(bytes, 0, vector, 0, vector.length);
System.arraycopy(
bytes,
vector.length,
message,
0,
message.length
);
return this.cipher(
Cipher.DECRYPT_MODE,
new IvParameterSpec(vector)
).doFinal(message);
} catch (final BadPaddingException | IllegalBlockSizeException ex) {
throw new DecodingException(ex);
}
}
/**
* Create new cipher based on the valid mode from {@link Cipher} class.
*
* @param mode Either Cipher.ENRYPT_MODE or Cipher.DECRYPT_MODE
* @param spec Param spec (IV)
* @return The cipher
* @throws IOException For any unexpected exceptions
*/
private Cipher cipher(final int mode, final AlgorithmParameterSpec spec)
throws IOException {
try {
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(mode, this.key, spec, this.random);
return cipher;
} catch (final InvalidKeyException | NoSuchAlgorithmException
| InvalidAlgorithmParameterException
| NoSuchPaddingException ex) {
throw new IOException(ex);
}
}
}