Skip to content

Instantly share code, notes, and snippets.

@rongself
Created December 4, 2014 10:16
Show Gist options
  • Save rongself/49fa6292a58fdf5b74f6 to your computer and use it in GitHub Desktop.
Save rongself/49fa6292a58fdf5b74f6 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 14-12-4
* Time: 下午1:44
*/
class ParamsEncryptor {
public static $cipher = MCRYPT_DES;
public static $modes = MCRYPT_MODE_ECB;
public static $key = '0d6cd47df56b081b78566ee4facbcf2254b';
public static function encode(array $params)
{
$string = json_encode($params);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(self::$cipher,self::$modes),MCRYPT_RAND);
return urlencode(mcrypt_encrypt(self::$cipher,self::$key,$string,self::$modes,$iv));
}
public static function decode($paramsString)
{
$iv = mcrypt_create_iv(mcrypt_get_iv_size(self::$cipher,self::$modes),MCRYPT_RAND);
$paramsString = urldecode($paramsString);
$decodedString = mcrypt_decrypt(self::$cipher,self::$key,$paramsString,self::$modes,$iv);
return json_decode(rtrim($decodedString,"\0"),true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment