精华内容
下载资源
问答
  • C#md5加密解密

    2017-12-04 10:13:06
    C#编写的md5加密解密,含调用例子.有需要的朋友请下载使用
  • C# MD5加密解密实例

    2013-11-10 13:36:42
    C# MD5加密解密实例,一个小程序,哈哈
  • C# MD5加密解密类 winform

    千次阅读 2012-12-18 09:57:49
    C#MD5加密解密
    从网上找到了一些相关的资料然后自己试着做了一下觉得收益匪浅。其实对MD5的加密解密,我们知道怎么调用就好了,其实也没有什么太多的必要去看其算法。以下代码希望对观望的读者有用。

    MD5加密解密的两个类:

    其中的一个加密解密类:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Globalization;
    using System.Security.Cryptography;
    using System.IO;
    namespace WindowsApplication2
    {
        class DES
        {
            // 创建Key
            public string GenerateKey()
            {
                DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
                return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
            }
            ///MD5加密
            public string MD5Encrypt(string pToEncrypt, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                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);
                }
                ret.ToString();
                return ret.ToString();
    
    
            }
    
            ///MD5解密
            public string MD5Decrypt(string pToDecrypt, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
                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;
                }
    
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
    
                StringBuilder ret = new StringBuilder();
    
                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
    
     
    
        }
    }
    
    


     

    另外一个加密解密类:

    using System;
    using System.Text;
    using System.IO;
    using System.Globalization;
    using System.Security.Cryptography;
    using System.Collections.Generic;
    
    namespace WindowsApplication2
    {
        class MD5
        {
            // 创建Key
            public string GenerateKey()
            {
                DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
                return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
            }
            // 加密字符串
            public string EncryptString(string sInputString, string sKey)
            {
                byte[] data = Encoding.UTF8.GetBytes(sInputString);
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                ICryptoTransform desencrypt = DES.CreateEncryptor();
                byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
                return BitConverter.ToString(result);
            }
            // 解密字符串
            public string DecryptString(string sInputString, string sKey)
            {
                string[] sInput = sInputString.Split("-".ToCharArray());
                byte[] data = new byte[sInput.Length];
                for (int i = 0; i < sInput.Length; i++)
                {
                    data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
                }
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                ICryptoTransform desencrypt = DES.CreateDecryptor();
                byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
                return Encoding.UTF8.GetString(result);
            }
    
        }
    }
    
    
    


     

    调用这两个类:

    public partial class EDForm : Form
        {
            public EDForm()
            {
                InitializeComponent();
            }
            DES des = new DES();
            string key = null;
            /// <summary>
            /// 加密字符串
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                key = des.GenerateKey();
                this.textBox2.Text = des.MD5Encrypt(textBox1.Text,key);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                MessageBox.Show(des.MD5Decrypt(textBox2.Text, key));
            }
        }
    
    


     

    注意:在调用时key必须是同一个,如若不然就会报“输入的数据格式不对”错了。

    展开全文
  • C# MD5加密解密

    2020-02-24 07:06:15
    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Security.Cryptography; using System.Text;.../// MD5 的摘要说明 /// </summary> pub...
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Security.Cryptography;
    using System.Text;
    
    /// <summary>
    /// MD5 的摘要说明
    /// </summary>
    public class MD5
    {
    	#region ========加密========
    	/// <summary>
    	/// 加密
    	/// </summary>
    	/// <param name="Text"></param>
    	/// <returns></returns>
    	public static string Encrypt(string Text)
    	{
    		return Encrypt(Text, "cong");
    	}
    	/// <summary> 
    	/// 加密数据 
    	/// </summary> 
    	/// <param name="Text"></param> 
    	/// <param name="sKey"></param> 
    	/// <returns></returns> 
    	public static string Encrypt(string Text, string sKey)
    	{
    		DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    		byte[] inputByteArray;
    		inputByteArray = Encoding.Default.GetBytes(Text);
    		des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    		des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    		System.IO.MemoryStream ms = new System.IO.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();
    	}
    
    	#endregion
    
    
    	#region ========解密========
    
    
    	/// <summary>
    	/// 解密
    	/// </summary>
    	/// <param name="Text"></param>
    	/// <returns></returns>
    	public static string Decrypt(string Text)
    	{
    		return Decrypt(Text, "cong");
    	}
    	/// <summary> 
    	/// 解密数据 
    	/// </summary> 
    	/// <param name="Text"></param> 
    	/// <param name="sKey"></param> 
    	/// <returns></returns> 
    	public static string Decrypt(string Text, string sKey)
    	{
    		DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    		int len;
    		len = Text.Length / 2;
    		byte[] inputByteArray = new byte[len];
    		int x, i;
    		for (x = 0; x < len; x++)
    		{
    			i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
    			inputByteArray[x] = (byte)i;
    		}
    		des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    		des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    		System.IO.MemoryStream ms = new System.IO.MemoryStream();
    		CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
    		cs.Write(inputByteArray, 0, inputByteArray.Length);
    		cs.FlushFinalBlock();
    		return Encoding.Default.GetString(ms.ToArray());
    	}
    
    	#endregion
    }

     

    展开全文
  • c#MD5加密解密

    2018-05-24 23:00:00
    MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest开发出来,经MD2、MD3和MD4发展而来。 是让大容量信息在用...

    MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest开发出来,经MD2、MD3和MD4发展而来。

    是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式(就是把一个任意长度的字节串变换成一定长的大整数)。不管是MD2、MD4还是MD5,它们都需要获得一个随机长度的信息并产生一个128位的信息摘要。虽然这些算法的结构或多或少有些相似,但MD2的设计与MD4和MD5完全不同,那是因为MD2是为8位机器做过设计优化的,而MD4和MD5却是面向32位的电脑。

    1.新建一个窗体应用程序

    有三种方式来加密,上两种注释的是不需要秘钥加密的,下一种是是需要秘钥和向量的;

    代码如下:

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace AES加密解密
    {
        public partial class MD5_DES_加密解密 : Form
        {
            public MD5_DES_加密解密()
            {
                InitializeComponent();
            }
    
            private void MD5_DES_加密解密_Load(object sender, EventArgs e)
            {
    
            }
    
            /// <summary>
    
            /// 32位MD5加密
    
            /// </summary>
    
            /// <param name="sender"></param>
    
            /// <param name="e"></param>
            /*  private void button1_Click(object sender, EventArgs e)
              {//这两种方式都可以
                  /*  byte[] result = Encoding.Default.GetBytes(this.textBox1.Text.Trim());    //textBox1为输入密码的文本框
                    MD5 md5 = new MD5CryptoServiceProvider();
                    byte[] output = md5.ComputeHash(result);
                    this.richTextBox1.Text = BitConverter.ToString(output).Replace("-", "");  //richTextBox1为输出加密文本的文本框
               */
            /*  if (richTextBox1.Text!=null)
             {
                 richTextBox1.Text = "";
                 string str = textBox1.Text;
    
                 MD5 md = MD5.Create();
    
                 byte[] bytes = md.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
    
    
                 foreach (byte b in bytes)
    
                 {
    
                     richTextBox1.Text += b.ToString();
    
                 }
    
             }
    
         }*/
    
            private void button1_Click(object sender, EventArgs e)
            {//加密-此种方法要输入秘钥键值
                richTextBox1.Text = Encode(textBox1.Text, textBox2.Text, textBox3.Text);
            }
    
            private void button2_Click(object sender, EventArgs e)
    
            {//解密-此种方法要输入秘钥键值
    
                richTextBox1.Text = Decode(textBox4.Text, textBox2.Text, textBox3.Text);
    
            }
    
           
            //加密
    
            public static string Encode(string data, string Key_64, string Iv_64)
    
            {
    
                string KEY_64 = Key_64;// "VavicApp";
    
                string IV_64 = Iv_64;// "VavicApp";//64位的键值和IV值要为8位
    
                try
    
                {
    
                    byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
    
                    byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
    
                    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    
                    int i = cryptoProvider.KeySize;
    
                    MemoryStream ms = new MemoryStream();
    
                    CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
    
                    StreamWriter sw = new StreamWriter(cst);
    
                    sw.Write(data);
    
                    sw.Flush();
    
                    cst.FlushFinalBlock();
    
                    sw.Flush();
    
                    return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
    
                }
    
                catch (Exception x)
    
                {
    
                    return x.Message;
    
                }
    
            }
            //解密
    
            public static string Decode(string data, string Key_64, string Iv_64)
    
            {
    
                string KEY_64 = Key_64;// "VavicApp";密钥
    
                string IV_64 = Iv_64;// "VavicApp"; 向量IV必须是 8 字节长度的十六进制数。
    
                try
    
                {
    
                    byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
    
                    byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
    
                    byte[] byEnc;
    
                    byEnc = Convert.FromBase64String(data); //把需要解密的字符串转为8位无符号数组
    
                    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    
                    MemoryStream ms = new MemoryStream(byEnc);
    
                    CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
    
                    StreamReader sr = new StreamReader(cst);
    
                    return sr.ReadToEnd();
    
                }
    
                catch (Exception x)
    
                {
    
                    return x.Message;
    
                }
    
            }
    
        }
    }

    2.实验效果

     

    转载于:https://www.cnblogs.com/kalezhangtao/p/9085698.html

    展开全文
  • 主要介绍了c#多种加解密示例,包括了MD5加密,SHA1加密,DES加解密,需要的朋友可以参考下
  • C#MD5加密解密

    2007-06-27 11:50:00
    C#MD5加密解密 2007-03-12 11:35 using System.Security.Cryptography;using System.IO;using System.Text; ///MD5加密public string MD5Encryp...
    C#MD5加密解密
    2007-03-12 11:35

    using System.Security.Cryptography;
    using    System.IO;  
    using    System.Text;

    ///MD5加密
      public string MD5Encrypt(string    pToEncrypt,  string    sKey)
        {  
         DESCryptoServiceProvider    des  
    =  new    DESCryptoServiceProvider();  
       
    byte[]    inputByteArray  =    Encoding.Default.GetBytes(pToEncrypt);  
         des.Key  
    =    ASCIIEncoding.ASCII.GetBytes(sKey);  
         des.IV  
    =    ASCIIEncoding.ASCII.GetBytes(sKey);  
         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);  
         }  
         ret.ToString();  
       
    return    ret.ToString();  


        }

      
    ///MD5解密
      public string MD5Decrypt(string    pToDecrypt,  string    sKey)
        {
         DESCryptoServiceProvider    des  
    =  new    DESCryptoServiceProvider();  

       
    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;  
         }  

         des.Key  
    =    ASCIIEncoding.ASCII.GetBytes(sKey);  
         des.IV  
    =    ASCIIEncoding.ASCII.GetBytes(sKey);  
         MemoryStream    ms  
    =  new    MemoryStream();  
         CryptoStream    cs  
    =  new    CryptoStream(ms,    des.CreateDecryptor(),CryptoStreamMode.Write);  
         cs.Write(inputByteArray,  
    0,    inputByteArray.Length);  
         cs.FlushFinalBlock();  

         StringBuilder    ret  
    =  new    StringBuilder();  
                 
       
    return    System.Text.Encoding.Default.GetString(ms.ToArray());  
        }

    -------------------------------------------------------------------------------

    using System;
    using System.Text;
    using System.Globalization;
    using System.Security.Cryptography;
    class DES
    {
    // 创建Key
    public string GenerateKey()
    {
    DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
    return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
    }
    // 加密字符串
    public string EncryptString(string sInputString, string sKey)
    {
    byte [] data = Encoding.UTF8.GetBytes(sInputString);
    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    ICryptoTransform desencrypt = DES.CreateEncryptor();
    byte [] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
    return BitConverter.ToString(result);
    }
    // 解密字符串
    public string DecryptString(string sInputString, string sKey)
    {
    string [] sInput = sInputString.Split("-".ToCharArray());
    byte [] data = new byte[sInput.Length];
    for(int i = 0; i < sInput.Length; i++)
    {
    data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
    }
    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    ICryptoTransform desencrypt = DES.CreateDecryptor();
    byte [] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
    return Encoding.UTF8.GetString(result);
    }
    }
    class Test
    {
    static void Main()
    {
    DES des = new DES();
    string key = des.GenerateKey();
    string s0 = "中国软件 - csdn.net";
    string s1 = des.EncryptString(s0, key);
    string s2 = des.DecryptString(s1, key);
    Console.WriteLine("原串: [{0}]", s0);
    Console.WriteLine("加密: [{0}]", s1);
    Console.WriteLine("解密: [{0}]", s2);
    }
    }
    /* 程序输出:
    原串: [中国软件 - csdn.net]
    加密: [E8-30-D0-F2-2F-66-52-14-45-9A-DC-C5-85-E7-62-9B-AD-B7-82-CF-A8-0A-59-77]
    解密: [中国软件 - csdn.net]
    */

    转载于:https://www.cnblogs.com/CShapWinForms/archive/2007/06/27/797479.html

    展开全文
  • C# MD5加密 解密

    千次阅读 2017-07-31 10:04:13
     des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));  des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web....
  • C#Md5加密解密算法

    2012-09-14 11:43:35
    MDS加密解密算法,Word文档,详细代码
  • C# MD5加密解密帮助类

    2017-10-10 16:32:00
    /// <summary> /// MD5加密解密帮助类 /// </summary> public static class DESHelper { /// <summary> /// MD5加密 /// </summary>...
  • C# MD5解密

    2012-07-16 16:29:19
    using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; using System.IO;   namespace Inventory ... class MD5 ... #region 使用MD5进行数据加
  • c# md5解密

    2014-12-02 17:01:59
    ///MD5解密 public string MD5Decrypt(string pToDecrypt) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0;...
  • C# MD5 加密,解密

    2020-10-23 13:41:02
    public class MD5Help {  ///MD5加密  public static string MD5Encrypt(string pToEncrypt, string sKey)  {  DESCryptoServiceProvider des = new DESCryptoServiceProvider();  byte[] inputByteAr
  • C#MD5 加密 解密

    2012-12-26 15:37:22
    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO;...namespace ENPOT.Manufacture.Security.DL.DataLogic ...{
  • C# 各种加密方法封装类软创加密类内含 MD5 加密解密 DES 法解密加密 RC2 加密解密 3DES 加密解密还有 AES 加解密等使用时用到哪一种加密方法可把代码单 独摘录出来本类比较综合代码中包括注释完整代码 view source...
  • ASP.NET C# MD5加密 解密

    千次阅读 2016-04-19 09:18:15
    readonly static string key = "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709";... string MD5_EncodeStr = MD5_Encode(“468498148418”);  Response.Write("[RandomNumMD5Encode]" + MD5_EncodeStr + "
  • c# MD5加密解密代码

    2013-11-27 16:22:00
    // 在此处添加更多操作并使用 [OperationContract] 标记它们 #region MD5加密 /// <summary> /// MD5加密 /// </summary> /// <param name="strSource">...
  • C#各种加密方法封装类软创加密类内含 MD5加密解密DES法解密加密RC2 加密解密3DES加密解密还有 AES加解密等使用时用到哪一种加密方法可把代码单 独摘录出来本类比较综合代码中包括注释完整代码 view sourceprint?...
  • C# MD5值遍历解密源码

    2019-02-26 15:40:20
    遍历所有MD5解密算法: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SciTools { /// &lt;summary&gt; /// ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 427
精华内容 170
关键字:

c#md5解密

c# 订阅