asp. net Implementation of DES Encryption and Decryption Operation Example

  • 2021-09-24 22:08:52
  • OfStack

This paper describes the DES encryption and decryption operation implemented by asp. net. Share it for your reference, as follows:


// Encryption method 
private string encrypt(string strToEncrypt)
{
    if (strToEncrypt == null || strToEncrypt == "") return strToEncrypt;
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    // Put the string into the byte Array, the idea encoding method 
    byte[] inputByteArray = Encoding.Default.GetBytes(strToEncrypt);
    // Establish the key and offset of the encrypted object 
    des.Key = new byte[] { 1, 3, 5, 7, 2, 4, 6, 8 };
    des.Mode = CipherMode.ECB;
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    StringBuilder ret = new StringBuilder();
    foreach (byte b in ms.ToArray())
    {
      ret.AppendFormat("{0:X2}", b);
    }
    return ret.ToString();
}
// Decryption method 
private string Decrypt(string pToDecrypt)
{
    if (pToDecrypt == null || pToDecrypt == "") return pToDecrypt;
    try
    {
      DESCryptoServiceProvider des = new DESCryptoServiceProvider();
      //Put the input string into the byte array
      byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
      for (int x = 0; x < pToDecrypt.Length / 2; x++)
      {
        int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
        inputByteArray[x] = (byte)i;
      }
      //key
      des.Key = new byte[] { 1, 3, 5, 7, 2, 4, 6, 8 }; ;
      //des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
      des.Mode = CipherMode.ECB;
      MemoryStream ms = new MemoryStream();
      CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
      cs.Write(inputByteArray, 0, inputByteArray.Length);
      cs.FlushFinalBlock();
      // Establish StringBuild Object, CreateDecrypt You use a stream object, and you must change the decrypted text into a stream object 
      StringBuilder ret = new StringBuilder();
      return System.Text.Encoding.Default.GetString(ms.ToArray());
    }
    catch (Exception Exp)
    {
      return String.Empty;
    }
}

PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:

Text online encryption and decryption tools (including AES, DES, RC4, etc.):
http://tools.ofstack.com/password/txt_encode

MD5 Online Encryption Tool:
http://tools.ofstack.com/password/CreateMD5Password

Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 Encryption Tool:
http://tools.ofstack.com/password/hash_md5_sha

Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
http://tools.ofstack.com/password/sha_encode

For more readers interested in asp. net related contents, please check the topics of this site: "Summary of json Skills for asp. net Operation", "Summary of asp. net String Operation Skills", "Summary of asp. net Operation Skills for XML", "Summary of asp. net File Operation Skills", "Summary of asp. net ajax Skills" and "Summary of asp. net Cache Operation Skills".

I hope this paper is helpful to everyone's asp. net programming.


Related articles: