반응형
encryptByRandom 은 iv가 random하게 생성되어서 같은 값을 암호화 하더라도 항상 다르게 나옵니다.
encryptByFix 는 iv가 key를 참조해 생성되어서 항상 같은 값으로 암호화 됩니다.
decrypt는 앞의 16자리를 iv로 참조해서 복호화하므로 encrypt 방법과 무관하게 복호화 됩니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.InvalidAlgorithmParameterException; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.util.Arrays; | |
import java.util.Base64; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.spec.GCMParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
public class Aes256Util { | |
public static final int GCM_IV_LENGTH = 16; | |
public static final int GCM_TAG_LENGTH = 128; | |
public static String encryptByRandom(String plaintext, byte[] key) | |
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, | |
IllegalBlockSizeException, BadPaddingException { | |
if (plaintext == null) { | |
return null; | |
} | |
SecureRandom random = new SecureRandom(); | |
byte[] iv = new byte[GCM_IV_LENGTH]; | |
random.nextBytes(iv); | |
SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); | |
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | |
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv); | |
cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec); | |
byte[] cipherText = cipher.doFinal(plaintext.getBytes()); | |
return new String(Base64.getEncoder().encode(concatenate(iv, cipherText))); | |
} | |
public static String encryptByFix(String plaintext, byte[] key) | |
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, | |
IllegalBlockSizeException, BadPaddingException { | |
if (plaintext == null) { | |
return null; | |
} | |
byte[] iv = Arrays.copyOfRange(key, 0, GCM_IV_LENGTH); | |
SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); | |
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | |
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv); | |
cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec); | |
byte[] cipherText = cipher.doFinal(plaintext.getBytes()); | |
return new String(Base64.getEncoder().encode(concatenate(iv, cipherText))); | |
} | |
public static String decrypt(String str, byte[] key) throws NoSuchAlgorithmException, NoSuchPaddingException, | |
InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { | |
if (str == null) { | |
return null; | |
} | |
byte[] ciphertext = Base64.getDecoder().decode(str.getBytes()); | |
byte[] iv = Arrays.copyOfRange(ciphertext, 0, GCM_IV_LENGTH); | |
byte[] cipherText = Arrays.copyOfRange(ciphertext, GCM_IV_LENGTH, ciphertext.length); | |
SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); | |
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | |
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv); | |
cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec); | |
return new String(cipher.doFinal(cipherText)); | |
} | |
private static byte[] concatenate(byte[] firstArray, byte[] secondArray) { | |
byte[] result = Arrays.copyOf(firstArray, firstArray.length + secondArray.length); | |
System.arraycopy(secondArray, 0, result, firstArray.length, secondArray.length); | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Aes256UtilTest { | |
public static void main(String[] args) throws Exception { | |
byte[] key = "92c42d9dk49dhj3480gh280h08sb80f0".getBytes(); | |
for (int i = 0; i < 60; i++) { | |
String palinText = "가"; | |
for (int j = 0; j < i; j++) { | |
palinText += "가"; | |
} | |
String encrypt = Aes256Util.encryptByRandom(palinText, key); | |
String decyprt = Aes256Util.decrypt(encrypt, key); | |
System.out.println(encrypt); | |
System.out.println(decyprt); | |
// ValidateUtil.isTrue(palinText.equals(decyprt), decyprt); | |
} | |
} | |
} |
반응형
'java' 카테고리의 다른 글
BCrypt.java 암호화 소스 (1) | 2024.01.14 |
---|---|
아마존 리눅스 open jdk 17 설치 (2023-10-19) (0) | 2023.10.19 |
Cannot load from short array because "sun.awt.FontConfiguration.head" is null (0) | 2023.03.30 |
spring custom ErrorController (0) | 2022.11.16 |
jackson JsonView (Json Filter) 사용법. (0) | 2022.10.07 |
사용자 정의 JsonSerializer (0) | 2022.09.27 |
spring 최초 한번 실행. (0) | 2022.09.07 |
스프링부트 jackson response date 타입 변환 설정 (0) | 2022.09.02 |