zoukankan      html  css  js  c++  java
  • DES与MD5加密

      1 using System;
      2 using System.Data;
      3 using System.Configuration;
      4 using System.Web;
      5 using System.Security.Cryptography;
      6 using System.Text;
      7 using System.IO;
      8 using System.Windows.Forms;
      9 
     10 
     11 /// <summary>
     12 /// DesEncrypt 的摘要说明
     13 /// </summary>
     14 public class DesMd5
     15 {
     16      public DesMd5()
     17 {
     18    //
     19    // TODO: 在此处添加构造函数逻辑
     20    //
     21 }
     22     
     23 
     24 
     25 
     26 
     27      /// <summary>
     28      /// Encrypt the string
     29      /// Attention:key must be 8 bits
     30      /// </summary>
     31      /// <param name="strText">string</param>
     32      /// <param name="strEncrKey">key</param>
     33      /// <returns></returns>
     34      public string DesEncrypt(string strText, string strEncrKey)
     35      {
     36          byte[] byKey = null;
     37          byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
     38          try
     39          {
     40              byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
     41              DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     42              byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
     43              MemoryStream ms = new MemoryStream();
     44              CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
     45              cs.Write(inputByteArray, 0, inputByteArray.Length);
     46              cs.FlushFinalBlock();
     47              return Convert.ToBase64String(ms.ToArray());
     48 
     49          }
     50          catch (System.Exception error)
     51          {
     52             // MessageBox.Show(error.Message);
     53              return "error:" + error.Message + "\r";
     54          }
     55      }
     56      /// <summary>
     57      /// Decrypt string
     58      /// Attention:key must be 8 bits
     59      /// </summary>
     60      /// <param name="strText">Decrypt string</param>
     61      /// <param name="sDecrKey">key</param>
     62      /// <returns>output string</returns>
     63      public string DesDecrypt(string strText, string sDecrKey)
     64      {
     65          byte[] byKey = null;
     66          byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
     67          byte[] inputByteArray = new Byte[strText.Length];
     68          try
     69          {
     70              byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
     71              DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     72              inputByteArray = Convert.FromBase64String(strText);
     73              MemoryStream ms = new MemoryStream();
     74              CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
     75              cs.Write(inputByteArray, 0, inputByteArray.Length);
     76              cs.FlushFinalBlock();
     77              System.Text.Encoding encoding = new System.Text.UTF8Encoding();
     78              return encoding.GetString(ms.ToArray());
     79          }
     80          catch (System.Exception error)
     81          {
     82             // MessageBox.Show(error.Message);
     83              return "error:" + error.Message + "\r";
     84          }
     85      }
     86      /// <summary>
     87      /// Encrypt files
     88      /// Attention:key must be 8 bits
     89      /// </summary>
     90      /// <param name="m_InFilePath">Encrypt file path</param>
     91      /// <param name="m_OutFilePath">output file</param>
     92      /// <param name="strEncrKey">key</param>
     93      public void DesEncryptFile(string m_InFilePath, string m_OutFilePath, string strEncrKey)
     94      {
     95          byte[] byKey = null;
     96          byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
     97          try
     98          {
     99              byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
    100              FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
    101              FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
    102              fout.SetLength(0);
    103              //Create variables to help with read and write.
    104              byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
    105              long rdlen = 0;               //This is the total number of bytes written.
    106              long totlen = fin.Length;     //This is the total length of the input file.
    107              int len;                      //This is the number of bytes to be written at a time.
    108              DES des = new DESCryptoServiceProvider();
    109              CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
    110 
    111              //Read from the input file, then encrypt and write to the output file.
    112              while (rdlen < totlen)
    113              {
    114                  len = fin.Read(bin, 0, 100);
    115                  encStream.Write(bin, 0, len);
    116                  rdlen = rdlen + len;
    117              }
    118              encStream.Close();
    119              fout.Close();
    120              fin.Close();
    121 
    122 
    123          }
    124          catch (System.Exception error)
    125          {
    126              MessageBox.Show(error.Message, "错误信息");
    127             // MessageBox.Show(error.Message.ToString());
    128          }
    129      }
    130      /// <summary>
    131      /// Decrypt files
    132      /// Attention:key must be 8 bits
    133      /// </summary>
    134      /// <param name="m_InFilePath">Decrypt filepath</param>
    135      /// <param name="m_OutFilePath">output filepath</param>
    136      /// <param name="sDecrKey">key</param>
    137      public void DesDecryptFile(string m_InFilePath, string m_OutFilePath, string sDecrKey)
    138      {
    139          byte[] byKey = null;
    140          byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    141          try
    142          {
    143              byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
    144              FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
    145              FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
    146              fout.SetLength(0);
    147              //Create variables to help with read and write.
    148              byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
    149              long rdlen = 0;               //This is the total number of bytes written.
    150              long totlen = fin.Length;     //This is the total length of the input file.
    151              int len;                      //This is the number of bytes to be written at a time.
    152              DES des = new DESCryptoServiceProvider();
    153              CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
    154 
    155              //Read from the input file, then encrypt and write to the output file.
    156              while (rdlen < totlen)
    157              {
    158                  len = fin.Read(bin, 0, 100);
    159                  encStream.Write(bin, 0, len);
    160                  rdlen = rdlen + len;
    161              }
    162              encStream.Close();
    163              fout.Close();
    164              fin.Close();
    165          }
    166          catch (System.Exception error)
    167          {
    168              MessageBox.Show(error.Message, "错误信息");
    169              //MessageBox.Show("error:" + error.Message);
    170          }
    171      }
    172     
    173      ///MD5加密
    174      public string MD5Encrypt(string pToEncrypt, string sKey)
    175      {
    176          DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    177          byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
    178          des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    179          des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    180          MemoryStream ms = new MemoryStream();
    181          CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
    182          cs.Write(inputByteArray, 0, inputByteArray.Length);
    183          cs.FlushFinalBlock();
    184          StringBuilder ret = new StringBuilder();
    185          foreach (byte b in ms.ToArray())
    186          {
    187              ret.AppendFormat("{0:X2}", b);
    188          }
    189          ret.ToString();
    190          return ret.ToString();
    191 
    192      }
    193      ///MD5解密
    194      public string MD5Decrypt(string pToDecrypt, string sKey)
    195      {
    196          DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    197          byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
    198          for (int x = 0; x < pToDecrypt.Length / 2; x++)
    199          {
    200              int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
    201              inputByteArray[x] = (byte)i;
    202          }
    203          des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    204          des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    205          MemoryStream ms = new MemoryStream();
    206          CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
    207          cs.Write(inputByteArray, 0, inputByteArray.Length);
    208          cs.FlushFinalBlock();
    209          StringBuilder ret = new StringBuilder();
    210          return System.Text.Encoding.Default.GetString(ms.ToArray());
    211      }
    212 
    213      //哈希加密
    214      public string HashEncrypt(string plain)
    215      {
    216          byte[] buffer = System.Text.Encoding.UTF8.GetBytes(plain);
    217          System.Security.Cryptography.SHA1CryptoServiceProvider sha =
    218              new System.Security.Cryptography.SHA1CryptoServiceProvider();
    219          byte[] hash = sha.ComputeHash(buffer);
    220          System.Text.StringBuilder passwordBuilder = new System.Text.StringBuilder(32);
    221          foreach (byte hashByte in hash)
    222          {
    223              passwordBuilder.Append(hashByte.ToString("x2"));
    224          }
    225          return passwordBuilder.ToString();
    226      }
    227 
    228 }
  • 相关阅读:
    Codeforces Round #595 (Div. 3) C题题解
    Educational Codeforces Round 83 (Rated for Div. 2)
    【算法竞赛进阶指南】Supermarket 贪心+并查集
    【算法竞赛进阶指南】字典树 The XOR Longest Path
    【算法竞赛进阶指南】字典树 The XOR Largest Pair
    【算法进阶指南】双端队列 DP+思维
    【算法进阶指南】蚯蚓 队列
    【2019 杭电多校第一场】Path 最短路+最小割
    【2017 ICPC 沈阳】G.Infinite Fraction Path 暴力优化
    【2017 CCPC 秦皇岛】A.Balloon Robot 思维
  • 原文地址:https://www.cnblogs.com/q1092813103/p/5735694.html
Copyright © 2011-2022 走看看