互联网开发之:一套完整的加解密和数据签名


        public static byte[] MD5(string value)
        {
            if (string.IsNullOrEmpty(value))
                return null;
            MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
            byte[] input, output;
            input = Encoding.UTF8.GetBytes(value);
            output = MD5.ComputeHash(input);
            byte[] inputbuffer = new byte[24];
            for (int i = 0; i < output.Length; i++)
            {
                if (i < 8)
                {
                    inputbuffer[i] = inputbuffer[16 + i] = output[i];
                }
                else
                {
                    inputbuffer[i] = output[i];
                }

            }
            return inputbuffer;
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="sourceValue"></param>
        /// <param name="key">明文密钥(需用MD5计算的哈希值16位+前8位作为算法最终密钥)</param>
        /// <returns></returns>
        public static string Encrypt3DES_V2(string sourceValue, string key)
        {
            string str = "";
            try
            {
                TripleDESCryptoServiceProvider tripleDESCryptography = new TripleDESCryptoServiceProvider();
                tripleDESCryptography.Key = MD5(key);
                tripleDESCryptography.Mode = CipherMode.ECB;
                tripleDESCryptography.Padding = PaddingMode.PKCS7;
                ICryptoTransform DESEncrypt = tripleDESCryptography.CreateEncryptor();
                byte[] buffer = Encoding.UTF8.GetBytes(sourceValue);
                str = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch
            {
            }
            return str;
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="sourceValue"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Decrypt3DES_V2(string sourceValue, string key)
        {
            string result = "";
            try
            {
                TripleDESCryptoServiceProvider tripleDESCryptography = new TripleDESCryptoServiceProvider();
                tripleDESCryptography.Key = MD5(key);
                tripleDESCryptography.Mode = CipherMode.ECB;
                tripleDESCryptography.Padding = PaddingMode.PKCS7;
                ICryptoTransform DESDecrypt = tripleDESCryptography.CreateDecryptor();
                byte[] buffer = Convert.FromBase64String(sourceValue);
                result = Encoding.GetEncoding("GB2312").GetString(DESDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch
            {
            }
            return result;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值