当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP与asp.net C#可共用的可逆加密算法

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-27 16:36:08 浏览: 评论:0 

因为工作需我们需要在php中生成加密然后在asp.net中接受过来的密码再解密,下面我找到一个PHP与asp.net C#可共用的可逆加密算法,有需要了解的同学可参考.

php加密算法,代码如下:

  1. <?php 
  2. class DES 
  3. { 
  4.     var $key; 
  5.     var $iv; //偏移量 
  6.      
  7.     function DES($key = '11001100', $iv=0 ) { 
  8.     //key长度8例如:1234abcd 
  9.         $this->key = $key; 
  10.         if( $iv == 0 ) { 
  11.             $this->iv = $key; //默认以$key 作为 iv 
  12.         } else { 
  13.             $this->iv = $iv; //mcrypt_create_iv ( mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM ); 
  14.         } 
  15.     } 
  16.      
  17.     function encrypt($str) { 
  18.     //加密,返回大写十六进制字符串 
  19.         $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC ); 
  20.         $str = $this->pkcs5Pad ( $str, $size ); 
  21.         return strtoupper( bin2hex( mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv ) ) ); 
  22.     } 
  23.      
  24.     function decrypt($str) { 
  25.     //解密 
  26.         $strBin = $this->hex2bin( strtolower( $str ) ); 
  27.         $str = mcrypt_cbc( MCRYPT_DES, $this->key, $strBin, MCRYPT_DECRYPT, $this->iv ); 
  28.         $str = $this->pkcs5Unpad( $str ); 
  29.         return $str; 
  30.     } 
  31.      
  32.     function hex2bin($hexData) { 
  33.         $binData = ""; 
  34.         for($i = 0; $i < strlen ( $hexData ); $i += 2) { 
  35.             $binData .= chr ( hexdec ( substr ( $hexData, $i, 2 ) ) ); 
  36.         } 
  37.         return $binData; 
  38.     } 
  39.  
  40.     function pkcs5Pad($text, $blocksize) { 
  41.         $pad = $blocksize - (strlen ( $text ) % $blocksize); 
  42.         return $text . str_repeat ( chr ( $pad ), $pad ); 
  43.     } 
  44.      
  45.     function pkcs5Unpad($text) { 
  46.         $pad = ord ( $text {strlen ( $text ) - 1} ); 
  47.         //开源代码phpfensi.com 
  48.         if ($pad > strlen ( $text )) 
  49.             return false; 
  50.         if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad) 
  51.             return false; 
  52.         return substr ( $text, 0, - 1 * $pad ); 
  53.     } 
  54.      
  55. } 
  56. ?> 

asp.net程序代码,代码如下:

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.IO; 
  4. using System.Linq; 
  5. using System.Security.Cryptography; 
  6. using System.Text; 
  7.  
  8. namespace WindowsFormsApplication1 
  9. { 
  10.     /// <summary> 
  11.     /// DES加密解密字符串 
  12.     /// </summary> 
  13.     public class DesEncryption 
  14.     { 
  15.         /// <summary> 
  16.         /// DES加密字符串 
  17.         /// </summary> 
  18.         /// <param name="encryptString">待加密的字符串</param> 
  19.         /// <param name="encryptKey">加密密钥,要求为8位</param> 
  20.         /// <returns>加密成功返回加密后的字符串,失败返回null</returns> 
  21.         public static string EncryptDES(string encryptString, string encryptKey = "11001100") 
  22.         { 
  23.             try 
  24.             { 
  25.                 byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(encryptKey.Substring(0, 8)); 
  26.                 byte[] rgbIV = rgbKey; 
  27.                 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); 
  28.                 DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); 
  29.                 MemoryStream mStream = new MemoryStream(); 
  30.                 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 
  31.                 cStream.Write(inputByteArray, 0, inputByteArray.Length); 
  32.                 cStream.FlushFinalBlock(); 
  33.                 StringBuilder ret = new StringBuilder(); 
  34.                 foreach (byte b in mStream.ToArray()) 
  35.                 { 
  36.                     ret.AppendFormat("{0:X2}", b); 
  37.                 } 
  38.                 ret.ToString(); 
  39.                 return ret.ToString();  
  40.             } 
  41.             catch 
  42.             { 
  43.                 return null; 
  44.             } 
  45.         } 
  46.  
  47.         /// <summary> 
  48.         /// DES解密字符串 
  49.         /// </summary> 
  50.         /// <param name="decryptString">待解密的字符串</param> 
  51.         /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> 
  52.         /// <returns>解密成功返回解密后的字符串,失败返回null</returns> 
  53.         public static string DecryptDES(string decryptString, string decryptKey = "11001100") 
  54.         { 
  55.             try 
  56.             { 
  57.                 byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(decryptKey); 
  58.                 byte[] rgbIV = rgbKey; 
  59.                 byte[] inputByteArray = new byte[decryptString.Length / 2]; 
  60.                 for (int x = 0; x < decryptString.Length / 2; x++) 
  61.                 { 
  62.                     int i = (Convert.ToInt32(decryptString.Substring(x * 2, 2), 16)); 
  63.                     inputByteArray[x] = (byte)i; 
  64.                 }             
  65.                 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); 
  66.                 MemoryStream mStream = new MemoryStream(); 
  67.                 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 
  68.                 cStream.Write(inputByteArray, 0, inputByteArray.Length); 
  69.                 cStream.FlushFinalBlock(); 
  70.                 return Encoding.UTF8.GetString(mStream.ToArray()); 
  71.             } 
  72.             catch 
  73.             { 
  74.                 return null; 
  75.             } 
  76.         } 
  77.     } 
  78. } 

Tags: asp net PHP可逆加密算法

分享到: