DES 加密 解密

 public static class Common
    {
/// <summary>
        /// 密钥
        /// </summary>
        public static string key = "@#$!";


        #region 解密
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Decode(string str, string key)
        {
            string sss = "";
            byte[] keyBytes = Encoding.Unicode.GetBytes(key);
            byte[] inputByteArray = Convert.FromBase64String(str);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyBytes), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            sss = Encoding.Unicode.GetString(mStream.ToArray()); //Unicode编码
            return sss;
        }

        #endregion

        #region 加密
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Encode(string str, string key)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] btKey = Encoding.Unicode.GetBytes(key); //向量   密钥
            byte[] buffer = Encoding.Unicode.GetBytes(str); //Unicode编码
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(btKey, btKey), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Convert.ToBase64String(stream.ToArray());
        }
        #endregion


        #region
        public static string GetSubString(string string_0, int int_0, string string_1)
        {
            return GetSubString(string_0, 0, int_0, string_1);
        }

        public static string GetSubString(string string_0, int int_0, int int_1, string string_1)
        {
            string str = string_0;
            byte[] bytes = Encoding.UTF8.GetBytes(string_0);
            foreach (char ch in Encoding.UTF8.GetChars(bytes))
            {
                if (((ch > '') && (ch < '')) || ((ch > 0xac00) && (ch < 0xd7a3)))
                {
                    if (int_0 < string_0.Length)
                    {
                        return string_0.Substring(int_0, ((int_1 + int_0) > string_0.Length) ? (string_0.Length - int_0) : int_1);
                    }
                    return "";
                }
            }
            if (int_1 < 0)
            {
                return str;
            }
            byte[] sourceArray = Encoding.Default.GetBytes(string_0);
            if (sourceArray.Length <= int_0)
            {
                return str;
            }
            int length = sourceArray.Length;
            if (sourceArray.Length <= (int_0 + int_1))
            {
                int_1 = sourceArray.Length - int_0;
                string_1 = "";
            }
            else
            {
                length = int_1 + int_0;
            }
            int num2 = int_1;
            int[] numArray = new int[int_1];
            byte[] destinationArray = null;
            int num3 = 0;
            for (int i = int_0; i < length; i++)
            {
                if (sourceArray[i] > 0x7f)
                {
                    num3++;
                    if (num3 == 3)
                    {
                        num3 = 1;
                    }
                }
                else
                {
                    num3 = 0;
                }
                numArray[i] = num3;
            }
            if ((sourceArray[length - 1] > 0x7f) && (numArray[int_1 - 1] == 1))
            {
                num2 = int_1 + 1;
            }
            destinationArray = new byte[num2];
            Array.Copy(sourceArray, int_0, destinationArray, 0, num2);
            return (Encoding.Default.GetString(destinationArray) + string_1);
        }

        #endregion
    }

 

转载于:https://www.cnblogs.com/wugang/archive/2013/03/13/2957650.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值