C#DES加密

來源:互聯網
上載者:User

標籤:解密   length   sub   new   stream   exception   nal   rac   return   

記錄一下

 

DES加密

public static string DESEncrypt(string Data, string key)        {            return DESEncrypt(Data, key, "utf-8");        }/// <summary>        /// DES密碼編譯演算法        /// </summary>        /// <param name="Data">加密明文</param>        /// <param name="key">密鑰長度為8個字元</param>        /// <param name="charset">字元編碼</param>        /// <returns>返回密文</returns>        public static string DESEncrypt(string Data, string key, string charset)        {            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            des.Key = ASCIIEncoding.ASCII.GetBytes(key);            des.IV = ASCIIEncoding.ASCII.GetBytes(key);            byte[] inputByteArray = Encoding.GetEncoding(charset).GetBytes(Data);            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();            byte[] ret = ms.ToArray();            cs.Close();            ms.Close();            return BytesToHexString(ret);        } public static string BytesToHexString(byte[] bytes)        {            System.Text.StringBuilder s = new System.Text.StringBuilder();            foreach (byte b in bytes)            {                s.Append(b.ToString("x2").ToUpper());            }            return s.ToString();        }

解密:

 public static string DESDecrypt(string Data, string key)        {            return DESDecrypt(Data, key, "utf-8");        }        /// <summary>        /// DES 解密演算法        /// </summary>        /// <param name="Data">密文</param>        /// <param name="key">密鑰長度為8個字元</param>        /// <param name="charset">字元編碼</param>        /// <returns>明文</returns>        public static string DESDecrypt(string Data, string key, string charset)        {            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            des.Key = ASCIIEncoding.ASCII.GetBytes(key);            des.IV = ASCIIEncoding.ASCII.GetBytes(key);            byte[] inputByteArray = HexStringToBytes(Data);            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();            byte[] ret = ms.ToArray();            cs.Close();            ms.Close();            return Encoding.GetEncoding(charset).GetString(ret);        } public static byte[] HexStringToBytes(string hexString)        {            if (hexString == null)            {                throw new ArgumentNullException("hexString");            }            if ((hexString.Length & 1) != 0)            {                throw new ArgumentOutOfRangeException("hexString", hexString, "hexString must contain an even number of characters.");            }            byte[] result = new byte[hexString.Length / 2];            for (int i = 0; i < hexString.Length; i += 2)            {                result[i / 2] = byte.Parse(hexString.Substring(i, 2), NumberStyles.HexNumber);            }            return result;        }

 

C#DES加密

相關文章

聯繫我們

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