巅云智能建站平台搭建版(创业门户版)火爆上线,终身授权!新增:文章智能采集+全站真静态打包+城市分站+智能小程序+非法词过滤+H5自适应+智能链词等功能功能详情
建站专提News

PHP和.net中des加解密的实现方法

一佰互联网站开发设计(www.yinxi.net) 发布日期 2019-04-30 09:21:35 浏览数: 97

php5.x版本,要添加php扩展php_mcrypt。

PHP版:

复制代码 代码如下:
class STD3Des
 {
     private $key = "";
     private $iv = "";     /**
     * 构造,传递二个已经进行base64_encode的KEY与IV
     *
     * @param string $key
     * @param string $iv
     */
     function __construct ($key, $iv)
     {
         if (empty($key) || empty($iv)) {
             echo "key and iv is not valid";
             exit();
         }
         $this->key = $key;
         $this->iv = $iv;
     }     /**
     *加密
     * @param <type> $value
     * @return <type>
     */
     public function encrypt ($value)
     {
         $td = mcrypt_module_open(MCRYPT_3DES, "", MCRYPT_MODE_CBC, "");
         $iv = base64_decode($this->iv);
         $value = $this->PaddingPKCS7($value);
         $key = base64_decode($this->key);
         mcrypt_generic_init($td, $key, $iv);
         $ret = base64_encode(mcrypt_generic($td, $value));
         mcrypt_generic_deinit($td);
         mcrypt_module_close($td);
         return $ret;
     }     /**
     *解密
     * @param <type> $value
     * @return <type>
     */
     public function decrypt ($value)
     {
         $td = mcrypt_module_open(MCRYPT_3DES, "", MCRYPT_MODE_CBC, "");
         $iv = base64_decode($this->iv);
         $key = base64_decode($this->key);
         mcrypt_generic_init($td, $key, $iv);
         $ret = trim(mdecrypt_generic($td, base64_decode($value)));
         $ret = $this->UnPaddingPKCS7($ret);
         mcrypt_generic_deinit($td);
         mcrypt_module_close($td);
         return $ret;
     }     private function PaddingPKCS7 ($data)
     {
         $block_size = mcrypt_get_block_size("tripledes", "cbc");
         $padding_char = $block_size - (strlen($data) % $block_size);
         $data .= str_repeat(chr($padding_char), $padding_char);
         return $data;
     }     private function UnPaddingPKCS7($text)
     {
         $pad = ord($text{strlen($text) - 1});
         if ($pad > strlen($text)) {
             return false;
         }
         if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
             return false;
         }
         return substr($text, 0, - 1 * $pad);
     }
 } 
 //使用
 include("STD3Des.class.php");
 $key="abcdefgh";
 $iv="abcdefgh";
 $msg="test string";
 $des=new STD3Des(base64_encode($key),base64_encode($iv));
 $rs1=$des->encrypt($msg);
 echo $rs1."<br />";
 $rs2=$des->decrypt($rs1);
 echo $rs2;

.net版本

复制代码 代码如下:
sealed public class CryptoHelper
 {
     /// <summary>
     /// Encrypts the specified input.
     /// </summary>
     /// <param name="input">The input.</param>
     /// <param name="key">key</param>
     /// <param name="iv">iv</param>
     /// <returns></returns>
     public static string EncryptDes(string input, byte[] key, byte[] iv)
     {
         if (input == null || input.Length == 0)
             return String.Empty;         DESCryptoServiceProvider des = new DESCryptoServiceProvider();
         MemoryStream ms = null;
         CryptoStream encStream = null;
         StreamWriter sw = null;
         string result = String.Empty;         try
         {
             ms = new MemoryStream();             // Create a CryptoStream using the memory stream and the
             // CSP DES key. 
             //des.Mode = CipherMode.CBC;
             //des.Padding = PaddingMode.PKCS7; 
             encStream = new CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write);             // Create a StreamWriter to write a string
             // to the stream.
             sw = new StreamWriter(encStream);             // Write the plaintext to the stream.
             sw.Write(input);             sw.Flush();
             encStream.FlushFinalBlock();
             ms.Flush(); 
             result = Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length, CultureInfo.InvariantCulture));
         }
         finally
         {
             //close objects
             if (sw != null)
                 sw.Close();
             if (encStream != null)
                 encStream.Close();
             if (ms != null)
                 ms.Close();
         }         // Return the encrypted string
         return result;
     }
     /// <summary>
     /// Decrypts the specified input.
     /// </summary>
     /// <param name="input">the input.</param>
     /// <param name="key">key</param>
     /// <param name="iv">iv</param>
     /// <returns></returns>
     public static string DecryptDes(string input, byte[] key, byte[] iv)
     {
         byte[] buffer;
         try { buffer = Convert.FromBase64String(input); }
         catch (System.ArgumentNullException) { return String.Empty; }
         // length is zero, or not an even multiple of four (plus a few other cases)
         catch (System.FormatException) { return String.Empty; }         DESCryptoServiceProvider des = new DESCryptoServiceProvider();
         MemoryStream ms = null;
         CryptoStream encStream = null;
         StreamReader sr = null;
         string result = String.Empty;         try
         {
             ms = new MemoryStream(buffer);             // Create a CryptoStream using the memory stream and the
             // CSP DES key.
             encStream = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);             // Create a StreamReader for reading the stream.
             sr = new StreamReader(encStream);             // Read the stream as a string.
             result = sr.ReadToEnd();
         }
         finally
         {
             //close objects
             if (sr != null)
                 sr.Close();
             if (encStream != null)
                 encStream.Close();
             if (ms != null)
                 ms.Close();
         }         return result;
     }
 } 
 //调用 string key = "abcdefgh";
 string iv = "abcdefgh";
 string msg="test string";
 string rs1 = CryptoHelper.EncryptDes(msg, System.Text.Encoding.ASCII.GetBytes(key), System.Text.Encoding.ASCII.GetBytes(iv));
 string rs2 = CryptoHelper.DecryptDes(rs1, System.Text.Encoding.ASCII.GetBytes(key), System.Text.Encoding.ASCII.GetBytes(iv));

一佰互联是全国知名建站品牌服务商,我们有九年网站建设、网站制作、网页设计、php开发和域名注册及虚拟主机服务经验,提供的自助建站服务更是全国有名。近年来还整合团队优势自主开发了可视化多用户”巅云建站系统“3.0平台版,拖拽排版网站制作设计,轻松实现pc站、手机微网站、小程序、APP一体化全网营销网站建设 ,已成功的为全国上百家网络公司提供自助建站平台搭建服务。

相关新闻more

30
04月
php file_put_contents(

命令:file_put_contents(); 命令解析:file_put_contents (PHP 5) file_put_conten... >>详情

01
05月
php通用检测函数集合第1/3页

<? //【警告】:未经许可请勿随便修改 //--------------------------------------------... >>详情

14
04月
为什么要选择白帽做优化

在搜索引擎优化业界,人们把使用作弊手段的称为黑帽,使用正当合法手段优化网站的称为白帽。刚才在Google里查了一下黑帽SEO,竟然只有两百多... >>详情

27
04月
PHP基于cookie与session统计网

本文实例讲述了PHP基于cookie与session统计网站访问量并输出显示的方法。分享给大家供大家参考,具体如下:<?php... >>详情

高端网站建设

美工兼顾SEO,为企业电子商务营销助力!

电话:

023-85725751
建站

产品

域名注册 虚拟主机 云服务器 企业邮局
智能建站 APP打包 微站/小程序 创业平台
网站推广 媒体营销 智能采集 AI机器人
400电话 短信营销 店销机器人
私人定制 门户网站