$CIPHERKEY = "비밀키값";
$data = "데이터 값";
echo Encryption::decrypt($data, $CIPHERKEY); //복호화 하기
<?php
//암호화 관련 클래스
class Encryption
{
//Encryption::decrypt($data, $CIPHERKEY);
########
//복호화 함수
static public function decrypt($data, $CIPHERKEY)
{
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
$key = substr($CIPHERKEY, 0, mcrypt_enc_get_key_size($cipher));
// Fake iv to call mcrypt_generic_init
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($cipher), MCRYPT_RAND);
//$cipherData = hex2bin($data);
$cipherData = $data;
if (mcrypt_generic_init($cipher, $key, $iv) != -1) {
$originalData = mdecrypt_generic($cipher, $cipherData);
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);
return $originalData;
} else {
return false;
}
}
###################################### ECB 암호화
function aes128_encode($data , $CIPHERKEY)
{
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($cipher), MCRYPT_RAND);
$key = substr($CIPHERKEY, 0, mcrypt_enc_get_key_size($cipher));
if (mcrypt_generic_init($cipher, $key, $iv) != 1) {
$cipherData = mcrypt_generic($cipher, $data);
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);
$sanitizedCipherData = trim(base64_encode($cipherData));
return $sanitizedCipherData;
} else {
return false;
}
}
function aes128_decode($data, $CIPHERKEY) # 복호화
{
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
$key = substr($CIPHERKEY, 0, mcrypt_enc_get_key_size($cipher));
// Fake iv to call mcrypt_generic_init
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($cipher), MCRYPT_RAND);
$cipherData = trim(base64_decode($data));
if (mcrypt_generic_init($cipher, $key, $iv) != -1) {
$originalData = mdecrypt_generic($cipher, $cipherData);
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);
return $originalData;
} else {
return false;
}
}
function hex2bin($hexstr)
{
$n = strlen($hexstr);
$sbin="";
$i=0;
while($i<$n)
{
$a =substr($hexstr,$i,2);
$c = pack("H*",$a);
if ($i==0){$sbin=$c;}
else {$sbin.=$c;}
$i+=2;
}
return $sbin;
}
}
?>