zoukankan      html  css  js  c++  java
  • C#加密解密源码

    1. 加密:    
    2. static String Encrypt(String pwd) {    
    3. DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密    
    4. PasswordDeriveBytes db = new PasswordDeriveBytes(pwd, null);//产生key    
    5. byte[] key = db.GetBytes(8);    
    6. MemoryStream ms = new MemoryStream();//存储加密后的数据    
    7. CryptoStream cs = new CryptoStream(ms,desc.CreateEncryptor(key, key),CryptoStreamMode.Write);    
    8. byte[] data = Encoding.Unicode.GetBytes(pwd);//取到密码的字节流    
    9. cs.Write(data, 0, data.Length);//进行加密    
    10. cs.FlushFinalBlock();    
    11. byte[] res = ms.ToArray();//取加密后的数据    
    12. return Encoding.Unicode.GetString(res);//转换到字符串返回    
    13. }    
    14. 解密:    
    15. static String Decrypt(String pwd, String data) {    
    16. DESCryptoServiceProvider desc = new DESCryptoServiceProvider();    
    17. PasswordDeriveBytes db = new PasswordDeriveBytes(pwd, null);//产生key    
    18. byte[] key = db.GetBytes(8);    
    19. MemoryStream ms = new MemoryStream();//存储解密后的数据    
    20. CryptoStream cs = new CryptoStream(ms,desc.CreateDecryptor(key, key),CryptoStreamMode.Write);    
    21. byte[] databytes = Encoding.Unicode.GetBytes(data);//取到加密后的数据的字节流    
    22. cs.Write(databytes, 0, databytes.Length);//解密数据    
    23. cs.FlushFinalBlock();    
    24. byte[] res = ms.ToArray();    
    25. return Encoding.Unicode.GetString(res);//返回解密后的数据    
    26. }      
    27. 1、方法一 (不可逆加密)   
    28.   
    29.   
    30.   
    31. public string EncryptPassword(string PasswordString,string PasswordFormat )    
    32.    {    
    33.    string encryptPassword = null;   
    34.    if (PasswordFormat="SHA1"){    
    35.    encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString   
    36.   
    37. ,"SHA1");    
    38.    }    
    39.    elseif (PasswordFormat="MD5")    
    40.    { encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString   
    41.   
    42. ,"MD5");    
    43.    }   
    44. return encryptPassword ;   
    45. }   
    46.   
    47. 2、方法二 (可逆加密)   
    48.   
    49.   
    50.     public interface IBindesh   
    51. {   
    52.     string encode(string str);   
    53.     string decode(string str);   
    54. }   
    55.   
    56. public class EncryptionDecryption : IBindesh   
    57.     {   
    58.         public string encode(string str)   
    59.         {   
    60.             string htext = "";   
    61.   
    62.             for ( int i = 0; i < str.Length; i++)   
    63.             {   
    64.                 htext = htext + (char) (str[i] + 10 - 1 * 2);   
    65.             }   
    66.             return htext;   
    67.         }   
    68.   
    69.         public string decode(string str)   
    70.         {   
    71.             string dtext = "";   
    72.   
    73.             for ( int i=0; i < str.Length; i++)   
    74.             {   
    75.                 dtext = dtext + (char) (str[i] - 10 + 1*2);   
    76.             }   
    77.             return dtext;   
    78.         }   
    79.   
    80.   
    81. 3、方法三 (可逆加密)   
    82.   
    83.   
    84.            
    85.         const string KEY_64 = "VavicApp";//注意了,是8个字符,64位   
    86.   
    87.         const string IV_64 = "VavicApp";    
    88. public string Encode(string data)   
    89.         {   
    90.             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);   
    91.             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);   
    92.   
    93.             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();   
    94.             int i = cryptoProvider.KeySize;   
    95.             MemoryStream ms = new MemoryStream();   
    96.             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey,   
    97.   
    98. byIV), CryptoStreamMode.Write);   
    99.   
    100.             StreamWriter sw = new StreamWriter(cst);   
    101.             sw.Write(data);   
    102.             sw.Flush();   
    103.             cst.FlushFinalBlock();   
    104.             sw.Flush();   
    105.             return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);   
    106.   
    107.         }   
    108.   
    109.         public string Decode(string data)   
    110.         {   
    111.             byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);   
    112.             byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);   
    113.   
    114.             byte[] byEnc;   
    115.             try  
    116.             {   
    117.                 byEnc = Convert.FromBase64String(data);   
    118.             }   
    119.             catch  
    120.             {   
    121.                 return null;   
    122.             }   
    123.   
    124.             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();   
    125.             MemoryStream ms = new MemoryStream(byEnc);   
    126.             CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey,   
    127.   
    128. byIV), CryptoStreamMode.Read);   
    129.             StreamReader sr = new StreamReader(cst);   
    130.             return sr.ReadToEnd();   
    131.         }   
    132.   
    133. 4、MD5不可逆加密   
    134.   
    135.     (32位加密)   
    136.   
    137. public string GetMD5(string s, string _input_charset)   
    138.     {   
    139.   
    140.         /**//**//**//// <summary>   
    141.         /// 与ASP兼容的MD5加密算法   
    142.         /// </summary>   
    143.   
    144.         MD5 md5 = new MD5CryptoServiceProvider();   
    145.         byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));   
    146.         StringBuilder sb = new StringBuilder(32);   
    147.         for (int i = 0; i < t.Length; i++)   
    148.         {   
    149.             sb.Append(t[i].ToString("x").PadLeft(2, '0'));   
    150.         }   
    151.         return sb.ToString();   
    152.     }   
    153.    (16位加密)   
    154.   
    155.   
    156. public static string GetMd5Str(string ConvertString)   
    157.     {   
    158.         MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();   
    159.         string t2 =   
    160.   
    161. BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);   
    162.         t2 = t2.Replace("-""");   
    163.         return t2;   
    164.     }   
    165.   
    166. 5、加解文本文件   
    167.   
    168.   
    169.     //加密文件   
    170.     private static void EncryptData(String inName, String outName, byte[] desKey, byte[]   
    171.   
    172. desIV)   
    173.     {   
    174.         //Create the file streams to handle the input and output files.   
    175.         FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);   
    176.         FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);   
    177.         fout.SetLength(0);   
    178.   
    179.         //Create variables to help with read and write.   
    180.         byte[] bin = new byte[100]; //This is intermediate storage for the encryption.   
    181.         long rdlen = 0;              //This is the total number of bytes written.   
    182.         long totlen = fin.Length;    //This is the total length of the input file.   
    183.         int len;                     //This is the number of bytes to be written at a time.   
    184.   
    185.         DES des = new DESCryptoServiceProvider();   
    186.         CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV),   
    187.   
    188. CryptoStreamMode.Write);   
    189.   
    190.         //Read from the input file, then encrypt and write to the output file.   
    191.         while (rdlen < totlen)   
    192.         {   
    193.             len = fin.Read(bin, 0, 100);   
    194.             encStream.Write(bin, 0, len);   
    195.             rdlen = rdlen + len;   
    196.         }   
    197.   
    198.         encStream.Close();   
    199.         fout.Close();   
    200.         fin.Close();   
    201.     }   
    202.   
    203.     //解密文件   
    204.     private static void DecryptData(String inName, String outName, byte[] desKey, byte[]   
    205.   
    206. desIV)   
    207.     {   
    208.         //Create the file streams to handle the input and output files.   
    209.         FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);   
    210.         FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);   
    211.         fout.SetLength(0);   
    212.   
    213.         //Create variables to help with read and write.   
    214.         byte[] bin = new byte[100]; //This is intermediate storage for the encryption.   
    215.         long rdlen = 0;              //This is the total number of bytes written.   
    216.         long totlen = fin.Length;    //This is the total length of the input file.   
    217.         int len;                     //This is the number of bytes to be written at a time.   
    218.   
    219.         DES des = new DESCryptoServiceProvider();   
    220.         CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV),   
    221.   
    222. CryptoStreamMode.Write);   
    223.   
    224.         //Read from the input file, then encrypt and write to the output file.   
    225.         while (rdlen < totlen)   
    226.         {   
    227.             len = fin.Read(bin, 0, 100);   
    228.             encStream.Write(bin, 0, len);   
    229.             rdlen = rdlen + len;   
    230.         }   
    231.   
    232.         encStream.Close();   
    233.         fout.Close();   
    234.         fin.Close();   
    235.     }   
    236.   
    237. 6、   
    238.   
    239. using System;   
    240. using System.Collections.Generic;   
    241. using System.Text;   
    242. using System.Security.Cryptography;   
    243. using System.IO;   
    244.   
    245. namespace Component   
    246. {   
    247.     public class Security   
    248.     {   
    249.         public Security()   
    250.         {    
    251.            
    252.         }   
    253.   
    254.         //默认密钥向量   
    255.         private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };   
    256.         /**//**//**//**//**//**//**//// <summary>   
    257.         /// DES加密字符串   
    258.         /// </summary>   
    259.         /// <param name="encryptString">待加密的字符串</param>   
    260.         /// <param name="encryptKey">加密密钥,要求为8位</param>   
    261.         /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>   
    262.         public static string EncryptDES(string encryptString, string encryptKey)   
    263.         {   
    264.             try  
    265.             {   
    266.                 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));   
    267.                 byte[] rgbIV = Keys;   
    268.                 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);   
    269.                 DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();   
    270.                 MemoryStream mStream = new MemoryStream();   
    271.                 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey,   
    272.   
    273. rgbIV), CryptoStreamMode.Write);   
    274.                 cStream.Write(inputByteArray, 0, inputByteArray.Length);   
    275.                 cStream.FlushFinalBlock();   
    276.                 return Convert.ToBase64String(mStream.ToArray());   
    277.             }   
    278.             catch  
    279.             {   
    280.                 return encryptString;   
    281.             }   
    282.         }   
    283.   
    284.         /**//**//**//**//**//**//**//// <summary>   
    285.         /// DES解密字符串   
    286.         /// </summary>   
    287.         /// <param name="decryptString">待解密的字符串</param>   
    288.         /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>   
    289.         /// <returns>解密成功返回解密后的字符串,失败返源串</returns>   
    290.         public static string DecryptDES(string decryptString, string decryptKey)   
    291.         {   
    292.             try  
    293.             {   
    294.                 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);   
    295.                 byte[] rgbIV = Keys;   
    296.                 byte[] inputByteArray = Convert.FromBase64String(decryptString);   
    297.                 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();   
    298.                 MemoryStream mStream = new MemoryStream();   
    299.                 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey,   
    300.   
    301. rgbIV), CryptoStreamMode.Write);   
    302.                 cStream.Write(inputByteArray, 0, inputByteArray.Length);   
    303.                 cStream.FlushFinalBlock();   
    304.                 return Encoding.UTF8.GetString(mStream.ToArray());   
    305.             }   
    306.             catch  
    307.             {   
    308.                 return decryptString;   
    309.             }   
    310.         }   
    311.   
    312.   
    313.     }   
    314. }  
  • 相关阅读:
    视频安防智能监控系统管理平台EasyNVS切换导航报错Cannot find module ‘@/views/sys/user’问题
    视频安防智能监控系统管理平台EasyNVS手机端扫码直播展示无信号但是PC端可以播放是什么原因?
    RTSP协议视频智能安防监控平台EasyNVR接入大华摄像头无法拉取H265格式视频流如何解决?
    RTSP协议视频智能安防监控平台EasyNVR录像播放及下载接口如何返回在线m3u8格式视频流?
    网页无插件直播视频平台EasyNVR老版本更新版本后CPU占用过高怎么解决?
    【解决方案】如何通过EasyNVR+EasyNVS搭建一套高清智能化的高速公路视频监控管理系统?
    【解决方案】现代化工厂如何实现智能化视频管理?EasyNVR安防视频监控系统打造智慧工厂
    【解决方案】EasyNVR视频边缘计算网关如何实现智慧消防?
    RTSP协议智能安防监控系统EasyNVR硬件设备IP探测软件EasySearcher无法运行如何修复?
    就地过年带旺“云游”,RTSP协议视频监控智能分析平台EasyNVR助力景区“慢直播”
  • 原文地址:https://www.cnblogs.com/IsNull/p/1781453.html
Copyright © 2011-2022 走看看