Cet article est un article miroir de traduction automatique, veuillez cliquer ici pour accéder à l’article original.

Vue: 21171|Répondre: 3

[Source] Le code source est chiffré avec DES pour le mot de passe du programme C#

[Copié le lien]
Publié sur 08/06/2015 09:31:47 | | |
DES signifie Data Encryption Standard, c’est-à-dire Data Encryption Standard, un algorithme de blocs utilisant le chiffrement des clés, identifié comme la Federal Data Processing Standard (FIPS) par le National Bureau of Standards du gouvernement fédéral américain en 1976, et qui a depuis été largement diffusé à l’international.
  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. }
Code de copie






Précédent:asp.net obtenir l’adresse URL de la page actuelle
Prochain:ASP.NET solution de contournement pour la section de configuration non reconnue « connectionStrings »
Publié sur 01/08/2017 06:48:32 |
Merci de partager
Publié sur 04/08/2017 15:59:44 |
Merci d’avoir partagé :)
Publié sur 16/08/2017 06:44:58 |
Merci d’avoir partagé :)
Démenti:
Tous les logiciels, supports de programmation ou articles publiés par Code Farmer Network sont uniquement destinés à l’apprentissage et à la recherche ; Le contenu ci-dessus ne doit pas être utilisé à des fins commerciales ou illégales, sinon les utilisateurs assumeront toutes les conséquences. Les informations sur ce site proviennent d’Internet, et les litiges de droits d’auteur n’ont rien à voir avec ce site. Vous devez supprimer complètement le contenu ci-dessus de votre ordinateur dans les 24 heures suivant le téléchargement. Si vous aimez le programme, merci de soutenir un logiciel authentique, d’acheter l’immatriculation et d’obtenir de meilleurs services authentiques. En cas d’infraction, veuillez nous contacter par e-mail.

Mail To:help@itsvse.com