Posts PHP와 MySQL 간 AES 암호화
Post
Cancel

PHP와 MySQL 간 AES 암호화

php 예시

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
class AES {  
        
	function encrypt(string $message, string $key, string $iv)   
	{  
		$encoding = 'UTF-8';  
	        $chiperAlgorithm = 'aes-256-cbc';  
        
		$utf8Key = mb_convert_encoding($key, $encoding, mb_detect_encoding($key));  
		$hashedKey = hash('sha256', $key);  
		$bytesKey= hex2bin($hashedKey);  
        
      	$utf8Message = mb_convert_encoding($message, $encoding, mb_detect_encoding($message));  
	        $utf8Iv = mb_convert_encoding($iv, $encoding, mb_detect_encoding($iv));  
        
		$encrypted = openssl_encrypt($utf8Message, $chiperAlgorithm, $bytesKey, OPENSSL_RAW_DATA, $utf8Iv);  
        
		return strtoupper(bin2hex($encrypted));  
	}  
        
	function decrypt(string $encrypted, string $key, string $iv)  
	{  
		$encoding = 'UTF-8';  
		$chiperAlgorithm = 'aes-256-cbc';  
        
		$utf8Key = mb_convert_encoding($key, $encoding, mb_detect_encoding($key));  
		$hashedKey = hash('sha256', $key);  
		$bytesKey= hex2bin($hashedKey);  
        
		$utf8Iv = mb_convert_encoding($iv, $encoding, mb_detect_encoding($iv));  
        
		return openssl_decrypt(hex2bin($encrypted), $chiperAlgorithm, $bytesKey, OPENSSL_RAW_DATA, $utf8Iv);  
	}  
}  

주의사항

  • openssl_encrypt 함수에 OPENSSL_RAW_DATA 옵션을 주지 않으면
    base64로 인코딩되어서 암호화된 문자열이 리턴된다.
  • openssl_decrypt 함수에 OPENSSL_RAW_DATA 옵션을 주지 않으면
    입력 받은 문자열이 base64 인코인되었다고 가정하고 처리한다.
This post is licensed under CC BY 4.0 by the author.