|
1 | 1 | import javax.crypto.Cipher; |
2 | 2 | import javax.crypto.NoSuchPaddingException; |
| 3 | +import javax.crypto.SecretKeyFactory; |
3 | 4 | import javax.crypto.spec.IvParameterSpec; |
4 | 5 | import java.security.NoSuchAlgorithmException; |
5 | 6 |
|
6 | 7 | /* Class that manages setting up cryptographic resources */ |
7 | 8 | public abstract class CryptoResource { |
8 | 9 |
|
9 | 10 | protected static final int INIT_VECTOR_SIZE = 16; |
| 11 | + protected static final int SALT_SIZE = 8; |
| 12 | + protected static final int ITERATION_COUNT = 65536; |
| 13 | + protected static final int KEY_LENGTH = 256; |
| 14 | + |
10 | 15 | private static final String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5PADDING"; |
| 16 | + private static final String KEY_FACTORY_ALGORITHM = "PBKDF2WithHmacSHA256"; |
| 17 | + protected static final String KEY_SPEC_ALGORITHM = "AES"; |
| 18 | + protected static final String RANDOM_SECURE_ALGORITHM = "SHA1PRNG"; |
| 19 | + |
11 | 20 | protected final Cipher cipher; |
| 21 | + protected final SecretKeyFactory secretKeyFactory; |
12 | 22 |
|
13 | 23 | /* Initialises cryptographic resources */ |
14 | 24 | public CryptoResource() throws CryptoException { |
15 | 25 | try { |
16 | 26 | cipher = Cipher.getInstance(CIPHER_TRANSFORMATION); |
| 27 | + secretKeyFactory = SecretKeyFactory.getInstance(KEY_FACTORY_ALGORITHM); |
17 | 28 | } catch (NoSuchAlgorithmException e) { |
18 | 29 | throw new CryptoException("Algorithm not found for " + CIPHER_TRANSFORMATION); |
19 | 30 | } catch (NoSuchPaddingException e) { |
20 | 31 | throw new CryptoException("Padding scheme not found for " + CIPHER_TRANSFORMATION); |
21 | 32 | } |
22 | 33 | } |
23 | 34 |
|
24 | | - /* Pads a password to 16 bytes with spaces */ |
25 | | - protected String padPasswordTo16Bytes(String password) { |
26 | | - StringBuilder keyBuilder = new StringBuilder(password); |
27 | | - while (keyBuilder.length() < 16) |
28 | | - keyBuilder.append(" "); |
29 | | - return keyBuilder.toString(); |
30 | | - } |
31 | | - |
32 | 35 | } |
0 commit comments