麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > PHP > 正文

PHP和C#可共用的可逆加密算法詳解

2024-05-04 23:39:48
字體:
供稿:網(wǎng)友

這篇文章主要介紹了PHP和C#可共用的可逆加密算法,需要的朋友可以參考下

在一些項(xiàng)目中要求在php中生成加密,然后在asp.net中接受過來的密碼再解密,下面和大家分享一個(gè)PHP與asp.net C#可共用的可逆加密算法,感興趣的可以參考參考。

php加密算法:

 

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

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. /// <summary> 
  10. /// DES加密解密字符串 
  11. /// </summary> 
  12. public class DesEncryption 
  13. /// <summary> 
  14. /// DES加密字符串 
  15. /// </summary> 
  16. /// <param name="encryptString">待加密的字符串</param> 
  17. /// <param name="encryptKey">加密密鑰,要求為8位</param> 
  18. /// <returns>加密成功返回加密后的字符串,失敗返回null</returns> 
  19. public static string EncryptDES(string encryptString, string encryptKey = "11001100"
  20. try 
  21. byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(encryptKey.Substring(0, 8)); 
  22. byte[] rgbIV = rgbKey; 
  23. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); 
  24. DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); 
  25. MemoryStream mStream = new MemoryStream(); 
  26. CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 
  27. cStream.Write(inputByteArray, 0, inputByteArray.Length); 
  28. cStream.FlushFinalBlock(); 
  29. StringBuilder ret = new StringBuilder(); 
  30. foreach (byte b in mStream.ToArray()) 
  31. ret.AppendFormat("{0:X2}", b); 
  32. ret.ToString(); 
  33. return ret.ToString();  
  34. catch 
  35. return null
  36.  
  37. /// <summary> 
  38. /// DES解密字符串 
  39. /// </summary> 
  40. /// <param name="decryptString">待解密的字符串</param> 
  41. /// <param name="decryptKey">解密密鑰,要求為8位,和加密密鑰相同</param> 
  42. /// <returns>解密成功返回解密后的字符串,失敗返回null</returns> 
  43. public static string DecryptDES(string decryptString, string decryptKey = "11001100"
  44. try 
  45. byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(decryptKey); 
  46. byte[] rgbIV = rgbKey; 
  47. byte[] inputByteArray = new byte[decryptString.Length / 2]; 
  48. for (int x = 0; x < decryptString.Length / 2; x++) 
  49. int i = (Convert.ToInt32(decryptString.Substring(x * 2, 2), 16)); 
  50. inputByteArray[x] = (byte)i; 
  51. }  
  52. DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); 
  53. MemoryStream mStream = new MemoryStream(); 
  54. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 
  55. cStream.Write(inputByteArray, 0, inputByteArray.Length); 
  56. cStream.FlushFinalBlock(); 
  57. return Encoding.UTF8.GetString(mStream.ToArray()); 
  58. catch 
  59. return null

以上就是PHP和C#可共用的可逆加密算法,希望對(duì)大家的學(xué)習(xí)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 蜜桃91麻豆| jizzyouxxxx| 免费在线观看成年人视频 | 蜜桃一本色道久久综合亚洲精品冫 | 欧美中文字幕一区二区三区亚洲 | 免费h片网站 | 高清视频91| 黄色av免费电影 | 久久99精品久久 | 久久国产成人精品国产成人亚洲 | 国色天香综合网 | 免费午夜视频在线观看 | 欧美日韩亚洲视频 | 国产色视频在线观看免费 | 91九色丨porny丨国产jk | 免费午夜网站 | 天天看成人免费毛片视频 | 国产精选久久久 | 国产精品久久久久久久久久东京 | 日韩毛片毛片久久精品 | 国产精品久久久久av | av在线免费播放 | 久久91精品国产91久久yfo | 91精品国产乱码久久久久久久久 | 一级黄色片武则天 | www国产成人免费观看视频,深夜成人网 | 91av大片| 久久精品日韩一区 | 欧美成人区 | 成人在线视频免费播放 | 欧美在线观看视频一区 | 国产精品手机在线亚洲 | 香蕉视频99 | 日本a级一区 | 久久久一区二区三区精品 | 亚洲最大的成人网 | 免费专区 - 91爱爱 | 精品国产一区二区亚洲人成毛片 | 免费激情视频网站 | 久久99精品久久久久久小说 | 欧美视频在线观看一区 |