C# DES 加密/解密類庫,支援檔案和中文/UNICODE字元,返回BASE64編碼字串

來源:互聯網
上載者:User
using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Security;using System.Security.Cryptography;/*---------------------------------------------- *  DES加密、解密類庫,字串加密結果使用BASE64編碼返回,支援檔案的加密和解密 *  作者: 三角貓/DeltaCat *  網址: http://www.zu14.cn *  轉載務必保留此資訊 * --------------------------------------------- */namespace ZU14{    public sealed class DES    {        string iv = "1234的yzo";        string key = "123在yzo";        /// <summary>        /// DES加密位移量,必須是>=8位長的字串        /// </summary>        public string IV        {            get { return iv; }            set { iv = value; }        }        /// <summary>        /// DES加密的私密金鑰,必須是8位長的字串        /// </summary>        public string Key        {            get { return key; }            set { key = value; }        }        /// <summary>        /// 對字串進行DES加密        /// </summary>        /// <param name="sourceString">待加密的字串</param>        /// <returns>加密後的BASE64編碼的字串</returns>        public string Encrypt(string sourceString)        {            byte[] btKey = Encoding.Default.GetBytes(key);            byte[] btIV = Encoding.Default.GetBytes(iv);            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            using (MemoryStream ms = new MemoryStream())            {                byte[] inData = Encoding.Default.GetBytes(sourceString);                try                {                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))                    {                        cs.Write(inData, 0, inData.Length);                        cs.FlushFinalBlock();                    }                    return Convert.ToBase64String(ms.ToArray());                }                catch                {                    throw;                }            }        }        /// <summary>        /// 對DES加密後的字串進行解密        /// </summary>        /// <param name="encryptedString">待解密的字串</param>        /// <returns>解密後的字串</returns>        public string Decrypt(string encryptedString)        {            byte[] btKey = Encoding.Default.GetBytes(key);            byte[] btIV = Encoding.Default.GetBytes(iv);            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            using (MemoryStream ms = new MemoryStream())            {                byte[] inData = Convert.FromBase64String(encryptedString);                try                {                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))                    {                        cs.Write(inData, 0, inData.Length);                        cs.FlushFinalBlock();                    }                    return Encoding.Default.GetString(ms.ToArray());                }                catch                {                    throw;                }            }        }        /// <summary>        /// 對檔案內容進行DES加密        /// </summary>        /// <param name="sourceFile">待加密的檔案絕對路徑</param>        /// <param name="destFile">加密後的檔案儲存的絕對路徑</param>        public void EncryptFile(string sourceFile, string destFile)        {            if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的檔案路徑不存在!", sourceFile);            byte[] btKey = Encoding.Default.GetBytes(key);            byte[] btIV = Encoding.Default.GetBytes(iv);            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            byte[] btFile = File.ReadAllBytes(sourceFile);            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))            {                try                {                    using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))                    {                        cs.Write(btFile, 0, btFile.Length);                        cs.FlushFinalBlock();                    }                }                catch                {                    throw;                }                finally                {                    fs.Close();                }            }        }        /// <summary>        /// 對檔案內容進行DES加密,加密後覆蓋掉原來的檔案        /// </summary>        /// <param name="sourceFile">待加密的檔案的絕對路徑</param>        public void EncryptFile(string sourceFile)        {            EncryptFile(sourceFile, sourceFile);        }        /// <summary>        /// 對檔案內容進行DES解密        /// </summary>        /// <param name="sourceFile">待解密的檔案絕對路徑</param>        /// <param name="destFile">解密後的檔案儲存的絕對路徑</param>        public void DecryptFile(string sourceFile, string destFile)        {            if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的檔案路徑不存在!", sourceFile);            byte[] btKey = Encoding.Default.GetBytes(key);            byte[] btIV = Encoding.Default.GetBytes(iv);            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            byte[] btFile = File.ReadAllBytes(sourceFile);            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))            {                try                {                    using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))                    {                        cs.Write(btFile, 0, btFile.Length);                        cs.FlushFinalBlock();                    }                }                catch                {                    throw;                }                finally                {                    fs.Close();                }            }        }        /// <summary>        /// 對檔案內容進行DES解密,加密後覆蓋掉原來的檔案        /// </summary>        /// <param name="sourceFile">待解密的檔案的絕對路徑</param>        public void DecryptFile(string sourceFile)        {            DecryptFile(sourceFile, sourceFile);        }    }}

使用的例子

ZU14.DES des = new ZU14.DES();des.IV = "abcd哈哈笑";des.Key = "必須八位";string es = des.Encrypt("在");Console.WriteLine(es);Console.Write(des.Decrypt(es));des.EncryptFile(@"d:\a.txt", @"d:\b.txt");des.DecryptFile(@"d:\b.txt");Console.ReadKey(true); 

OK了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.