관리 메뉴

피터의 개발이야기

[JAVA] 양방향 암호화기법 AES_ENCRYPT, AES_DECRPYT 본문

Programming/JAVA

[JAVA] 양방향 암호화기법 AES_ENCRYPT, AES_DECRPYT

기록하는 백앤드개발자 2021. 1. 2. 08:00
반응형

데이터의 암호화를 위해 개발하였던 것을 정리하도록 하겠습니다.


CryptUtil

package com.peterica.swagger.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import org.apache.commons.codec.binary.Hex;

@Component
@Slf4j
public class CryptUtil {
    private static String db_secret_key="암호화키";

    // 암호화
    public String generateEncryptedKey(String passKey) {
        String strKey = getSHA512();
        try {

            final Cipher encryptCipher = Cipher.getInstance("AES");
            encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(strKey, "UTF-8"));
            return new String(Hex.encodeHex(encryptCipher.doFinal(passKey.getBytes("UTF-8")))).toUpperCase();
        } catch (Exception e) {
            log.error("Encrypted Key Error", e);
            return null;
        }
    }

    // 복호화
    public static String generateKeyDecrypt(String passwordhex) {
        String strKey = getSHA512();
        try {
            final Cipher decryptCipher = Cipher.getInstance("AES");
            decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(strKey, "UTF-8"));
            return new String(decryptCipher.doFinal(Hex.decodeHex(passwordhex.toCharArray())));
        } catch (Exception e) {
            log.error("Key Decrypted Error", e);
            return null;
        }
    }

    private static SecretKeySpec generateMySQLAESKey(final String key, final String encoding) {
        try {
            final byte[] finalKey = new byte[16];
            int i = 0;
            for(byte b : key.getBytes(encoding))
                finalKey[i++%16] ^= b;
            return new SecretKeySpec(finalKey, "AES");
        } catch(UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    // SHA512
    private static String getSHA512() {
        String toReturn = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-512");
            digest.reset();
            digest.update(db_secret_key.getBytes("utf8"));
            toReturn = String.format("%0128x", new BigInteger(1, digest.digest()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toReturn;
    }
}

 

Test

    @Test
    void cryptEncTest(){
        String encStr = cryptUtil.generateEncryptedKey("암호할 대상");
        System.out.println(encStr);
        // 1243B4EE1CEB166505A52813487E1D325BE39F417D10D57A07A102DE7F01E0DB
    }

    @Test
    void cryptDecTest(){
        System.out.println(cryptUtil.generateKeyDecrypt("1243B4EE1CEB166505A52813487E1D325BE39F417D10D57A07A102DE7F01E0DB"));
        // 암호화 대상
    }

 

 

 

Mysql AES_ENCRYPT

select HEX(AES_ENCRYPT('암호할 대상', SHA2('암호화키', 512))) encrypt;

 

Mysql AEC_DECRYPT

select AES_DECRYPT(unhex('1243B4EE1CEB166505A52813487E1D325BE39F417D10D57A07A102DE7F01E0DB'), SHA2('암호화키', 512)) AS text;

 

소스는 여기에 있습니다.

반응형
Comments