|
1 | 1 | package com.thealgorithms.ciphers; |
2 | 2 |
|
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.nio.charset.StandardCharsets; |
3 | 5 | import java.security.InvalidAlgorithmParameterException; |
4 | 6 | import java.security.InvalidKeyException; |
5 | 7 | import java.security.NoSuchAlgorithmException; |
| 8 | +import java.security.SecureRandom; |
6 | 9 | import javax.crypto.BadPaddingException; |
7 | 10 | import javax.crypto.Cipher; |
8 | 11 | import javax.crypto.IllegalBlockSizeException; |
|
18 | 21 | * underlying bytes. |
19 | 22 | */ |
20 | 23 | public final class AESEncryption { |
| 24 | + private static final int GCM_IV_LENGTH = 12; |
| 25 | + private static final int GCM_TAG_LENGTH = 128; |
| 26 | + private static final int MIN_CIPHERTEXT_LENGTH = GCM_IV_LENGTH + GCM_TAG_LENGTH / Byte.SIZE; |
| 27 | + private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); |
| 28 | + private static final SecureRandom SECURE_RANDOM = new SecureRandom(); |
| 29 | + |
21 | 30 | private AESEncryption() { |
22 | 31 | } |
23 | 32 |
|
24 | | - private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); |
25 | | - private static Cipher aesCipher; |
26 | | - |
27 | 33 | /** |
28 | 34 | * 1. Generate a plain text for encryption 2. Get a secret key (printed in |
29 | 35 | * hexadecimal form). In actual use this must be encrypted and kept safe. |
@@ -55,34 +61,71 @@ public static SecretKey getSecretEncryptionKey() throws NoSuchAlgorithmException |
55 | 61 | } |
56 | 62 |
|
57 | 63 | /** |
58 | | - * Encrypts plainText in AES using the secret key |
| 64 | + * Encrypts {@code plainText} (UTF-8) with AES-GCM using {@code secKey}. |
59 | 65 | * |
60 | | - * @return byteCipherText (The encrypted text) |
61 | | - * @throws NoSuchPaddingException (from Cipher) |
| 66 | + * <p>Each call generates a fresh 12-byte random IV and uses an explicit 128-bit (16-byte) |
| 67 | + * GCM authentication tag. The returned byte array is formatted as |
| 68 | + * {@code IV || ciphertext || authentication tag} (a 12-byte IV followed by the ciphertext |
| 69 | + * and 16-byte authentication tag). |
| 70 | + * |
| 71 | + * @param plainText the plaintext string to encrypt |
| 72 | + * @param secKey the AES secret key used for encryption |
| 73 | + * @return byte array formatted as {@code IV || ciphertext || authentication tag} |
62 | 74 | * @throws NoSuchAlgorithmException (from Cipher) |
| 75 | + * @throws NoSuchPaddingException (from Cipher) |
63 | 76 | * @throws InvalidKeyException (from Cipher) |
64 | | - * @throws BadPaddingException (from Cipher) |
| 77 | + * @throws InvalidAlgorithmParameterException (from Cipher) |
65 | 78 | * @throws IllegalBlockSizeException (from Cipher) |
| 79 | + * @throws BadPaddingException (from Cipher) |
66 | 80 | */ |
67 | | - public static byte[] encryptText(String plainText, SecretKey secKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { |
68 | | - // AES defaults to AES/ECB/PKCS5Padding in Java 7 |
69 | | - aesCipher = Cipher.getInstance("AES/GCM/NoPadding"); |
70 | | - aesCipher.init(Cipher.ENCRYPT_MODE, secKey); |
71 | | - return aesCipher.doFinal(plainText.getBytes()); |
| 81 | + public static byte[] encryptText(String plainText, SecretKey secKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { |
| 82 | + byte[] iv = new byte[GCM_IV_LENGTH]; |
| 83 | + SECURE_RANDOM.nextBytes(iv); |
| 84 | + |
| 85 | + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 86 | + GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv); |
| 87 | + cipher.init(Cipher.ENCRYPT_MODE, secKey, gcmParameterSpec); |
| 88 | + byte[] cipherBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); |
| 89 | + |
| 90 | + ByteBuffer buffer = ByteBuffer.allocate(iv.length + cipherBytes.length); |
| 91 | + buffer.put(iv); |
| 92 | + buffer.put(cipherBytes); |
| 93 | + return buffer.array(); |
72 | 94 | } |
73 | 95 |
|
74 | 96 | /** |
75 | | - * Decrypts encrypted byte array using the key used for encryption. |
| 97 | + * Decrypts a payload formatted as {@code IV || ciphertext || authentication tag} using |
| 98 | + * {@code secKey}. |
| 99 | + * |
| 100 | + * <p>The payload must contain a 12-byte IV followed by the ciphertext and a 16-byte (128-bit) |
| 101 | + * GCM authentication tag (at least 28 bytes in total). |
76 | 102 | * |
77 | | - * @return plainText |
| 103 | + * @param byteCipherText encrypted payload formatted as {@code IV || ciphertext || authentication tag} |
| 104 | + * @param secKey the AES secret key used during encryption |
| 105 | + * @return decrypted UTF-8 plaintext string |
| 106 | + * @throws IllegalArgumentException if {@code byteCipherText} is {@code null} or shorter than 28 bytes |
| 107 | + * @throws NoSuchAlgorithmException (from Cipher) |
| 108 | + * @throws NoSuchPaddingException (from Cipher) |
| 109 | + * @throws InvalidKeyException (from Cipher) |
| 110 | + * @throws InvalidAlgorithmParameterException (from Cipher) |
| 111 | + * @throws IllegalBlockSizeException (from Cipher) |
| 112 | + * @throws BadPaddingException if authentication tag verification or decryption fails |
78 | 113 | */ |
79 | 114 | public static String decryptText(byte[] byteCipherText, SecretKey secKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { |
80 | | - // AES defaults to AES/ECB/PKCS5Padding in Java 7 |
| 115 | + if (byteCipherText == null || byteCipherText.length < MIN_CIPHERTEXT_LENGTH) { |
| 116 | + throw new IllegalArgumentException("Ciphertext is too short"); |
| 117 | + } |
| 118 | + ByteBuffer buffer = ByteBuffer.wrap(byteCipherText); |
| 119 | + byte[] iv = new byte[GCM_IV_LENGTH]; |
| 120 | + buffer.get(iv); |
| 121 | + byte[] cipherBytes = new byte[buffer.remaining()]; |
| 122 | + buffer.get(cipherBytes); |
| 123 | + |
81 | 124 | Cipher decryptionCipher = Cipher.getInstance("AES/GCM/NoPadding"); |
82 | | - GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, aesCipher.getIV()); |
| 125 | + GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv); |
83 | 126 | decryptionCipher.init(Cipher.DECRYPT_MODE, secKey, gcmParameterSpec); |
84 | | - byte[] bytePlainText = decryptionCipher.doFinal(byteCipherText); |
85 | | - return new String(bytePlainText); |
| 127 | + byte[] bytePlainText = decryptionCipher.doFinal(cipherBytes); |
| 128 | + return new String(bytePlainText, StandardCharsets.UTF_8); |
86 | 129 | } |
87 | 130 |
|
88 | 131 | /** |
|
0 commit comments