거누의 개발노트

Java AES128 양방향 암호화 (MySQL/JAVA) [개발노트] 본문

Java

Java AES128 양방향 암호화 (MySQL/JAVA) [개발노트]

Gogozzi 2020. 12. 30. 11:00
반응형

AES128은 양방향 암호화로 대칭키를 가지고 암호화, 복호화하는 방식이다.

 

1. JAVA에서 암/복호화할 때

2. MySQL에서 AES128 암/복호화한 것을 JAVA에서 비교해야할 때(동기화)

두가지 버전으로 설명해 보겠슴다.

 

 

👉 1번 JAVA에서 암/복호화

package com.common;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
 
public class AES128 {
	private String ips;
    private Key keySpec;
	
	public AES128(String key) {
        try {
            byte[] keyBytes = new byte[16];
            byte[] b = key.getBytes("UTF-8");
            System.arraycopy(b, 0, keyBytes, 0, keyBytes.length);
            SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
            this.ips = key.substring(0, 16);
            this.keySpec = keySpec;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public String encrypt(String str) {
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec,
                    new IvParameterSpec(ips.getBytes()));
 
            byte[] encrypted = cipher.doFinal(str.getBytes("UTF-8"));
            String Str = new String(Base64.encodeBase64(encrypted));
 
            return Str;
        } catch (Exception e) {
        	return null;
        }
    }
 
    public String decrypt(String str) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec,
                    new IvParameterSpec(ips.getBytes("UTF-8")));
 
            byte[] byteStr = Base64.decodeBase64(str.getBytes());
            String Str = new String(cipher.doFinal(byteStr), "UTF-8");
 
            return Str;
        } catch (Exception e) {
            return null;
        }
    }
}

 

👉 암호화 / 복호화

package com.common;

public class Encryption {
    
	public static void main(String[] args) throws Exception{
        
		String key = "This is Key!!!!!";
		
		AES128 aes = new AES128(key);
		
		
		String txt = "Hello World";
		String encrypt = aes.encrypt(txt);
		String decrypt = aes.decrypt(encrypt); 
		
		
		System.out.println("평문 : " + txt);
		System.out.println("암호화 : " + encrypt);
		System.out.println("복호화 : " + decrypt);
    	
    }
    
}

 

👉 결과

평문 : Hello World
암호화 : 3crpg0RloHjMMfBE/i38gQ==
복호화 : Hello World

 

=======================구분=======================

 

 

👉 2. MySQL에서 AES128 암/복호화한 것을 JAVA에서 비교해야할 때(동기화)

package com.common;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Hex;
 
public class AES128 {
	
	//MySQL Encrypt
    public String MySQLEncrypt(String str, String key) throws Exception {
    	final Cipher encryptCipher = Cipher.getInstance("AES");                            
        encryptCipher.init(Cipher.ENCRYPT_MODE, MySQLSetKey(key, "UTF-8"));        
        return new String(Hex.encodeHex(encryptCipher.doFinal(str.getBytes("UTF-8")))).toUpperCase();
    }
     
    
    //MySQL Decrypt
    public String MySQLDecrypt(String str, String key) throws Exception {
    final Cipher decryptCipher = Cipher.getInstance("AES");                            
    decryptCipher.init(Cipher.DECRYPT_MODE, MySQLSetKey(key, "UTF-8"));
    return new String(decryptCipher.doFinal(Hex.decodeHex(str.toCharArray())));
    
    }
    
    //key Setting
    public static SecretKeySpec MySQLSetKey(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(Exception e) {
            throw new RuntimeException(e);
        }
    }
}

 

👉 암호화/ 복호화

package com.common;

public class Encryption {
    
	public static void main(String[] args) throws Exception{
        
		String key = "This is Key!!!!!";
		
		AES128 aes = new AES128();
		
		
		String txt = "Hello World";
		String encrypt = aes.MySQLEncrypt(txt, key);
		String decrypt = aes.MySQLDecrypt(encrypt, key);
		
		
		System.out.println("평문 : " + txt);
		System.out.println("암호화 : " + encrypt);
		System.out.println("복호화 : " + decrypt);
    	
    }
    
}

 

👉 결과

평문 : Hello World
암호화 : D9676DB88DD40CBFFA277516A81CCA52
복호화 : Hello World

 

끝~

 

썸네일

 

반응형
Comments