AES ENCRYPT, AES DECRYPT

MYSQL COMPATIBLE AES ENCRYPTION / DECRYPTION
MySQL 호환 AES 암호화 / 복호화

1 PHP[ | ]

// patrickdk at patrickdk dot com - http://php.net/manual/en/ref.mcrypt.php
function AES_ENCRYPT($val,$ky) { 
        $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; 
        for($a=0;$a<strlen($ky);$a++) $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a])); 
        $mode = MCRYPT_MODE_ECB; 
        $enc = MCRYPT_RIJNDAEL_128; 
        $val = str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16))); 
        return mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM)); 
}
 
function AES_DECRYPT($val,$ky) { 
        $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; 
        for($a=0;$a<strlen($ky);$a++) $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a])); 
        $mode = MCRYPT_MODE_ECB; 
        $enc = MCRYPT_RIJNDAEL_128; 
        $dec = @mcrypt_decrypt($enc, $key, $val, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) ); 
        return rtrim($dec,(( ord(substr($dec,strlen($dec)-1,1))>=0 and ord(substr($dec, strlen($dec)-1,1))<=16)? chr(ord( substr($dec,strlen($dec)-1,1))):null)); 
}
 
echo md5(AES_ENCRYPT('hello world', 'key')).PHP_EOL;
// a4c366bf1ffb59be4c0be00f89166e4d
echo AES_DECRYPT(AES_ENCRYPT('hello world','key'), 'key').PHP_EOL;
// hello world

echo md5(AES_ENCRYPT('안녕 친구들', '비밀키')).PHP_EOL;
// 0a9fd16fae30dc9fa87515a7dbcf3422
echo AES_DECRYPT(AES_ENCRYPT('안녕 친구들','비밀키'), '비밀키').PHP_EOL;
// 안녕 친구들

2 Python[ | ]

from Crypto.Cipher import AES

def AES_ENCRYPT(msg, key):
        msg2 = 16 - (len(msg) % 16)
        msg2 = '%s%s' % (msg, chr(msg2)*msg2)
        key2 = bytearray(16)
        for i, c in enumerate(key): key2[i%16] ^= ord(key[i])
        return AES.new(bytes(key2), AES.MODE_ECB).encrypt(msg2)

def AES_DECRYPT(msg, key):
	key2 = bytearray(16)
        for i, c in enumerate(key): key2[i%16] ^= ord(key[i])
	dec = AES.new(bytes(key2), AES.MODE_ECB).decrypt(msg)
        return dec[:-ord(dec[len(dec) - 1])]

from hashlib import md5
print( md5(AES_ENCRYPT('hello world', 'key')).hexdigest() )
# a4c366bf1ffb59be4c0be00f89166e4d
print( AES_DECRYPT(AES_ENCRYPT('hello world', 'key'), 'key') )
# hello world

print( md5(AES_ENCRYPT('안녕 친구들', '비밀키')).hexdigest() )
# 803b80e0206a0faeb5ce179ec02e612c
print( AES_DECRYPT(AES_ENCRYPT('안녕 친구들', '비밀키'), '비밀키') )
# 안녕 친구들

3 SQL[ | ]

3.1 MySQL[ | ]

SELECT HEX( AES_ENCRYPT('hello world', 'key') );
-- EE21283CA34FC340C65234E030EFCCD8
SELECT MD5( AES_ENCRYPT( 'hello world', 'key' ) );
-- a4c366bf1ffb59be4c0be00f89166e4d
SELECT AES_DECRYPT(AES_ENCRYPT('hello world','key'), 'key');
-- hello world

SELECT HEX( AES_ENCRYPT('안녕하세요', 'key') );
-- 192A4EF5C139F411DBA75BEF0BFB2D22
SELECT MD5( AES_ENCRYPT('안녕하세요', 'key') );
-- 75af28bf601eb7142a85fa52c791190f
SELECT AES_DECRYPT(AES_ENCRYPT('안녕하세요', 'key'), 'key');
-- ec9588eb8595ed9598ec84b8ec9a94
SELECT CONVERT( AES_DECRYPT( AES_ENCRYPT('안녕하세요', 'key'), 'key') USING utf8);
-- 안녕하세요

SELECT HEX( AES_ENCRYPT('안녕하세요', '비밀키') );
-- 73813BCA89FDEB8B629C7C1D0E4924EE
SELECT MD5( AES_ENCRYPT('안녕하세요', '비밀키') );
-- ae2502ff273e98ed54f2935c6cf8e77b
SELECT AES_DECRYPT(AES_ENCRYPT('안녕하세요', '비밀키'), '비밀키');
-- ec9588eb8595ed9598ec84b8ec9a94
SELECT CONVERT( AES_DECRYPT( AES_ENCRYPT('안녕하세요', '비밀키'), '비밀키') USING utf8);
-- 안녕하세요

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}