본문 바로가기

프로그래밍/PHP

php 암호화 하기 aes-128, aes-256


// PHP 5.3.0 부터 openssl_encrypt 사용가능
// 상수정의
// 키
define('KEY', '12312332423412342354353452342343'); //128bit (16자리)

define('KEY_128', substr(KEY, 0, 128 / 8)); //256bit (32자리)

define('KEY_256', substr(KEY, 0, 256 / 8));

$str1 = '암호화 텍스트 확인';
echo 'plain : ' . $str1 . '<br>';

$str2 = openssl_encrypt($str1, 'AES-128-CBC', KEY_128, 0, KEY_128);
echo 'AES128 encrypted : ' . $str2 . '<br>';

$str3 = openssl_decrypt($str2, 'AES-128-CBC', KEY_128, 0, KEY_128);
echo 'AES128 decrypted : ' . $str3 . '<br>';

$str4 = openssl_encrypt($str1, 'AES-256-CBC', KEY_256, 0, KEY_128);
echo 'AES256 encrypted : ' . $str4 . '<br>';

$str5 = openssl_decrypt($str4, 'AES-256-CBC', KEY_256, 0, KEY_128);
echo 'AES256 decrypted : ' . $str5 . '<br>';

$str6 = base64_encode(hash('SHA256', $str1, true));
echo 'SHA256 encrypted : ' . $str6 . '<br><br><br>';