C# 字串DES加/解密與MD5加密類

來源:互聯網
上載者:User

C#源碼,用.Net封裝的類庫寫的加密和解密的方法,很容易轉換成VB.Net

using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace MyDes
{
    /// <summary>
    /// DES加/解密與MD5加密
    /// </summary>
    public class My_Des
    {
        #region 私人屬性

        /// <summary>
        /// MD5加密的字串
        /// </summary>
        private string md5Str = null;
        /// <summary>
        /// DES加密的字串
        /// </summary>
        private string encryptStr = null;

        /// <summary>
        /// DES解密的字串
        /// </summary>
        private string decryptStr = null;

        /// <summary>
        /// DES密匙
        /// </summary>
        private string mydesKey = null;

        /// <summary>
        /// 返回的字串
        /// </summary>
        private string mydesStr = null;

        /// <summary>
        /// 錯誤資訊
        /// </summary>
        private string messAge = null;

        #endregion

        #region 公用屬性

        /// <summary>
        /// MD5加密字串
        /// </summary>
        public string MD5Str
        {
            get { return md5Str; }
            set { md5Str = value; }
        }
        /// <summary>
        /// DES加密的字串
        /// </summary>
        public string EncryptStr
        {
            get { return encryptStr; }
            set { encryptStr = value; }
        }

        /// <summary>
        ///DES 解密的字串
        /// </summary>
        public string DecryptStr
        {
            get { return decryptStr; }
            set { decryptStr = value; }
        }

        /// <summary>
        /// DES密匙
        /// </summary>
        public string MyDesKey
        {
            get { return mydesKey; }
            set { mydesKey = value; }
        }

        /// <summary>
        /// 返回的字串
        /// </summary>
        public string MyDesStr
        {
            get { return mydesStr; }
            set { mydesStr = value; }
        }
        /// <summary>
        /// 錯誤資訊
        /// </summary>
        public string Message
        {
            get { return messAge; }
            set { messAge = value; }
        }
        #endregion

        #region 公用方法
        /// <summary>
        /// 執行DES加密
        /// </summary>
        public void DesEncrypt()
        {
            try
            {
                byte[] MyStr_E = Encoding.UTF8.GetBytes(this.encryptStr);
                byte[] MyKey_E = Encoding.UTF8.GetBytes(this.mydesKey);

                DESCryptoServiceProvider MyDes_E = new DESCryptoServiceProvider();
                MyDes_E.Key = MyKey_E;
                MyDes_E.IV = MyKey_E;

                MemoryStream MyMem_E = new MemoryStream();

                CryptoStream MyCry_E = new CryptoStream(MyMem_E, MyDes_E.CreateEncryptor(), CryptoStreamMode.Write);
                MyCry_E.Write(MyStr_E, 0, MyStr_E.Length);
                MyCry_E.FlushFinalBlock();
                MyCry_E.Close();

                this.mydesStr = Convert.ToBase64String(MyMem_E.ToArray());
            }
            catch (Exception Error)
            {
                this.messAge = "DES加密出錯:"+Error.Message;
            }
        }

        /// <summary>
        /// 執行DES解密
        /// </summary>
        public void DesDecrypt()
        {
            try
            {
                byte[] MyStr_D = Convert.FromBase64String(this.decryptStr);
                byte[] MyKey_D = Encoding.UTF8.GetBytes(this.mydesKey);

                DESCryptoServiceProvider MyDes_D = new DESCryptoServiceProvider();
                MyDes_D.Key = MyKey_D;
                MyDes_D.IV = MyKey_D;

                MemoryStream MyMem_D = new MemoryStream();

                CryptoStream MyCry_D = new CryptoStream(MyMem_D, MyDes_D.CreateDecryptor(), CryptoStreamMode.Write);                        
                MyCry_D.Write(MyStr_D, 0, MyStr_D.Length);
                MyCry_D.FlushFinalBlock();
                MyCry_D.Close();

                this.mydesStr = Encoding.UTF8.GetString(MyMem_D.ToArray());
            }
            catch (Exception Error)
            {
                this.messAge ="DES解密出錯:" +Error.Message;
            }
        }

        /// <summary>
        /// 執行MD5加密
        /// </summary>
        public void MD5JiaMi()
        {
            MD5CryptoServiceProvider MyMD5 = new MD5CryptoServiceProvider();
            try
            {
                Byte[] MyMD5_Str = MyMD5.ComputeHash(Encoding.UTF8.GetBytes(this.md5Str));
                this.MyDesStr = Encoding.UTF8.GetString(MyMD5_Str);
            }
            catch (Exception Error)
            {
                this.messAge ="MD5加密出錯:"+ Error.Message;
            }

        }

        #endregion
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.