PHP에서 AES, SHA 암호화가 필요하게 되었습니다.
회사에 있는 CentOS에 PHP 설치하고 진행해보았습니다.
역시.. PHP는 간단하네요..
PHP의 버전이 5.3.0 이상이면 아래와 같이 하면 됩니다.
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 | <?php //PHP 5.3.0 부터 openssl_encrypt 사용가능 //상수정의 //키 define('KEY', '01234567890123456789012345678901'); //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>'; ?> | cs |
PHP의 버전이 5.3.0 미만이라면 mcrypt 모듈을 설치하여 진행하면 됩니다.
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | <?php //mcrypt 모듈 설치 필요 //상수정의 //키 define('KEY', '01234567890123456789012345678901'); //128bit (16자리) define('KEY_128', substr(KEY, 0, 128 / 8)); //256bit (32자리) define('KEY_256', substr(KEY, 0, 256 / 8)); //패딩 추가 function pkcs7padding($data) { $padding = 16 - strlen($data) % 16; $padding_text = str_repeat(chr($padding), $padding); return $data . $padding_text; } //패딩 제거 function pkcs7unPadding($data) { $length = strlen($data); $unpadding = ord($data[$length - 1]); return substr($data, 0, $length - $unpadding); } //AES 128 암호화 function encryptAES128($string) { $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, KEY_128, pkcs7padding($string), MCRYPT_MODE_CBC, KEY_128); $base64Encoded = base64_encode($encrypted); return $base64Encoded; } //AES 128 복호화 function decryptAES128($string) { $base64Decoded = base64_decode($string); $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, KEY_128, $base64Decoded, MCRYPT_MODE_CBC, KEY_128); return pkcs7unPadding($decrypted); } //AES 256 암호화 function encryptAES256($string) { $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, KEY_256, pkcs7padding($string), MCRYPT_MODE_CBC, KEY_128); $base64Encoded = base64_encode($encrypted); return $base64Encoded; } //AES 256 복호화 function decryptAES256($string) { $base64Decoded = base64_decode($string); $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, KEY_256, $base64Decoded, MCRYPT_MODE_CBC, KEY_128); return pkcs7unPadding($decrypted); } //SHA 256 암호화 function encryptSHA256($string) { $encrypted = hash('SHA256', $string, true); $base64Encoded = base64_encode($encrypted); return $base64Encoded; } $str1 = '암호화되지 않은 문자'; echo 'plain : ' . $str1 . '<br>'; $str2 = encryptAES128($str1); echo 'AES128 encrypted : ' . $str2 . '<br>'; $str3 = decryptAES128($str2); echo 'AES128 decrypted : ' . $str3 . '<br>'; $str4 = encryptAES256($str1); echo 'AES256 encrypted : ' . $str4 . '<br>'; $str5 = decryptAES256($str4); echo 'AES256 decrypted : ' . $str5 . '<br>'; $str6 = encryptSHA256($str1); echo 'SHA256 encrypted : ' . $str6 . '<br>'; ?> | cs |
실행결과
'others' 카테고리의 다른 글
Environment Variables in create-react-app (0) | 2020.05.21 |
---|---|
Cloud SQL in GCP (0) | 2020.05.14 |
AES, SHA 암호화 5, Swift (0) | 2016.04.21 |
AES, SHA 암호화 4, T-SQL (0) | 2015.08.26 |
AES, SHA 암호화 3, C# (6) | 2015.08.26 |