Spring-JSP

[Spring-JSP] AES-256 암호화

Jeong Jeon
반응형

AES256을 사용하여 DB에 등록될 사용자 정보를 암/복호화 하는 방법이다.

우리는 보통 사용자의 중요정보(개인정보)를 DataBase에 관리를 하게된다.

이때 암호화를 하지 않은 상태로 관리를 하게 된다면, DataBase 정보 탈취의 우려가 있을 가능성이 있다.

탈취자와 보안을 위해 한번이라도 더 귀찮게 만들어주자.

 

1). AES256SecureUtil.java

  • 모듈화해서 사용할 수 있도록 만들어두고 사용하자.

 

public class AES256SecureUtil { 
    private static String key = "원하는비밀Key^$%^"; 
    private String iv; 
    private Key keySpec; 
    /** 
     * @desc 초기화 생성자 함수 전역에 설정된 key 값으로 필요 변수 값들 초기화 
     * @throws UnsupportedEncodingException 
     */ 
    public AES256SecureUtil() throws UnsupportedEncodingException { 
        this.iv = AES256SecureUtil.key.substring(0, 16); 
        byte[] keyBytes = new byte[16]; 
        byte[] b = AES256SecureUtil.key.getBytes("UTF-8"); 
        int len = b.length; 
        if (len > keyBytes.length) { 
            len = keyBytes.length; 
        } 
        System.arraycopy(b, 0, keyBytes, 0, len); 
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); 
        this.keySpec = keySpec; 
    } 
    /** 
     * @desc 문자열을 전달받아 암호화하여 암호화된 문자열을 리턴한다. 
     * @param str 
     * @return 
     * @throws java.io.UnsupportedEncodingException 
     * @throws NoSuchAlgorithmException 
     * @throws NoSuchPaddingException 
     * @throws InvalidKeyException 
     * @throws InvalidAlgorithmParameterException 
     * @throws IllegalBlockSizeException 
     * @throws BadPaddingException 
     */ 
    public String aesEncode(String str) throws java.io.UnsupportedEncodingException, 
                                                    NoSuchAlgorithmException, 
                                                    NoSuchPaddingException, 
                                                    InvalidKeyException, 
                                                    InvalidAlgorithmParameterException, 
                                                    IllegalBlockSizeException, 
                                                    BadPaddingException { 
        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; 
    } 
    /** 
     * @desc 암호화된 문자열을 복호화 하여 리턴한다. 
     * @param str 
     * @return 
     * @throws java.io.UnsupportedEncodingException 
     * @throws NoSuchAlgorithmException 
     * @throws NoSuchPaddingException 
     * @throws InvalidKeyException 
     * @throws InvalidAlgorithmParameterException 
     * @throws IllegalBlockSizeException 
     * @throws BadPaddingException 
     */ 
    public String aesDecode(String str) throws java.io.UnsupportedEncodingException, 
                                                        NoSuchAlgorithmException, 
                                                        NoSuchPaddingException, 
                                                        InvalidKeyException, 
                                                        InvalidAlgorithmParameterException, 
                                                        IllegalBlockSizeException, 
                                                        BadPaddingException { 
        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"); 
    } 
}

암호화 사용예제

//이름, 전화번호, 이메일 암호화 
	String normalAccountTel = ""; 
	String normalAccountName = ""; 
	String normalAccountEmail = accountVo.getAccountEmail(); 
	String frontEmail = ""; 
	String backEmail = ""; 
	String[] arr = normalAccountEmail.split("@"); 
	frontEmail = arr[0]; 
	backEmail = arr[1]; 
	normalAccountEmail = frontEmail + "@" + backEmail; 

	AES256SecureUtil aes256SecureUtil = new AES256SecureUtil(); 

	normalAccountTel = aes256SecureUtil.aesEncode(accountVo.getAccountTel()); 
	normalAccountName = aes256SecureUtil.aesEncode(accountVo.getAccountName()); 
	normalAccountEmail = aes256SecureUtil.aesEncode(normalAccountEmail); 
	accountVo.setAccountTel(normalAccountTel); 
	accountVo.setAccountName (normalAccountName); 
	accountVo.setAccountEmail (normalAccountEmail);

복호화 사용예제

ArrayList<AccountVo> accountList = accountDao.selectAccountList(accountVo.setLang(locale.toString())); 
    for (int i = 0; i < accountList.size(); i++) {  
	accountList.get(i).setAccountTel(aes256SecureUtil.aesDecode((accountList.get(i).getAccountTel())));  
	accountList.get(i).setAccountName(aes256SecureUtil.aesDecode((accountList.get(i).getAccountName())));  
	accountList.get(i).setAccountEmail(aes256SecureUtil.aesDecode((accountList.get(i).getAccountEmail())));  
    }
반응형