mcrypt_decrypt并不总是返回正确的文本

时间:2013-10-11 16:33:48

标签: php encryption

这是我的代码:

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”未成功解密。

0 个答案:

没有答案