ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [php]128비트 암호화 클래스 <StringEncrypter>
    프로그램/php 2013. 9. 26. 01:54
    php용 128비트  암호화 클래스 StringEncrypter

     

    php의 기본 암호화 기능도 있지만 더 보안성이 좋은 128비트 암호화 클래스를 소개 하고자 합니다.

    MD5는 Hash의 충돌내성이 약하다고 알려져 있다고 합니다.

    일본에서는 표준알고리즘에서 제외되었다고 하네요.

    클래스와 선언 방법 그리고 간단한 사용 방법을 소스코드로 정리 해놓겠습니다.

     

     

    key = md5 ($key, TRUE) ;
                   $this->initialVector = md5 ($initialVector, TRUE) ;
              }
    
              /**
              * Encrypts a string.
              *
              * value: A string to encrypt. It must be a UTF-8 string.
              *        Null is regarded as an empty string.
              * return: An encrypted string.
              */
              public function encrypt ($value)
              {
                   if ( is_null ($value) )
                        $value = "" ;
    
                   if ( !is_string ($value) )
                        throw new Exception ("A non-string value can not be encrypted.") ;
    
    
                   // Append padding
                   $value = self::toPkcs7 ($value) ;
    
                   // Encrypt the value.
                   $output = mcrypt_encrypt (MCRYPT_RIJNDAEL_128, $this->key, $value, MCRYPT_MODE_CBC, $this->initialVector) ;
    
                   // Return a base64 encoded string of the encrypted value.
                   return base64_encode ($output) ;
              }
    
              /**
              * Decrypts a string which is encrypted with the same key and initial vector.
              *
              * value: A string to decrypt. It must be a string encrypted with the same key and initial vector.
              *        Null or an empty string is not allowed.
              * return: A decrypted string
              */
              public function decrypt ($value)
              {
                   if ( !is_string ($value) or $value == "" )
                        throw new Exception ("The cipher string must be a non-empty string.") ;
    
    
                   // Decode base64 encoding.
                   $value = base64_decode ($value) ;
    
                   // Decrypt the value.
                   $output = mcrypt_decrypt (MCRYPT_RIJNDAEL_128, $this->key, $value, MCRYPT_MODE_CBC, $this->initialVector) ;
    
                   // Strip padding and return.
                   return self::fromPkcs7 ($output) ;
              }
    
              /**
              * Encodes data according to the PKCS7 padding algorithm.
              *
              * value: A string to pad. It must be a UTF-8 string.
              *        Null is regarded as an empty string.
              * return: A padded string
              */
              private static function toPkcs7 ($value)
              {
                   if ( is_null ($value) )
                        $value = "" ;
    
                   if ( !is_string ($value) )
                        throw new Exception ("A non-string value can not be used to pad.") ;
    
    
                   // Get a number of bytes to pad.
                   $padSize = self::STRENCRYPTER_BLOCK_SIZE - (strlen ($value) % self::STRENCRYPTER_BLOCK_SIZE) ;
    
                   // Add padding and return.
                   return $value . str_repeat (chr ($padSize), $padSize) ;
              }
    
              /**
              * Decodes data according to the PKCS7 padding algorithm.
              *
              * value: A string to strip. It must be an encoded string by PKCS7.
              *        Null or an empty string is not allowed.
              * return: A stripped string
              */
              private static function fromPkcs7 ($value)
              {
                   if ( !is_string ($value) or $value == "" )
                        throw new Exception ("The string padded by PKCS7 must be a non-empty string.") ;
    
                   $valueLen = strlen ($value) ;
    
                   // The length of the string must be a multiple of block size.
                   if ( $valueLen % self::STRENCRYPTER_BLOCK_SIZE > 0 )
                        throw new Exception ("The length of the string is not a multiple of block size.") ;
    
    
                   // Get the padding size.
                   $padSize = ord ($value{$valueLen - 1}) ;
    
                   // The padding size must be a number greater than 0 and less equal than the block size.
                   if ( ($padSize < 1) or ($padSize > self::STRENCRYPTER_BLOCK_SIZE) )
                        throw new Exception ("The padding size must be a number greater than 0 and less equal than the block size.") ;
    
                   // Check padding.
                   for ($i = 0; $i < $padSize; $i++)
                   {
                        if ( ord ($value{$valueLen - $i - 1}) != $padSize )
                             throw new Exception ("A padded value is not valid.") ;
                   }
    
                   // Strip padding and return.
                   return substr ($value, 0, $valueLen - $padSize) ;
              }
         }
    ?>
    

     

    사용방법 입니다.

    간단하게 클래스를 선언해서 함수를 호출 하여 사용하시면 됩니다.

         include $_SERVER[DOCUMENT_ROOT]."/StringEncrypter.php";
         define ("KEY", "TMT8250") ;
         define ("IV", "TMT8250") ;
    
         $encrypter = new StringEncrypter (KEY, IV) ;
        
         $enc_ssn = $encrypter->encrypt ($m_ss1 . $m_ss2) ;
    
    
         $m_pw     = $encrypter->encrypt($m_pw);
    

    댓글

Designed by Tistory.