XOR加密--PHP版
来源:互联网 发布:淘宝怎么更改支付宝 编辑:程序博客网 时间:2024/05/18 07:44
关于XOR加密的概念可以参考 阮一峰老师的博客:http://www.ruanyifeng.com/blog/2017/05/xor.html
下面给出代码实例
<?phpclass XorEncrypt{private static $key = 'XorEncryptKey';private static $randomStrLen = 3;public static function encrypt($message,$key = ''){$key = $key == '' ? self::$key : $key;$messageLen = strlen($message);$cipherText = '';$keyLen = strlen($key);for($i=0;$i<=$messageLen;$i+=$keyLen){$cipherText .= substr($message,$i,$keyLen) ^ $key;}//加入随机字符$randomStr = self::generateRandomStr();return base64_encode($randomStr.$cipherText);}public static function decrypt($cipherText,$key= ''){$key = $key == '' ? self::$key : $key;$cipherText = substr(base64_decode($cipherText),self::$randomStrLen);$cipherTextLen = strlen($cipherText);$message = '';$keyLen = strlen($key);for($i=0;$i<=$cipherTextLen;$i+=$keyLen){$message .= substr($cipherText,$i,$keyLen) ^ $key;}return $message;}private static function generateRandomStr(){$randomStr = '';for($i=1;$i<=self::$randomStrLen;$i++){$randomStr .= chr(mt_rand(33, 126));}return $randomStr;}}$message = "加密字符串";$cipherText = XorEncrypt::encrypt($message);echo "加密后:".$cipherText.PHP_EOL;$message = XorEncrypt::decrypt($cipherText);echo "加密后:".$message.PHP_EOL;
需要注意的是 做XOR运算时,两个字符串需长度相等,否则会截断过长的部分。
即:key的长度大于等于message。
阅读全文
0 0
- XOR加密--PHP版
- XOR加密
- php 使用异或(XOR)加密/解密文件
- php 使用异或(XOR)加密/解密文件
- XOR 加密解密算法
- XOR加密初识
- Xor动态加密
- XOR 加密简介
- XOR 加密简介
- xor算法加密解密功能
- 基于XOR的加密程序
- XOR 异或加密简介
- C语言加密数据(xor加密)
- php版 tea 加密
- 利用xor特点进行简单加密解密
- 异或(XOR)运算加密/解密算法
- 加密101-异或(xor)
- XOR (异或)加密简单实现
- 前端性能优化+CDN优化
- datagrid控制分页js代码
- JAVA设计模式之单例模式
- Solr的安装及配置
- 极光推送服务器端代码(java服务器后台向手机端自定义推送消息)
- XOR加密--PHP版
- SpringMVC的Controller接口方法参数解析
- 编辑页面日期后显示多个0
- list,tuple,dict,set的区别和用法
- webrtc 服务器搭建(1)apprtc房间服务器
- MFC的消息机制
- swing组件功能与布局
- 【技术文档】jeecg3.7.1-maven版本搭建环境手把手入门-eclipse
- MySQL索引背后的数据结构及算法原理