add aes main source
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package com.baeldung.crypto;
|
||||
|
||||
import org.assertj.core.api.WithAssertions;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.crypto.*;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
|
||||
//@SpringBootTest
|
||||
class AESUtilUnitTest implements WithAssertions {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException {
|
||||
// given
|
||||
String input = "baeldung";
|
||||
SecretKey key = AESUtil.generateKey(128);
|
||||
IvParameterSpec ivParameterSpec = AESUtil.generateIv();
|
||||
String algorithm = "AES/CBC/PKCS5Padding";
|
||||
|
||||
// when
|
||||
String cipherText = AESUtil.encrypt(algorithm, input, key, ivParameterSpec);
|
||||
String plainText = AESUtil.decrypt(algorithm, cipherText, key, ivParameterSpec);
|
||||
|
||||
// then
|
||||
Assertions.assertEquals(input, plainText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenFile_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException, IOException {
|
||||
// given
|
||||
SecretKey key = AESUtil.generateKey(128);
|
||||
String algorithm = "AES/CBC/PKCS5Padding";
|
||||
IvParameterSpec ivParameterSpec = AESUtil.generateIv();
|
||||
File inputFile = Paths.get("src/test/resources/baeldung.txt").toFile();
|
||||
File encryptedFile = new File("classpath:baeldung.encrypted");
|
||||
File decryptedFile = new File("document.decrypted");
|
||||
|
||||
// when
|
||||
AESUtil.encryptFile(algorithm, key, ivParameterSpec, inputFile, encryptedFile);
|
||||
AESUtil.decryptFile(algorithm, key, ivParameterSpec, encryptedFile, decryptedFile);
|
||||
|
||||
// then
|
||||
assertThat(inputFile).hasSameTextualContentAs(decryptedFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenObject_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException {
|
||||
// given
|
||||
Student student = new Student("Baeldung", 20);
|
||||
SecretKey key = AESUtil.generateKey(128);
|
||||
IvParameterSpec ivParameterSpec = AESUtil.generateIv();
|
||||
String algorithm = "AES/CBC/PKCS5Padding";
|
||||
|
||||
// when
|
||||
SealedObject sealedObject = AESUtil.encryptObject(algorithm, student, key, ivParameterSpec);
|
||||
Student object = (Student) AESUtil.decryptObject(algorithm, sealedObject, key, ivParameterSpec);
|
||||
|
||||
// then
|
||||
assertThat(student).isEqualToComparingFieldByField(object);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPassword_whenEncrypt_thenSuccess() throws InvalidKeySpecException, NoSuchAlgorithmException {
|
||||
// given
|
||||
String plainText = "www.baeldung.com";
|
||||
String password = "baeldung";
|
||||
String salt = "12345678";
|
||||
IvParameterSpec ivParameterSpec = AESUtil.generateIv();
|
||||
SecretKey key = AESUtil.getKeyFromPassword(password,salt);
|
||||
|
||||
// when
|
||||
String cipherText = AESUtil.encryptPasswordBased(plainText, key, ivParameterSpec);
|
||||
String decryptedCipherText = AESUtil.decryptPasswordBased(cipherText, key, ivParameterSpec);
|
||||
|
||||
// then
|
||||
Assertions.assertEquals(plainText, decryptedCipherText);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user