2018/3/13 追記:
PHP7.1で非推奨になりました。OpenSSLを使ったこちらをご参照ください。
以下の方法はPHP7.1から非推奨となりますのでご注意ください。
シンプルに暗号化、復号化する関数です。
| 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 | /* 暗号化 */ function encrypt($value) {   $encodedValue = base64_encode($value);   $td = get_crypt_module();   $encrypted = mcrypt_generic($td, $encodedValue);   $encodedEncryptValue = base64_encode($encrypted);   mcrypt_generic_deinit($td);   mcrypt_module_close($td);   return $encodedEncryptValue; } /* 復号化 */ function decrypt($value) {   $decodedValue = base64_decode($value);   $td = get_crypt_module();   $decrypted = mdecrypt_generic($td, $decodedValue);   mcrypt_generic_deinit($td);   mcrypt_module_close($td);   $decodedDecryptValue = base64_decode($decrypted);   return $decodedDecryptValue; } /* 暗号化モジュール */ function get_crypt_module() {   /* 暗号化形式を指定してモジュールオープン */   /* DESなら MCRYPT_DES */   /* AES128なら MCRYPT_RIJNDAEL_128 */   $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');   $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);   $ks = mcrypt_enc_get_key_size($td);   /* 任意のキーを設定(文字数は暗号化形式に合わせてください) */   $key = 'hogehoge';   mcrypt_generic_init($td, $key, $iv);   return $td; } | 
