源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

php结合md5的加密解密算法实例

  • 时间:2022-01-21 09:11 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:php结合md5的加密解密算法实例
本文实例讲述了php结合md5的加密解密算法。分享给大家供大家参考,具体如下:
<?php
/*
* Created on 2016-9-30
*
*/
function encrypt($data, $key)
{
 $key = md5($key);
  $x = 0;
  $len = strlen($data);
  $l = strlen($key);
  for ($i = 0; $i < $len; $i++)
  {
    if ($x == $l)
    {
     $x = 0;
    }
    $char .= $key{$x};
    $x++;
  }
  for ($i = 0; $i < $len; $i++)
  {
    $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
  }
  return base64_encode($str);
}
function decrypt($data, $key)
{
 $key = md5($key);
  $x = 0;
  $data = base64_decode($data);
  $len = strlen($data);
  $l = strlen($key);
  for ($i = 0; $i < $len; $i++)
  {
    if ($x == $l)
    {
     $x = 0;
    }
    $char .= substr($key, $x, 1);
    $x++;
  }
  for ($i = 0; $i < $len; $i++)
  {
    if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1)))
    {
      $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
    }
    else
    {
      $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
    }
  }
  return $str;
}
$data = '编程素材网www.1sucai.cn'; // 被加密信息
$data=iconv("gbk","utf-8",$data);
$key = 'www.1sucai.cn';   // 密钥
$encrypt = encrypt($data, $key);
$decrypt = decrypt($encrypt, $key);
echo $encrypt, "<br/>", $decrypt;
?>

运行结果如下:
TrXMTM8SFB3DGhTr2qeuYqOXZmpmn8mo
编程素材网www.1sucai.cn

[b]PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:[/b] [b]密码安全性在线检测:[/b] [b][url=http://tools.jb51.net/password/my_password_safe]http://tools.jb51.net/password/my_password_safe[/url][/b] [b]高强度密码生成器:[/b] [url=http://tools.jb51.net/password/CreateStrongPassword][b]http://tools.jb51.net/password/CreateStrongPassword[/b][/url] [b]MD5在线加密工具:[/b] [url=http://tools.jb51.net/password/CreateMD5Password][b]http://tools.jb51.net/password/CreateMD5Password[/b][/url] [b]迅雷、快车、旋风URL加密/解密工具:[/b] [url=http://tools.jb51.net/password/urlrethunder][b]http://tools.jb51.net/password/urlrethunder[/b][/url] [b]在线散列/哈希算法加密工具:[/b] [url=http://tools.jb51.net/password/hash_encrypt][b]http://tools.jb51.net/password/hash_encrypt[/b][/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/144.htm]php加密方法总结[/url]》、《[url=http://www.1sucai.cn/Special/459.htm]PHP编码与转码操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/614.htm]PHP数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/111.htm]php程序设计算法总结[/url]》、《[url=http://www.1sucai.cn/Special/180.htm]php正则表达式用法总结[/url]》、及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部