1 package be
.nikiroo
.utils
;
3 import java
.io
.IOException
;
4 import java
.security
.InvalidKeyException
;
5 import java
.security
.NoSuchAlgorithmException
;
7 import javax
.crypto
.BadPaddingException
;
8 import javax
.crypto
.Cipher
;
9 import javax
.crypto
.IllegalBlockSizeException
;
10 import javax
.crypto
.NoSuchPaddingException
;
11 import javax
.crypto
.SecretKey
;
12 import javax
.crypto
.spec
.SecretKeySpec
;
15 * Small utility class to do AES encryption/decryption.
17 * Do not assume it is actually secure until you checked the code...
21 public class CryptUtils
{
22 private Cipher ecipher
;
23 private Cipher dcipher
;
26 * Small and leazy way to initialize a 128 bits key with {@link CryptUtils}.
28 * <b>Some</b> part of the key will be used to generate a 128 bits key and
29 * initialize the {@link CryptUtils}; even NULL will generate something.
31 * <b>This is most probably not secure. Do not use if you actually care
35 * the {@link String} to use as a base for the key, can be NULL
37 public CryptUtils(String key
) {
40 } catch (InvalidKeyException e
) {
41 // We made sure that the key is correct, so nothing here
47 * Create a new instance of {@link CryptUtils} with the given 128 bytes key.
49 * The key <b>must</b> be exactly 128 bytes long.
52 * the 128 bits (32 bytes) of the key
54 * @throws InvalidKeyException
55 * if the key is not an array of 128 bytes
57 public CryptUtils(byte[] bytes32
) throws InvalidKeyException
{
62 * This method required an array of 128 bytes.
65 * the array, which <b>must</b> be of 128 bits (32 bytes)
67 * @throws InvalidKeyException
68 * if the key is not an array of 128 bits (32 bytes)
70 private void init(byte[] bytes32
) throws InvalidKeyException
{
71 if (bytes32
== null || bytes32
.length
!= 32) {
72 throw new InvalidKeyException(
73 "The size of the key must be of 128 bits (32 bytes), it is: "
74 + (bytes32
== null ?
"null" : "" + bytes32
.length
)
78 SecretKey key
= new SecretKeySpec(bytes32
, "AES");
80 ecipher
= Cipher
.getInstance("AES/ECB/PKCS5Padding");
81 dcipher
= Cipher
.getInstance("AES/ECB/PKCS5Padding");
82 ecipher
.init(Cipher
.ENCRYPT_MODE
, key
);
83 dcipher
.init(Cipher
.DECRYPT_MODE
, key
);
84 } catch (NoSuchAlgorithmException e
) {
85 // Every implementation of the Java platform is required to support
86 // this standard Cipher transformation with 128 bits keys
88 } catch (NoSuchPaddingException e
) {
89 // Every implementation of the Java platform is required to support
90 // this standard Cipher transformation with 128 bits keys
92 } catch (InvalidKeyException e
) {
93 // Every implementation of the Java platform is required to support
94 // this standard Cipher transformation with 128 bits keys
103 * the data to encrypt
105 * @return the encrypted data
107 * @throws IOException
108 * in case of I/O error (i.e., the data is not what you assumed
111 public byte[] encrypt(byte[] data
) throws IOException
{
113 return ecipher
.doFinal(data
);
114 } catch (IllegalBlockSizeException e
) {
115 throw new IOException(e
);
116 } catch (BadPaddingException e
) {
117 throw new IOException(e
);
125 * the data to encrypt
127 * @return the encrypted data
129 * @throws IOException
130 * in case of I/O error (i.e., the data is not what you assumed
133 public byte[] encrypt(String data
) throws IOException
{
134 return encrypt(data
.getBytes("UTF8"));
138 * Encrypt the data, then encode it into Base64.
141 * the data to encrypt
143 * TRUE to also compress the data in GZIP format; remember that
144 * compressed and not-compressed content are different; you need
145 * to know which is which when decoding
147 * @return the encrypted data, encoded in Base64
149 * @throws IOException
150 * in case of I/O error (i.e., the data is not what you assumed
153 public String
encrypt64(String data
, boolean zip
) throws IOException
{
154 return encrypt64(data
.getBytes("UTF8"), zip
);
158 * Encrypt the data, then encode it into Base64.
161 * the data to encrypt
163 * TRUE to also compress the data in GZIP format; remember that
164 * compressed and not-compressed content are different; you need
165 * to know which is which when decoding
167 * @return the encrypted data, encoded in Base64
169 * @throws IOException
170 * in case of I/O error (i.e., the data is not what you assumed
173 public String
encrypt64(byte[] data
, boolean zip
) throws IOException
{
174 return StringUtils
.base64(encrypt(data
), zip
);
178 * Decode the data which is assumed to be encrypted with the same utilities.
181 * the encrypted data to decode
183 * @return the original, decoded data
185 * @throws IOException
186 * in case of I/O error
188 public byte[] decrypt(byte[] data
) throws IOException
{
190 return dcipher
.doFinal(data
);
191 } catch (IllegalBlockSizeException e
) {
192 throw new IOException(e
);
193 } catch (BadPaddingException e
) {
194 throw new IOException(e
);
199 * Decode the data which is assumed to be encrypted with the same utilities
200 * and is a Base64 encoded value.
203 * the encrypted data to decode in Base64 format
205 * TRUE to also uncompress the data from a GZIP format
206 * automatically; if set to FALSE, zipped data can be returned
208 * @return the original, decoded data
210 * @throws IOException
211 * in case of I/O error
213 public byte[] decrypt64(String data
, boolean zip
) throws IOException
{
214 return decrypt(StringUtils
.unbase64(data
, zip
));
218 * Decode the data which is assumed to be encrypted with the same utilities
219 * and is a Base64 encoded value, then convert it into a String (this method
220 * assumes the data <b>was</b> indeed a UTF-8 encoded {@link String}).
223 * the encrypted data to decode in Base64 format
225 * TRUE to also uncompress the data from a GZIP format
226 * automatically; if set to FALSE, zipped data can be returned
228 * @return the original, decoded data
230 * @throws IOException
231 * in case of I/O error
233 public String
decrypt64s(String data
, boolean zip
) throws IOException
{
234 return new String(decrypt(StringUtils
.unbase64(data
, zip
)), "UTF-8");
238 * This is probably <b>NOT</b> secure!
241 * some {@link String} input
243 * @return a 128 bits key computed from the given input
245 static private byte[] key2key(String input
) {
246 return StringUtils
.getMd5Hash(input
).getBytes();