这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
c358ecd
Merge branch 'eugenp:master' into master
wynnteo May 16, 2024
74f4a8b
Merge branch 'eugenp:master' into master
wynnteo May 23, 2024
12ae4a2
Merge branch 'eugenp:master' into master
wynnteo May 24, 2024
a556cad
Merge branch 'eugenp:master' into master
wynnteo May 28, 2024
e5f3c5a
Merge branch 'eugenp:master' into master
wynnteo May 31, 2024
5b337e9
Merge branch 'eugenp:master' into master
wynnteo Jun 5, 2024
55688c1
Merge branch 'eugenp:master' into master
wynnteo Jun 10, 2024
8b0ca7c
Merge branch 'eugenp:master' into master
wynnteo Jun 11, 2024
6350935
Merge branch 'eugenp:master' into master
wynnteo Jun 16, 2024
d73ca7f
Merge branch 'eugenp:master' into master
wynnteo Jun 23, 2024
45003f6
Merge branch 'eugenp:master' into master
wynnteo Jun 24, 2024
c458ec0
Merge branch 'eugenp:master' into master
wynnteo Jun 25, 2024
3705a63
Merge branch 'eugenp:master' into master
wynnteo Jul 1, 2024
1c782c3
Merge branch 'eugenp:master' into master
wynnteo Jul 2, 2024
f2de90d
Merge branch 'eugenp:master' into master
wynnteo Jul 5, 2024
53dc085
Merge branch 'eugenp:master' into master
wynnteo Jul 8, 2024
d4c19ab
Merge branch 'eugenp:master' into master
wynnteo Jul 10, 2024
ff8cd93
Merge branch 'eugenp:master' into master
wynnteo Jul 15, 2024
29bcb1d
Merge branch 'eugenp:master' into master
wynnteo Jul 16, 2024
16d5163
Merge branch 'eugenp:master' into master
wynnteo Jul 19, 2024
842edf8
Merge branch 'eugenp:master' into master
wynnteo Jul 22, 2024
f132c30
Merge branch 'eugenp:master' into master
wynnteo Jul 24, 2024
ed1c995
Merge branch 'eugenp:master' into master
wynnteo Jul 25, 2024
be344fb
Merge branch 'eugenp:master' into master
wynnteo Aug 6, 2024
541adfb
Merge branch 'eugenp:master' into master
wynnteo Aug 9, 2024
6fd4d5d
Merge branch 'eugenp:master' into master
wynnteo Aug 13, 2024
80a462c
Merge branch 'eugenp:master' into master
wynnteo Aug 15, 2024
55f1987
Merge branch 'eugenp:master' into master
wynnteo Aug 20, 2024
6df4fb8
Merge branch 'eugenp:master' into master
wynnteo Aug 26, 2024
d1e423e
Merge branch 'eugenp:master' into master
wynnteo Sep 5, 2024
7c7354e
Merge branch 'eugenp:master' into master
wynnteo Sep 16, 2024
f434a47
Merge branch 'eugenp:master' into master
wynnteo Sep 19, 2024
4774de0
Merge branch 'eugenp:master' into master
wynnteo Sep 21, 2024
2260623
Merge branch 'eugenp:master' into master
wynnteo Sep 30, 2024
984f427
Merge branch 'eugenp:master' into master
wynnteo Sep 30, 2024
80cbd03
Merge branch 'eugenp:master' into master
wynnteo Oct 3, 2024
22dcaba
Merge branch 'eugenp:master' into master
wynnteo Oct 3, 2024
05494c0
Merge branch 'eugenp:master' into master
wynnteo Oct 10, 2024
0e87d28
Merge branch 'eugenp:master' into master
wynnteo Oct 14, 2024
983608b
Merge branch 'eugenp:master' into master
wynnteo Oct 22, 2024
b37b377
Merge branch 'eugenp:master' into master
wynnteo Oct 29, 2024
dadfb2f
Merge branch 'eugenp:master' into master
wynnteo Nov 1, 2024
ece1b1c
Merge branch 'eugenp:master' into master
wynnteo Nov 7, 2024
319081a
Merge branch 'eugenp:master' into master
wynnteo Nov 8, 2024
ac445aa
Merge branch 'eugenp:master' into master
wynnteo Nov 26, 2024
64c96b9
Merge branch 'eugenp:master' into master
wynnteo Dec 11, 2024
ee18089
Merge branch 'eugenp:master' into master
wynnteo Dec 18, 2024
d32e660
Merge branch 'eugenp:master' into master
wynnteo Dec 19, 2024
c186fdd
Merge branch 'eugenp:master' into master
wynnteo Dec 24, 2024
e216122
Merge branch 'eugenp:master' into master
wynnteo Jan 9, 2025
96f545e
Merge branch 'eugenp:master' into master
wynnteo Jan 20, 2025
b2a8b7b
Merge branch 'eugenp:master' into master
wynnteo Feb 5, 2025
afb1645
Merge branch 'eugenp:master' into master
wynnteo Feb 12, 2025
11308fb
Merge branch 'eugenp:master' into master
wynnteo Feb 21, 2025
76e0935
Merge branch 'eugenp:master' into master
wynnteo Feb 25, 2025
c0fe750
Merge branch 'eugenp:master' into master
wynnteo Feb 28, 2025
80bee83
Merge branch 'eugenp:master' into master
wynnteo Mar 10, 2025
71b8373
Merge branch 'eugenp:master' into master
wynnteo Mar 13, 2025
f8338d0
Merge branch 'eugenp:master' into master
wynnteo Mar 18, 2025
6687c14
Update CBC to GCM
wynnteo Mar 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKeyFactory;
import javax.crypto.SealedObject;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
Expand All @@ -26,7 +27,7 @@

public class AESUtil {

public static String encrypt(String algorithm, String input, SecretKey key, IvParameterSpec iv)
public static String encrypt(String algorithm, String input, SecretKey key, GCMParameterSpec iv)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(algorithm);
Expand All @@ -36,7 +37,7 @@ public static String encrypt(String algorithm, String input, SecretKey key, IvPa
.encodeToString(cipherText);
}

public static String decrypt(String algorithm, String cipherText, SecretKey key, IvParameterSpec iv)
public static String decrypt(String algorithm, String cipherText, SecretKey key, GCMParameterSpec iv)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(algorithm);
Expand All @@ -62,13 +63,13 @@ public static SecretKey getKeyFromPassword(String password, String salt)
return secret;
}

public static IvParameterSpec generateIv() {
byte[] iv = new byte[16];
public static GCMParameterSpec generateIv() {
byte[] iv = new byte[12];
new SecureRandom().nextBytes(iv);
return new IvParameterSpec(iv);
return new GCMParameterSpec(128, iv);
}

public static void encryptFile(String algorithm, SecretKey key, IvParameterSpec iv,
public static void encryptFile(String algorithm, SecretKey key, GCMParameterSpec iv,
File inputFile, File outputFile) throws IOException, NoSuchPaddingException,
NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
Expand All @@ -92,7 +93,7 @@ public static void encryptFile(String algorithm, SecretKey key, IvParameterSpec
outputStream.close();
}

public static void decryptFile(String algorithm, SecretKey key, IvParameterSpec iv,
public static void decryptFile(String algorithm, SecretKey key, GCMParameterSpec iv,
File encryptedFile, File decryptedFile) throws IOException, NoSuchPaddingException,
NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
Expand All @@ -117,7 +118,7 @@ public static void decryptFile(String algorithm, SecretKey key, IvParameterSpec
}

public static SealedObject encryptObject(String algorithm, Serializable object, SecretKey key,
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
GCMParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException, IOException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
Expand All @@ -126,7 +127,7 @@ public static SealedObject encryptObject(String algorithm, Serializable object,
}

public static Serializable decryptObject(String algorithm, SealedObject sealedObject, SecretKey key,
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
GCMParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException, ClassNotFoundException,
BadPaddingException, IllegalBlockSizeException, IOException {
Cipher cipher = Cipher.getInstance(algorithm);
Expand All @@ -135,19 +136,19 @@ public static Serializable decryptObject(String algorithm, SealedObject sealedOb
return unsealObject;
}

public static String encryptPasswordBased(String plainText, SecretKey key, IvParameterSpec iv)
public static String encryptPasswordBased(String plainText, SecretKey key, GCMParameterSpec iv)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
return Base64.getEncoder()
.encodeToString(cipher.doFinal(plainText.getBytes()));
}

public static String decryptPasswordBased(String cipherText, SecretKey key, IvParameterSpec iv)
public static String decryptPasswordBased(String cipherText, SecretKey key, GCMParameterSpec iv)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
return new String(cipher.doFinal(Base64.getDecoder()
.decode(cipherText)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javax.crypto.SealedObject;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
Expand All @@ -27,12 +28,12 @@ void givenString_whenEncrypt_thenSuccess()
// given
String input = "baeldung";
SecretKey key = AESUtil.generateKey(128);
IvParameterSpec ivParameterSpec = AESUtil.generateIv();
String algorithm = "AES/CBC/PKCS5Padding";
GCMParameterSpec gcmParameterSpec = AESUtil.generateIv();
String algorithm = "AES/GCM/NoPadding";

// when
String cipherText = AESUtil.encrypt(algorithm, input, key, ivParameterSpec);
String plainText = AESUtil.decrypt(algorithm, cipherText, key, ivParameterSpec);
String cipherText = AESUtil.encrypt(algorithm, input, key, gcmParameterSpec);
String plainText = AESUtil.decrypt(algorithm, cipherText, key, gcmParameterSpec);

// then
Assertions.assertEquals(input, plainText);
Expand All @@ -44,16 +45,16 @@ void givenFile_whenEncrypt_thenSuccess()
BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
// given
SecretKey key = AESUtil.generateKey(128);
String algorithm = "AES/CBC/PKCS5Padding";
IvParameterSpec ivParameterSpec = AESUtil.generateIv();
String algorithm = "AES/GCM/NoPadding";
GCMParameterSpec gcmParameterSpec = AESUtil.generateIv();
File inputFile = Paths.get("src/test/resources/baeldung.txt")
.toFile();
File encryptedFile = new File("baeldung.encrypted");
File decryptedFile = new File("document.decrypted");

// when
AESUtil.encryptFile(algorithm, key, ivParameterSpec, inputFile, encryptedFile);
AESUtil.decryptFile(algorithm, key, ivParameterSpec, encryptedFile, decryptedFile);
AESUtil.encryptFile(algorithm, key, gcmParameterSpec, inputFile, encryptedFile);
AESUtil.decryptFile(algorithm, key, gcmParameterSpec, encryptedFile, decryptedFile);

// then
assertThat(inputFile).hasSameTextualContentAs(decryptedFile);
Expand All @@ -69,12 +70,12 @@ void givenObject_whenEncrypt_thenSuccess()
// given
Student student = new Student("Baeldung", 20);
SecretKey key = AESUtil.generateKey(128);
IvParameterSpec ivParameterSpec = AESUtil.generateIv();
String algorithm = "AES/CBC/PKCS5Padding";
GCMParameterSpec gcmParameterSpec = AESUtil.generateIv();
String algorithm = "AES/GCM/NoPadding";

// when
SealedObject sealedObject = AESUtil.encryptObject(algorithm, student, key, ivParameterSpec);
Student object = (Student) AESUtil.decryptObject(algorithm, sealedObject, key, ivParameterSpec);
SealedObject sealedObject = AESUtil.encryptObject(algorithm, student, key, gcmParameterSpec);
Student object = (Student) AESUtil.decryptObject(algorithm, sealedObject, key, gcmParameterSpec);

// then
assertThat(student).isEqualTo(object);
Expand All @@ -88,12 +89,12 @@ void givenPassword_whenEncrypt_thenSuccess()
String plainText = "www.baeldung.com";
String password = "baeldung";
String salt = "12345678";
IvParameterSpec ivParameterSpec = AESUtil.generateIv();
GCMParameterSpec gcmParameterSpec = AESUtil.generateIv();
SecretKey key = AESUtil.getKeyFromPassword(password, salt);

// when
String cipherText = AESUtil.encryptPasswordBased(plainText, key, ivParameterSpec);
String decryptedCipherText = AESUtil.decryptPasswordBased(cipherText, key, ivParameterSpec);
String cipherText = AESUtil.encryptPasswordBased(plainText, key, gcmParameterSpec);
String decryptedCipherText = AESUtil.decryptPasswordBased(cipherText, key, gcmParameterSpec);

// then
Assertions.assertEquals(plainText, decryptedCipherText);
Expand Down