MYSQL에서 마이그레이션 도중에 AES_ENCRYPT, AES_DECRYPT를 사용 한 컬럼 때문에 고민

google 신이 도와줌..

아 어짜피 MYSQL 이제 오라클꺼니까..  서로 호환 되는건가..






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.GregorianCalendar;
 
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
 
import org.apache.commons.codec.binary.Hex; 
 
public class AesUtil {
    public static void main(String[] args) throws Exception{
        // Encrypt
        final Cipher encryptCipher = Cipher.getInstance("AES");                            
        encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey("암호키""UTF-8"));        
        System.out.println(new String(Hex.encodeHex(encryptCipher.doFinal("암호화 될 변수".getBytes("UTF-8")))).toUpperCase()); 
        
        // Decrypt
        final Cipher decryptCipher = Cipher.getInstance("AES");                            
        decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey("암호키""UTF-8"));
        System.out.println(new String(decryptCipher.doFinal(Hex.decodeHex("33362642F191A6FA40E6F2341001D969AE9640831F2314DD7B18832DA8A077B0".toCharArray()))));
    }
    
    public 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);
        }
    }
}
 
 
cs


Posted by Swamp of hope
,