C#DES加密

2021-02-16 17:18

阅读:654

标签:解密   length   sub   new   stream   exception   nal   rac   return   

记录一下

 

DES加密

public static string DESEncrypt(string Data, string key)
        {
            return DESEncrypt(Data, key, "utf-8");
        }

/// 
        /// DES加密算法
        /// 
        /// 加密明文
        /// 密钥长度为8个字符
        /// 字符编码
        /// 返回密文
        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");
        }


        /// 
        /// DES 解密算法
        /// 
        /// 密文
        /// 密钥长度为8个字符
        /// 字符编码
        /// 明文
        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 2)
            {
                result[i / 2] = byte.Parse(hexString.Substring(i, 2), NumberStyles.HexNumber);
            }

            return result;
        }

 

C#DES加密

标签:解密   length   sub   new   stream   exception   nal   rac   return   

原文地址:https://www.cnblogs.com/Cein/p/8406442.html


评论


亲,登录后才可以留言!