Skip to content

Instantly share code, notes, and snippets.

@sanhuang
Created September 20, 2016 16:00
Show Gist options
  • Save sanhuang/601bb137a2abdb007fb9cb9b8dee7f01 to your computer and use it in GitHub Desktop.
Save sanhuang/601bb137a2abdb007fb9cb9b8dee7f01 to your computer and use it in GitHub Desktop.
PHP套用lib mcrypt做加解密處理方法

從pttnews專案轉移出

使用方式

直接呼叫**encrypt()**進行加密,**decrypt()**進行解密。

// pass-for-check-key
  public $checkval = 'K2Jstudio';
  // chi
  private $password = '2w3e4rr5t62q';

  function encrypt($decrypted, $key='2w3e4rr5t62q', $salt='!kQm*fF3pXe1Kbm%9') {
      // Build a 256-bit $key which is a SHA256 hash of $salt and $key.
      $key = hash('SHA256', $salt . $key, true);
      // Build $iv and $iv_base64.  We use a block size of 128 bits (AES compliant) and CBC mode.  (Note: ECB mode is inadequate as IV is not used.)
      srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
      //
      if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
      // Encrypt $decrypted and an MD5 of $decrypted using $key.  MD5 is fine to use here because it's just to verify successful decryption.
      $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
      // We're done!
      return $iv_base64 . $encrypted;
  }

  private function decrypt($encrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') {
      // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
      $key = hash('SHA256', $salt . $password, true);
      // Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
      $iv = base64_decode(substr($encrypted, 0, 22) . '==');
      // Remove $iv from $encrypted.
      $encrypted = substr($encrypted, 22);
      // Decrypt the data.  rtrim won't corrupt the data because the last 32 characters are the md5 hash; thus any \0 character has to be padding.
      $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4");
      // Retrieve $hash which is the last 32 characters of $decrypted.
      $hash = substr($decrypted, -32);
      // Remove the last 32 characters from $decrypted.
      $decrypted = substr($decrypted, 0, -32);
      // Integrity check.  If this fails, either the data is corrupted, or the password/salt was incorrect.
      if (md5($decrypted) != $hash) return false;
      // Yay!
      return $decrypted;
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment