|
1 | 1 | package com.howtodoinjava.core.security;
|
2 | 2 |
|
3 |
| -import java.nio.charset.StandardCharsets; |
4 | 3 | import java.security.SecureRandom;
|
5 | 4 | import java.security.spec.KeySpec;
|
6 | 5 | import java.util.Base64;
|
7 |
| - |
8 | 6 | import javax.crypto.Cipher;
|
9 | 7 | import javax.crypto.SecretKey;
|
10 | 8 | import javax.crypto.SecretKeyFactory;
|
|
14 | 12 |
|
15 | 13 | public class AES256 {
|
16 | 14 |
|
17 |
| - private AES256(){ |
18 |
| - } |
| 15 | + private static final int KEY_LENGTH = 256; |
| 16 | + private static final int ITERATION_COUNT = 65536; |
19 | 17 |
|
20 |
| - private static final String SECRET_KEY = "my_super_secret_key_ho_ho_ho"; |
21 |
| - private static final byte[] SALT; |
22 |
| - private static final SecureRandom random; |
23 |
| - private static final IvParameterSpec ivspec; |
24 |
| - static { |
25 |
| - random = new SecureRandom(); |
26 |
| - |
27 |
| - SALT = new byte[16]; |
28 |
| - random.nextBytes(SALT); |
29 |
| - |
30 |
| - byte[] bytesIV = new byte[16]; |
31 |
| - random.nextBytes(bytesIV); |
32 |
| - ivspec = new IvParameterSpec(bytesIV); |
33 |
| - } |
| 18 | + public static String encrypt(String strToEncrypt, String secretKey, String salt) { |
34 | 19 |
|
35 |
| - public static String encrypt(String strToEncrypt) { |
36 | 20 | try {
|
| 21 | + |
| 22 | + SecureRandom secureRandom = new SecureRandom(); |
| 23 | + byte[] iv = new byte[16]; |
| 24 | + secureRandom.nextBytes(iv); |
| 25 | + IvParameterSpec ivspec = new IvParameterSpec(iv); |
| 26 | + |
37 | 27 | SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
|
38 |
| - KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALT, 65536, 256); |
| 28 | + KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), ITERATION_COUNT, KEY_LENGTH); |
39 | 29 | SecretKey tmp = factory.generateSecret(spec);
|
40 |
| - SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES"); |
| 30 | + SecretKeySpec secretKeySpec = new SecretKeySpec(tmp.getEncoded(), "AES"); |
41 | 31 |
|
42 | 32 | Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
43 |
| - cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec); |
44 |
| - return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8))); |
| 33 | + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivspec); |
| 34 | + |
| 35 | + byte[] cipherText = cipher.doFinal(strToEncrypt.getBytes("UTF-8")); |
| 36 | + byte[] encryptedData = new byte[iv.length + cipherText.length]; |
| 37 | + System.arraycopy(iv, 0, encryptedData, 0, iv.length); |
| 38 | + System.arraycopy(cipherText, 0, encryptedData, iv.length, cipherText.length); |
| 39 | + |
| 40 | + return Base64.getEncoder().encodeToString(encryptedData); |
45 | 41 | } catch (Exception e) {
|
46 |
| - System.out.println("Error while encrypting: " + e.toString()); |
| 42 | + // Handle the exception properly |
| 43 | + e.printStackTrace(); |
| 44 | + return null; |
47 | 45 | }
|
48 |
| - return null; |
49 | 46 | }
|
50 | 47 |
|
51 |
| - public static String decrypt(String strToDecrypt) { |
| 48 | + public static String decrypt(String strToDecrypt, String secretKey, String salt) { |
| 49 | + |
52 | 50 | try {
|
| 51 | + |
| 52 | + byte[] encryptedData = Base64.getDecoder().decode(strToDecrypt); |
| 53 | + byte[] iv = new byte[16]; |
| 54 | + System.arraycopy(encryptedData, 0, iv, 0, iv.length); |
| 55 | + IvParameterSpec ivspec = new IvParameterSpec(iv); |
| 56 | + |
53 | 57 | SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
|
54 |
| - KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALT, 65536, 256); |
| 58 | + KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), ITERATION_COUNT, KEY_LENGTH); |
55 | 59 | SecretKey tmp = factory.generateSecret(spec);
|
56 |
| - SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES"); |
| 60 | + SecretKeySpec secretKeySpec = new SecretKeySpec(tmp.getEncoded(), "AES"); |
| 61 | + |
| 62 | + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 63 | + cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivspec); |
| 64 | + |
| 65 | + byte[] cipherText = new byte[encryptedData.length - 16]; |
| 66 | + System.arraycopy(encryptedData, 16, cipherText, 0, cipherText.length); |
57 | 67 |
|
58 |
| - Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); |
59 |
| - cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec); |
60 |
| - return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); |
| 68 | + byte[] decryptedText = cipher.doFinal(cipherText); |
| 69 | + return new String(decryptedText, "UTF-8"); |
61 | 70 | } catch (Exception e) {
|
62 |
| - System.out.println("Error while decrypting: " + e.toString()); |
| 71 | + // Handle the exception properly |
| 72 | + e.printStackTrace(); |
| 73 | + return null; |
63 | 74 | }
|
64 |
| - return null; |
65 | 75 | }
|
66 | 76 | }
|
0 commit comments