网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 设计资源 > .Net编程 > 正文

C#MD5,RC2,DES加密类

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务
  1. using System; 
  2.  
  3. using System.Collections.Generic; 
  4.  
  5. using System.Text; 
  6.  
  7. using System.IO; 
  8.  
  9. using System.Security.Cryptography; 
  10.  
  11.  
  12.  
  13. namespace DevSDK.Security 
  14.  
  15.  
  16.     /// <summary> 
  17.  
  18.     /// 软创加密类 
  19.  
  20.     /// </summary> 
  21.  
  22.     public static class Cryptography 
  23.  
  24.     { 
  25.  
  26.         /// <summary> 
  27.  
  28.         /// MD5 加密静态方法 
  29.  
  30.         /// </summary> 
  31.  
  32.         /// <param name="EncryptString">待加密的密文</param> 
  33.  
  34.         /// <returns>returns</returns> 
  35.  
  36.         public static string MD5Encrypt(string EncryptString) 
  37.  
  38.         { 
  39.  
  40.             if (string.IsNullOrEmpty(EncryptString)) 
  41.  
  42.             { 
  43.  
  44.                 throw (new Exception("密文不得为空")); 
  45.  
  46.             } 
  47.  
  48.  
  49.  
  50.             MD5 m_ClassMD5 = new MD5CryptoServiceProvider(); 
  51.  
  52.  
  53.  
  54.             string m_strEncrypt = ""
  55.  
  56.  
  57.  
  58.             try 
  59.  
  60.             { 
  61.  
  62.                 m_strEncrypt = 
  63.  
  64.                     BitConverter.ToString(m_ClassMD5.ComputeHash( 
  65.  
  66.                     Encoding.Default.GetBytes(EncryptString))).Replace("-"""); 
  67.  
  68.             } 
  69.  
  70.             catch (ArgumentException ex) { throw ex; } 
  71.  
  72.             catch (CryptographicException ex) { throw ex; } 
  73.  
  74.             catch (Exception ex) { throw ex; } 
  75.  
  76.             finally { m_ClassMD5.Clear(); } 
  77.  
  78.  
  79.  
  80.             return m_strEncrypt; 
  81.  
  82.         } 
  83.  
  84.         /// <summary> 
  85.  
  86.         /// DES 加密(数据加密标准,速度较快,适用于加密大量数据的场合) 
  87.  
  88.         /// </summary> 
  89.  
  90.         /// <param name="EncryptString">待加密的密文</param> 
  91.  
  92.         /// <param name="EncryptKey">加密的密钥</param> 
  93.  
  94.         /// <returns>returns</returns> 
  95.  
  96.         public static string DESEncrypt(string EncryptString, string EncryptKey) 
  97.  
  98.         { 
  99.  
  100.             if (string.IsNullOrEmpty(EncryptString))  
  101.  
  102.             {  
  103.  
  104.                 throw (new Exception("密文不得为空")); 
  105.  
  106.             } 
  107.  
  108.  
  109.  
  110.             if (string.IsNullOrEmpty(EncryptKey)) 
  111.  
  112.             {  
  113.  
  114.                 throw (new Exception("密钥不得为空"));  
  115.  
  116.             } 
  117.  
  118.  
  119.  
  120.             if (EncryptKey.Length != 8)  
  121.  
  122.             {  
  123.  
  124.                 throw (new Exception("密钥必须为8位"));  
  125.  
  126.             } 
  127.  
  128.  
  129.  
  130.             byte m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 
  131.  
  132.  
  133.  
  134.             string m_strEncrypt = ""
  135.  
  136.  
  137.  
  138.             DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider(); 
  139.  
  140.  
  141.  
  142.             try 
  143.  
  144.             { 
  145.  
  146.                 byte m_btEncryptString = Encoding.Default.GetBytes(EncryptString); 
  147.  
  148.  
  149.  
  150.                 MemoryStream m_stream = new MemoryStream(); 
  151.  
  152.  
  153.  
  154.                 CryptoStream m_cstream = new CryptoStream(m_stream, 
  155.  
  156.                     m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), 
  157.  
  158.                     m_btIV), CryptoStreamMode.Write); 
  159.  
  160.  
  161.  
  162.                 m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); 
  163.  
  164.  
  165.  
  166.                 m_cstream.FlushFinalBlock(); 
  167.  
  168.  
  169.  
  170.                 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray()); 
  171.  
  172.  
  173.  
  174.                 m_stream.Close(); m_stream.Dispose(); 
  175.  
  176.  
  177.  
  178.                 m_cstream.Close(); m_cstream.Dispose(); 
  179.  
  180.             } 
  181.  
  182.             catch (IOException ex) { throw ex; } 
  183.  
  184.             catch (CryptographicException ex) { throw ex; } 
  185.  
  186.             catch (ArgumentException ex) { throw ex; } 
  187.  
  188.             catch (Exception ex) { throw ex; } 
  189.  
  190.             finally { m_DESProvider.Clear(); } 
  191.  
  192.  
  193.  
  194.             return m_strEncrypt; 
  195.  
  196.         } 
  197.  
  198.         /// <summary> 
  199.  
  200.         /// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合) 
  201.  
  202.         /// </summary> 
  203.  
  204.         /// <param name="DecryptString">待解密的密文</param> 
  205.  
  206.         /// <param name="DecryptKey">解密的密钥</param> 
  207.  
  208.         /// <returns>returns</returns> 
  209.  
  210.         public static string DESDecrypt(string DecryptString, string DecryptKey) 
  211.  
  212.         { 
  213.  
  214.             if (string.IsNullOrEmpty(DecryptString)) 
  215.  
  216.             {  
  217.  
  218.                 throw (new Exception("密文不得为空"));  
  219.  
  220.             } 
  221.  
  222.  
  223.  
  224.             if (string.IsNullOrEmpty(DecryptKey))  
  225.  
  226.             {  
  227.  
  228.                 throw (new Exception("密钥不得为空")); 
  229.  
  230.             } 
  231.  
  232.  
  233.  
  234.             if (DecryptKey.Length != 8)  
  235.  
  236.             {  
  237.  
  238.                 throw (new Exception("密钥必须为8位"));  
  239.  
  240.             } 
  241.  
  242.  
  243.  
  244.             byte m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 
  245.  
  246.  
  247.  
  248.             string m_strDecrypt = ""
  249.  
  250.  
  251.  
  252.             DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider(); 
  253.  
  254.  
  255.  
  256.             try 
  257.  
  258.             { 
  259.  
  260.                 byte m_btDecryptString = Convert.FromBase64String(DecryptString); 
  261.  
  262.  
  263.  
  264.                 MemoryStream m_stream = new MemoryStream(); 
  265.  
  266.  
  267.  
  268.                 CryptoStream m_cstream = new CryptoStream(m_stream, 
  269.  
  270.                     m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), 
  271.  
  272.                     m_btIV), CryptoStreamMode.Write); 
  273.  
  274.  
  275.  
  276.                 m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); 
  277.  
  278.  
  279.  
  280.                 m_cstream.FlushFinalBlock(); 
  281.  
  282.  
  283.  
  284.                 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray()); 
  285.  
  286.  
  287.  
  288.                 m_stream.Close(); m_stream.Dispose(); 
  289.  
  290.  
  291.  
  292.                 m_cstream.Close(); m_cstream.Dispose(); 
  293.  
  294.             } 
  295.  
  296.             catch (IOException ex) { throw ex; } 
  297.  
  298.             catch (CryptographicException ex) { throw ex; } 
  299.  
  300.             catch (ArgumentException ex) { throw ex; } 
  301.  
  302.             catch (Exception ex) { throw ex; } 
  303.  
  304.             finally { m_DESProvider.Clear(); } 
  305.  
  306.  
  307.  
  308.             return m_strDecrypt; 
  309.  
  310.         } 
  311.  
  312.         /// <summary> 
  313.  
  314.         /// RC2 加密(用变长密钥对大量数据进行加密) 
  315.  
  316.         /// </summary> 
  317.  
  318.         /// <param name="EncryptString">待加密密文</param> 
  319.  
  320.         /// <param name="EncryptKey">加密密钥</param> 
  321.  
  322.         /// <returns>returns</returns> 
  323.  
  324.         public static string RC2Encrypt(string EncryptString, string EncryptKey) 
  325.  
  326.         { 
  327.  
  328.             if (string.IsNullOrEmpty(EncryptString))  
  329.  
  330.             {  
  331.  
  332.                 throw (new Exception("密文不得为空"));  
  333.  
  334.             } 
  335.  
  336.  
  337.  
  338.             if (string.IsNullOrEmpty(EncryptKey))  
  339.  
  340.             {  
  341.  
  342.                 throw (new Exception("密钥不得为空")); 
  343.  
  344.             } 
  345.  
  346.  
  347.  
  348.             if (EncryptKey.Length < 5 || EncryptKey.Length > 16)  
  349.  
  350.             {  
  351.  
  352.                 throw (new Exception("密钥必须为5-16位"));  
  353.  
  354.             } 
  355.  
  356.  
  357.  
  358.             string m_strEncrypt = ""
  359.  
  360.  
  361.  
  362.             byte m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 
  363.  
  364.  
  365.  
  366.             RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider(); 
  367.  
  368.  
  369.  
  370.             try 
  371.  
  372.             { 
  373.  
  374.                 byte m_btEncryptString = Encoding.Default.GetBytes(EncryptString); 
  375.  
  376.  
  377.  
  378.                 MemoryStream m_stream = new MemoryStream(); 
  379.  
  380.  
  381.  
  382.                 CryptoStream m_cstream = new CryptoStream(m_stream,  
  383.  
  384.                     m_RC2Provider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), 
  385.  
  386.                     m_btIV), CryptoStreamMode.Write); 
  387.  
  388.  
  389.  
  390.                 m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); 
  391.  
  392.  
  393.  
  394.                 m_cstream.FlushFinalBlock(); 
  395.  
  396.  
  397.  
  398.                 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray()); 
  399.  
  400.  
  401.  
  402.                 m_stream.Close(); m_stream.Dispose(); 
  403.  
  404.  
  405.  
  406.                 m_cstream.Close(); m_cstream.Dispose(); 
  407.  
  408.             } 
  409.  
  410.             catch (IOException ex) { throw ex; } 
  411.  
  412.             catch (CryptographicException ex) { throw ex; } 
  413.  
  414.             catch (ArgumentException ex) { throw ex; } 
  415.  
  416.             catch (Exception ex) { throw ex; } 
  417.  
  418.             finally { m_RC2Provider.Clear(); } 
  419.  
  420.  
  421.  
  422.             return m_strEncrypt; 
  423.  
  424.         } 
  425.  
  426.         /// <summary> 
  427.  
  428.         /// RC2 解密(用变长密钥对大量数据进行加密) 
  429.  
  430.         /// </summary> 
  431.  
  432.         /// <param name="DecryptString">待解密密文</param> 
  433.  
  434.         /// <param name="DecryptKey">解密密钥</param> 
  435.  
  436.         /// <returns>returns</returns> 
  437.  
  438.         public static string RC2Decrypt(string DecryptString, string DecryptKey) 
  439.  
  440.         { 
  441.  
  442.             if (string.IsNullOrEmpty(DecryptString))  
  443.  
  444.             {  
  445.  
  446.                 throw (new Exception("密文不得为空")); 
  447.  
  448.             } 
  449.  
  450.  
  451.  
  452.             if (string.IsNullOrEmpty(DecryptKey))  
  453.  
  454.             {  
  455.  
  456.                 throw (new Exception("密钥不得为空"));  
  457.  
  458.             } 
  459.  
  460.  
  461.  
  462.             if (DecryptKey.Length < 5 || DecryptKey.Length > 16)  
  463.  
  464.             {  
  465.  
  466.                 throw (new Exception("密钥必须为5-16位"));  
  467.  
  468.             } 
  469.  
  470.  
  471.  
  472.             byte m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 
  473.  
  474.  
  475.  
  476.             string m_strDecrypt = ""
  477.  
  478.  
  479.  
  480.             RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider(); 
  481.  
  482.  
  483.  
  484.             try 
  485.  
  486.             { 
  487.  
  488.                 byte m_btDecryptString = Convert.FromBase64String(DecryptString); 
  489.  
  490.  
  491.  
  492.                 MemoryStream m_stream = new MemoryStream(); 
  493.  
  494.  
  495.  
  496.                 CryptoStream m_cstream = new CryptoStream(m_stream, 
  497.  
  498.                     m_RC2Provider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), 
  499.  
  500.                     m_btIV), CryptoStreamMode.Write); 
  501.  
  502.  
  503.  
  504.                 m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); 
  505.  
  506.  
  507.  
  508.                 m_cstream.FlushFinalBlock(); 
  509.  
  510.  
  511.  
  512.                 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray()); 
  513.  
  514.  
  515.  
  516.                 m_stream.Close(); m_stream.Dispose(); 
  517.  
  518.  
  519.  
  520.                 m_cstream.Close(); m_cstream.Dispose(); 
  521.  
  522.             } 
  523.  
  524.             catch (IOException ex) { throw ex; } 
  525.  
  526.             catch (CryptographicException ex) { throw ex; } 
  527.  
  528.             catch (ArgumentException ex) { throw ex; } 
  529.  
  530.             catch (Exception ex) { throw ex; } 
  531.  
  532.             finally { m_RC2Provider.Clear(); } 
  533.  
  534.  
  535.  
  536.             return m_strDecrypt; 
  537.  
  538.         } 
  539.  
  540.         /// <summary> 
  541.  
  542.         /// 3DES 加密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高) 
  543.  
  544.         /// </summary> 
  545.  
  546.         /// <param name="EncryptString">待加密密文</param> 
  547.  
  548.         /// <param name="EncryptKey1">密钥一</param> 
  549.  
  550.         /// <param name="EncryptKey2">密钥二</param> 
  551.  
  552.         /// <param name="EncryptKey3">密钥三</param> 
  553.  
  554.         /// <returns>returns</returns> 
  555.  
  556.         public static string DES3Encrypt(string EncryptString,  
  557.  
  558.             string EncryptKey1, string EncryptKey2, string EncryptKey3) 
  559.  
  560.         { 
  561.  
  562.             string m_strEncrypt = ""
  563.  
  564.  
  565.  
  566.             try 
  567.  
  568.             { 
  569.  
  570.                 m_strEncrypt = DESEncrypt(EncryptString, EncryptKey3); 
  571.  
  572.  
  573.  
  574.                 m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey2); 
  575.  
  576.  
  577.  
  578.                 m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey1); 
  579.  
  580.             } 
  581.  
  582.             catch (Exception ex) { throw ex; } 
  583.  
  584.  
  585.  
  586.             return m_strEncrypt; 
  587.  
  588.         } 
  589.  
  590.         /// <summary> 
  591.  
  592.         /// 3DES 解密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高) 
  593.  
  594.         /// </summary> 
  595.  
  596.         /// <param name="DecryptString">待解密密文</param> 
  597.  
  598.         /// <param name="DecryptKey1">密钥一</param> 
  599.  
  600.         /// <param name="DecryptKey2">密钥二</param> 
  601.  
  602.         /// <param name="DecryptKey3">密钥三</param> 
  603.  
  604.         /// <returns>returns</returns> 
  605.  
  606.         public static string DES3Decrypt(string DecryptString,  
  607.  
  608.             string DecryptKey1, string DecryptKey2, string DecryptKey3) 
  609.  
  610.         { 
  611.  
  612.             string m_strDecrypt = ""
  613.  
  614.  
  615.  
  616.             try 
  617.  
  618.             { 
  619.  
  620.                 m_strDecrypt = DESDecrypt(DecryptString, DecryptKey1); 
  621.  
  622.  
  623.  
  624.                 m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey2); 
  625.  
  626.  
  627.  
  628.                 m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey3); 
  629.  
  630.             } 
  631.  
  632.             catch (Exception ex) { throw ex; } 
  633.  
  634.  
  635.  
  636.             return m_strDecrypt; 
  637.  
  638.         } 
  639.  
  640.         /// <summary> 
  641.  
  642.         /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高, 
  643.  
  644.         /// 目前 AES 标准的一个实现是 Rijndael 算法) 
  645.  
  646.         /// </summary> 
  647.  
  648.         /// <param name="EncryptString">待加密密文</param> 
  649.  
  650.         /// <param name="EncryptKey">加密密钥</param> 
  651.  
  652.         /// <returns></returns> 
  653.  
  654.         public static string AESEncrypt(string EncryptString, string EncryptKey) 
  655.  
  656.         { 
  657.  
  658.             if (string.IsNullOrEmpty(EncryptString))  
  659.  
  660.             {  
  661.  
  662.                 throw (new Exception("密文不得为空")); 
  663.  
  664.             } 
  665.  
  666.  
  667.  
  668.             if (string.IsNullOrEmpty(EncryptKey))  
  669.  
  670.             {  
  671.  
  672.                 throw (new Exception("密钥不得为空"));  
  673.  
  674.             } 
  675.  
  676.  
  677.  
  678.             string m_strEncrypt = ""
  679.  
  680.  
  681.  
  682.             byte m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ=="); 
  683.  
  684.  
  685.  
  686.             Rijndael m_AESProvider = Rijndael.Create(); 
  687.  
  688.  
  689.  
  690.             try 
  691.  
  692.             { 
  693.  
  694.                 byte m_btEncryptString = Encoding.Default.GetBytes(EncryptString); 
  695.  
  696.  
  697.  
  698.                 MemoryStream m_stream = new MemoryStream(); 
  699.  
  700.  
  701.  
  702.                 CryptoStream m_csstream =  
  703.  
  704.                     new CryptoStream(m_stream, m_AESProvider.CreateEncryptor( 
  705.  
  706.                         Encoding.Default.GetBytes(EncryptKey), m_btIV), 
  707.  
  708.                         CryptoStreamMode.Write); 
  709.  
  710.  
  711.  
  712.                 m_csstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);  
  713.  
  714.                 m_csstream.FlushFinalBlock(); 
  715.  
  716.  
  717.  
  718.                 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray()); 
  719.  
  720.  
  721.  
  722.                 m_stream.Close(); m_stream.Dispose(); 
  723.  
  724.  
  725.  
  726.                 m_csstream.Close(); m_csstream.Dispose(); 
  727.  
  728.             } 
  729.  
  730.             catch (IOException ex) { throw ex; } 
  731.  
  732.             catch (CryptographicException ex) { throw ex; } 
  733.  
  734.             catch (ArgumentException ex) { throw ex; } 
  735.  
  736.             catch (Exception ex) { throw ex; } 
  737.  
  738.             finally { m_AESProvider.Clear(); } 
  739.  
  740.  
  741.  
  742.             return m_strEncrypt; 
  743.  
  744.         } 
  745.  
  746.         /// <summary> 
  747.  
  748.         /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快, 
  749.  
  750.         /// 安全级别高,目前 AES 标准的一个实现是 Rijndael 算法) 
  751.  
  752.         /// </summary> 
  753.  
  754.         /// <param name="DecryptString">待解密密文</param> 
  755.  
  756.         /// <param name="DecryptKey">解密密钥</param> 
  757.         /// <returns></returns> 
  758.         public static string AESDecrypt(string DecryptString, string DecryptKey) 
  759.         { 
  760.             if (string.IsNullOrEmpty(DecryptString))  
  761.             {  
  762.                 throw (new Exception("密文不得为空"));  
  763.             } 
  764.             if (string.IsNullOrEmpty(DecryptKey))  
  765.             {  
  766.                 throw (new Exception("密钥不得为空"));  
  767.             } 
  768.             string m_strDecrypt = ""
  769.             byte m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ=="); 
  770.             Rijndael m_AESProvider = Rijndael.Create(); 
  771.             try 
  772.             { 
  773.                 byte m_btDecryptString = Convert.FromBase64String(DecryptString); 
  774.                 MemoryStream m_stream = new MemoryStream(); 
  775.                 CryptoStream m_csstream =  
  776.                     new CryptoStream(m_stream, m_AESProvider.CreateDecryptor( 
  777.                         Encoding.Default.GetBytes(DecryptKey), m_btIV), 
  778.                         CryptoStreamMode.Write); 
  779.                 m_csstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);  
  780.                 m_csstream.FlushFinalBlock(); 
  781.                 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray()); 
  782.                 m_stream.Close(); m_stream.Dispose(); 
  783.                 m_csstream.Close(); m_csstream.Dispose(); 
  784.             } 
  785.             catch (IOException ex) { throw ex; } 
  786.             catch (CryptographicException ex) { throw ex; } 
  787.             catch (ArgumentException ex) { throw ex; } 
  788.             catch (Exception ex) { throw ex; } 
  789.             finally { m_AESProvider.Clear(); } 
  790.             return m_strDecrypt; 
  791.  
  792.         } 
  793.  
  794.     } 
  795.  
  • 下一篇资讯: C#文本处理(字符)
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师