DES加密解密字符串

46 篇文章 1 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;//引入命名空间,加密 
using System.IO;

namespace DES加密_解密字符串
{

    /// <summary>
    /// DES加密解密,默认有密钥,只需传入任意的字符串密钥
    /// </summary>
    public static class DES
    {
        //默认密钥向量
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /// <summary>
        /// 默认密钥
        /// </summary>
        private static string miyue = "09@/*!^-+123";


        #region DES加密字符串

        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的16进制字符串,失败返回源串</returns>
        public static string EncryptDES(string encryptString)
        {
            try
            {
                //string encryptKey加密密钥参数,我将该参数这里去掉了,用了默认密钥
                //byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbKey = Encoding.UTF8.GetBytes(miyue);//加密密钥          
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                byte[] tembyte = mStream.ToArray();
                StringBuilder sb = new StringBuilder();
                foreach (byte item in tembyte)
                {
                    sb.AppendFormat("{0:x2}", item);
                }
                return sb.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        #endregion

        #region DES解密字符串

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的16进制字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string DecryptDES(string decryptString)
        {
            try
            {
                //string decryptKey解密密钥参数,我将该参数这里去掉了,用了默认密钥
                //byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbKey = Encoding.UTF8.GetBytes(miyue);//解密密钥          
                byte[] rgbIV = Keys;
                decryptString = decryptString.Replace(" ", "");
                decryptString = decryptString.Replace("\r\n", "");
                string txt16 = decryptString;
                byte[] inputByteArray = new byte[txt16.Length / 2];
                for (int i = 0; i < inputByteArray.Length; i++)
                {
                    inputByteArray[i] = Convert.ToByte(txt16.Substring(i * 2, 2), 16);
                }
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch (Exception)
            {
                throw;
            }
        } 
        #endregion

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值