This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21171|Reply: 3

[Source] The source code is encrypted with DES for the C# program password

[Copy link]
Posted on 6/8/2015 9:31:47 AM | | |
DES stands for Data Encryption Standard, that is, data encryption standard, which is a block algorithm that uses key encryption, which was identified as the Federal Data Processing Standard (FIPS) by the National Bureau of Standards of the U.S. federal government in 1976, and has since been widely circulated internationally.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Security.Cryptography;
  6. using System.Text;

  7. namespace TestWeb
  8. {
  9.     public class DESEncrypt
  10.     {
  11.         #region ========加密========

  12.         /// <summary>
  13.         /// 加密
  14.         /// </summary>
  15.         /// <param name="Text"></param>
  16.         /// <returns></returns>
  17.         public static string Encrypt(string Text)
  18.         {
  19.             return Encrypt(Text, "Test");
  20.         }
  21.         /// <summary>
  22.         /// 加密数据
  23.         /// </summary>
  24.         /// <param name="Text"></param>
  25.         /// <param name="sKey"></param>
  26.         /// <returns></returns>
  27.         public static string Encrypt(string Text, string sKey)
  28.         {
  29.             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  30.             byte[] inputByteArray;
  31.             inputByteArray = Encoding.Default.GetBytes(Text);
  32.             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  33.             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  34.             System.IO.MemoryStream ms = new System.IO.MemoryStream();
  35.             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  36.             cs.Write(inputByteArray, 0, inputByteArray.Length);
  37.             cs.FlushFinalBlock();
  38.             StringBuilder ret = new StringBuilder();
  39.             foreach (byte b in ms.ToArray())
  40.             {
  41.                 ret.AppendFormat("{0:X2}", b);
  42.             }
  43.             return ret.ToString();
  44.         }

  45.         #endregion
  46.         #region ========解密========

  47.         /// <summary>
  48.         /// 解密
  49.         /// </summary>
  50.         /// <param name="Text"></param>
  51.         /// <returns></returns>
  52.         public static string Decrypt(string Text)
  53.         {
  54.             return Decrypt(Text, "Test");
  55.         }
  56.         /// <summary>
  57.         /// 解密数据
  58.         /// </summary>
  59.         /// <param name="Text"></param>
  60.         /// <param name="sKey"></param>
  61.         /// <returns></returns>
  62.         public static string Decrypt(string Text, string sKey)
  63.         {
  64.             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  65.             int len;
  66.             len = Text.Length / 2;
  67.             byte[] inputByteArray = new byte[len];
  68.             int x, i;
  69.             for (x = 0; x < len; x++)
  70.             {
  71.                 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  72.                 inputByteArray[x] = (byte)i;
  73.             }
  74.             des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  75.             des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  76.             System.IO.MemoryStream ms = new System.IO.MemoryStream();
  77.             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  78.             cs.Write(inputByteArray, 0, inputByteArray.Length);
  79.             cs.FlushFinalBlock();
  80.             return Encoding.Default.GetString(ms.ToArray());
  81.         }

  82.         #endregion
  83.     }
  84. }
Copy code






Previous:asp.net get the URL address of the current page
Next:ASP.NET workaround for the unrecognized configuration section &quot;connectionStrings&quot;
Posted on 8/1/2017 6:48:32 AM |
Thank you for sharing
Posted on 8/4/2017 3:59:44 PM |
thanks for sharing :)
Posted on 8/16/2017 6:44:58 AM |
thanks for sharing :)
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com