본문 바로가기
  • A space that records me :)
Language/JAVA

[JAVA] AES256, SHA256 - 암호화 복호화

by yjkim_97 2020. 11. 30.

base64 메이븐 추가

Apache Commons에서 저 두개를 하면 전체 다 import인가?

<dependency>
	<groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.13</version>
</dependency>




<!-- Apache Commons -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
</dependency>

SHA256 - 암호화

  • SHA256은 암호화만 가능하며 복호화할 수 없다. (암호화 키가 없기 때문)
  • 256bit의 hash 값이다.
public static String encryptSHA256(String value) throws NoSuchAlgorithmException{
  StringBuffer encryptData = new StringBuffer();
  MessageDigest sha = MessageDigest.getInstance("SHA-256");

  sha.update(value.getBytes());

  byte[] digest = sha.digest();

  for (int i=0; i<digest.length; i++) {
  	encryptData.append(String.format("%02x", digest[i] & 0xFF));
  }

  return encryptData.toString();
}

AES256 - 암/복호화

  • 대칭키 암호화 알고리즘 (암호화,복호화 키가 동일)
  • 16자리의 암호화 키가 필요하다. (16자리 이하인경우 UnsupportedEncodingException 발생)
  • 국내에서 지원하는 자바 정책은 AES128까지 이므로 jar를 추가해줘야 한다.
local_policy.jar
US_export_policy.jar

 

암호화 key 생성

/* AES_128_CBC Key 생성 함수 */
	public static Key getAESKey()
	{
		Key keySpec = null;
		try {
	
			String key = "키 값";
			byte[] keyBytes = new byte[16];
			byte[] b;
				b = key.getBytes("UTF-8");
	
			int len = b.length;
			if (len > keyBytes.length)
			{
				len = keyBytes.length;
			}
	
			System.arraycopy(b, 0, keyBytes, 0, len);
			keySpec = new SecretKeySpec(keyBytes, "AES");
	
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return keySpec;
	}

 

암호화

// 암호화
	public static String encryptAES256(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,
		NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
		IllegalBlockSizeException, BadPaddingException 
	{
		Key keySpec = getAESKey();
		Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
		c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));
	
		byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
		String enStr = new String(Base64.encodeBase64(encrypted));
	
		return enStr;
	}

 

복호화

//복호화
	public static String decryptAES256(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,
		NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
		IllegalBlockSizeException, BadPaddingException 
	{
		Key keySpec = getAESKey();
		Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
		c.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes("UTF-8")));
	
		byte[] byteStr = Base64.decodeBase64(str.getBytes());
	
		return new String(c.doFinal(byteStr),"UTF-8");
	}

 

 

전체 소스

package com.kt.tbb.iptv.coupon.framework.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class AES256 {
	public static final String iv = "iv 키값"; 

	/* AES_128_CBC Key 생성 함수 */
	public static Key getAESKey()
	{
		Key keySpec = null;
		try {
	
			String key = "키 값";
			byte[] keyBytes = new byte[16];
			byte[] b;
				b = key.getBytes("UTF-8");
	
			int len = b.length;
			if (len > keyBytes.length)
			{
				len = keyBytes.length;
			}
	
			System.arraycopy(b, 0, keyBytes, 0, len);
			keySpec = new SecretKeySpec(keyBytes, "AES");
	
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return keySpec;
	}
	
	// 암호화
	public static String encryptAES256(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,
		NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
		IllegalBlockSizeException, BadPaddingException 
	{
		Key keySpec = getAESKey();
		Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
		c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));
	
		byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
		String enStr = new String(Base64.encodeBase64(encrypted));
	
		return enStr;
	}

	//복호화
	public static String decryptAES256(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,
		NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
		IllegalBlockSizeException, BadPaddingException 
	{
		Key keySpec = getAESKey();
		Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
		c.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes("UTF-8")));
	
		byte[] byteStr = Base64.decodeBase64(str.getBytes());
	
		return new String(c.doFinal(byteStr),"UTF-8");
	}
}