Este artículo es un artículo espejo de traducción automática, por favor haga clic aquí para saltar al artículo original.

Vista: 21171|Respuesta: 3

[Fuente] El código fuente está cifrado con DES para la contraseña del programa C#

[Copiar enlace]
Publicado en 8/6/2015 9:31:47 | | |
DES significa Data Encryption Standard, es decir, data encryption standard, que es un algoritmo de bloques que utiliza cifrado de claves, identificado como el Federal Data Processing Standard (FIPS) por la Oficina Nacional de Normas del gobierno federal de EE. UU. en 1976, y desde entonces ha circulado ampliamente internacionalmente.
  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. }
Copiar código






Anterior:asp.net obtener la dirección URL de la página actual
Próximo:ASP.NET solución alternativa para la sección de configuración no reconocida &quot;connectionStrings&quot;
Publicado en 1/8/2017 6:48:32 |
Gracias por compartir
Publicado en 4/8/2017 15:59:44 |
Gracias por compartir :)
Publicado en 16/8/2017 6:44:58 |
Gracias por compartir :)
Renuncia:
Todo el software, materiales de programación o artículos publicados por Code Farmer Network son únicamente para fines de aprendizaje e investigación; El contenido anterior no se utilizará con fines comerciales o ilegales; de lo contrario, los usuarios asumirán todas las consecuencias. La información de este sitio proviene de Internet, y las disputas de derechos de autor no tienen nada que ver con este sitio. Debes eliminar completamente el contenido anterior de tu ordenador en un plazo de 24 horas desde la descarga. Si te gusta el programa, por favor apoya el software genuino, compra el registro y obtén mejores servicios genuinos. Si hay alguna infracción, por favor contáctanos por correo electrónico.

Mail To:help@itsvse.com