DES加密与机密(C#)

using System;
using System.Text;
*****


namespace namespace.Utils.Security
{
    public class PackageKey
    {


        private  const string basekey = "%$";


        #region DES加密


        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="expressly">明文</param>
        /// <param name="keySuffix">密钥后缀</param>
        /// <returns>密文字节数组,如为空则加密失败</returns>
        public static byte[] EncryptDesToken(string expressly, string keySuffix)
        {
            string desKey = basekey+ keySuffix;


            byte[] keyBytes = Encoding.UTF8.GetBytes(desKey);
            byte[] expresslyBytes = Encoding.UTF8.GetBytes(expressly);
            try
            {
                return DesHelper.Encrypt(expresslyBytes, keyBytes);
            }
            catch (Exception)
            {
                return null;
            }
        }


        public static byte[] EncryptDesToken(string expressly, string keySuffix,string keyPrefix)
        {
            string desKey = keyPrefix + keySuffix;


            byte [] keyBytes = Encoding.UTF8.GetBytes(desKey);
            byte [] expresslyBytes = Encoding.UTF8.GetBytes(expressly);
            try
            {
                return DesHelper.Encrypt(expresslyBytes, keyBytes);
            }
            catch ( Exception )
            {
                return null;
            }
        }


        public static byte[] EncryptDesToken(string expressly, string keySuffix,Encoding encode)
        {
                  
            if(encode==Encoding.UTF8)
            {
                return EncryptDesToken(expressly, keySuffix);
              
            }
            else
            {
                string desKey = basekey + keySuffix;
                byte[] keyBytes = Encoding.UTF8.GetBytes(desKey);
                byte[] expresslyBytes;
                expresslyBytes = Encoding.Convert(encode, Encoding.UTF8, encode.GetBytes(expressly));
                try
                {
                    return DesHelper.Encrypt(expresslyBytes, keyBytes);
                }
                catch (Exception)
                {
                    return null;
                }
            }       
        }
        #endregion


        #region DES解密


        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="ciphertextBytes">密文字节数组</param>
        /// <param name="keySuffix"></param>
        /// <returns>明文字节数组,如为空则解密失败</returns>
        public static byte[] DecryptDesToken(byte[] ciphertextBytes, string keySuffix)
        {
            string desKey = basekey+keySuffix;


            byte[] keyBytes = Encoding.UTF8.GetBytes(desKey);
            try
            {
                return DesHelper.Decrypt(ciphertextBytes, keyBytes); 
            }
            catch (Exception ex)
            {
                LoggerHelper.WriteRequestLog(string.Format("解密失败密钥是:" + desKey + ". ex:" + ex.ToString()));
                return null;
            }
        }
        #endregion


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值