これは私のコードです:
class Security {
public function __construct($textkey) {
$this->securekey = hash('sha256',$textkey,TRUE);
$this->iv = mcrypt_create_iv(32);
}
// holds properties for encrpyting
private $securekey, $iv;
/*
* encrypting data
*
* @param $data the value to be encrypted
* @return string the encrypted data
*/
public function encryptUrlData($input) {
$url = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input, MCRYPT_MODE_ECB, $this->iv));
return urlencode($url);
}
/*
* encrypting data
*
* @param $data the value to be encrypted
* @return string the encrypted data
*/
public function decryptUrlData($input) {
$input = urldecode($input);
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input), MCRYPT_MODE_ECB, $this->iv));
}
ID を暗号化しているため、基本的に暗号化する値は数字のみです。たとえば、値が「3」の ID は正常に復号化されますが、「1」は正常に復号化されません。