반응형

 

encryptByRandom 은 iv가 random하게 생성되어서 같은 값을 암호화 하더라도 항상 다르게 나옵니다.
encryptByFix 는 iv가 key를 참조해 생성되어서 항상 같은 값으로 암호화 됩니다.

 

 decrypt는 앞의 16자리를 iv로 참조해서 복호화하므로 encrypt 방법과 무관하게 복호화 됩니다.

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;
}
}
view raw Aes256Util.java hosted with ❤ by GitHub
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);
}
}
}

 

반응형

+ Recent posts