文档章节

一般加密解密算法

Kiddy-Adult
 Kiddy-Adult
发布于 2017/04/22 13:55
字数 2967
阅读 8
收藏 0
View Code
public class NmSecurity
    {
        /**************************************************
            * FileName: NmSecurity.cs
            * Version: 1.00
            * Author: Kria 
            * Date: 2009-3-4
            * Last Modified Date: 2009-3-6
            * Function: 
            * CopyRight (c) Netmarch Company 2000-2003
            *            All rights reserved
        **************************************************/

        private static string encodingType = ConfigurationManager.AppSettings["EncryptStringEncoding"];
        
        //------------------------------------加密部分开始----------------------------------------------------
        /// <summary>
        /// 使用MD5加密方法加密数据
        /// </summary>
        /// <param name="sourceData">用户密码等需要被MD5加密的源数据</param>
        /// <returns>MD5加密后的形成字符串</returns>
        public static string Md5(string sourceData)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(sourceData, "MD5");
        }

        /// <summary>
        /// 返回二进制数组的Hash值
        /// </summary>
        /// <param name="sourceData">二进制数组</param>
        /// <returns>产生的Hash值</returns>
        public static string Hash(byte[] sourceData)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            return BitConverter.ToString(md5.ComputeHash(sourceData)).Replace("-", "").ToLower();
        }

        /// <summary>
        /// 获取随机对称加密密钥
        /// </summary>
        /// <returns>密钥字符串</returns>
        public static string GenericSymmetricKey()
        {
            SymmetricAlgorithm sa = Rijndael.Create();
            sa.GenerateKey();
            return Convert.ToBase64String(sa.Key);
        }


        /// <summary>
        /// 获取随机非对称加密密钥
        /// </summary>
        /// <returns>密钥字符串</returns>
        public static string GenericAsymmetricKey()
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            return Convert.ToBase64String(rsa.ExportCspBlob(true));
        }

        /// <summary>
        /// 从带私钥的密钥中获取只包含公钥的密钥
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetPublicKey(string key)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportCspBlob(Convert.FromBase64String(key));
            return Convert.ToBase64String(rsa.ExportCspBlob(false));

        }

        /// <summary>
        /// 二进制流对二进制流的对称加密
        /// </summary>
        /// <param name="sourceData">需要加密的源数据</param>
        /// <param name="key">16位或24位密钥,或者使用GenericSymmetricKey方法产生随机对称密钥</param>
        /// <returns>加密后的二进制流形式的字节数组</returns>
        public static byte[] SymmetricEncryptBinaryToBinary(byte[] sourceData, string key)
        {
            try
            {
                SymmetricAlgorithm sa = Rijndael.Create();
                sa.Mode = CipherMode.ECB;
                sa.Padding = PaddingMode.Zeros;
                try
                {
                    sa.Key = Convert.FromBase64String(key);
                }
                catch
                {
                    sa.Key = Encoding.GetEncoding(encodingType).GetBytes(key);
                }
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(sourceData, 0, sourceData.Length);
                cs.Close();
                byte[] encryptBinaryData = ms.ToArray();
                ms.Close();
                return encryptBinaryData;
            }
            catch
            {
                throw new Exception("您所使用的密钥无效!");
            }
        }

        /// <summary>
        /// 二进制流对字符串的对称加密
        /// </summary>
        /// <param name="sourceData">需要加密的源数据</param>
        /// <param name="key">16位或24位密钥,或者使用GenericSymmetricKey方法产生随机对称密钥</param>
        /// <returns>加密后的形成的字符串</returns>
        public static string SymmetricEncryptBinaryToString(byte[] sourceData, string key)
        {
            byte[] encryptBinaryData = SymmetricEncryptBinaryToBinary(sourceData, key);
            return Convert.ToBase64String(encryptBinaryData);
        }

        /// <summary>
        /// 字符串对二进制流的对称加密
        /// </summary>
        /// <param name="sourceData">需要加密的源数据</param>
        /// <param name="key">16位或24位密钥,或者使用GenericSymmetricKey方法产生随机对称密钥</param>
        /// <returns>加密后的二进制流形式的字节数组</returns>
        public static byte[] SymmetricEncryptStringToBinary(string sourceData, string key)
        {
                byte[] byteData = Encoding.GetEncoding(encodingType).GetBytes(sourceData);
                return SymmetricEncryptBinaryToBinary(byteData, key);
        }

        /// <summary>
        /// 字符串对字符串的对称加密
        /// </summary>
        /// <param name="sourceData">需要加密的字符串</param>
        /// <param name="key">16位或24位密钥,或者使用GenericSymmetricKey方法产生随机对称密钥</param>
        /// <returns>加密后形成的字符串</returns>
        public static string SymmetricEncryptStringToString(string sourceData, string key)
        {
            try
            {
                byte[] byteData = Encoding.GetEncoding(encodingType).GetBytes(sourceData);
                byte[] encryptBinaryData = SymmetricEncryptBinaryToBinary(byteData, key);
                return Convert.ToBase64String(encryptBinaryData);
            }
            catch
            {
                throw new Exception("您所使用的密钥无效!");
            }
        }



        /// <summary>
        /// 二进制流对二进制流的非对称加密
        /// </summary>
        /// <param name="sourceData">需要加密的源数据</param>
        /// <param name="key">RSA密钥或者使用GenericAsymmetricKey方法产生随机非对称密钥</param>
        /// <returns>加密后的二进制流形式的字节数组</returns>
        public static byte[] AsymmetricEncryptBinaryToBinary(byte[] sourceData, string key)
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.ImportCspBlob(Convert.FromBase64String(key));
                byte[] byteEncrypt = rsa.Encrypt(sourceData, false);
                return byteEncrypt;
            }
            catch
            {
                throw new Exception("您所使用的密钥无效!");
            }
        }

        /// <summary>
        /// 二进制流对字符串的非对称加密
        /// </summary>
        /// <param name="sourceData">需要加密的源数据</param>
        /// <param name="key">RSA密钥或者使用GenericAsymmetricKey方法产生随机非对称密钥</param>
        /// <returns>加密后的形成的字符串</returns>
        public static string AsymmetricEncryptBinaryToString(byte[] sourceData, string key)
        {

            byte[] byteEncrypt = AsymmetricEncryptBinaryToBinary(sourceData, key);
            return Convert.ToBase64String(byteEncrypt);
        }

        /// <summary>
        /// 字符串对二进制流的非对称加密
        /// </summary>
        /// <param name="sourceData">需要加密的字符串</param>
        /// <param name="key">RSA密钥或者使用GenericAsymmetricKey方法产生随机非对称密钥</param>
        /// <returns>加密后的二进制流形式的字节数组</returns>
        public static byte[] AsymmetricEncryptStringToBinary(string sourceData, string key)
        {

            byte[] byteData = Encoding.GetEncoding(encodingType).GetBytes(sourceData);
            byte[] byteEncrypt = AsymmetricEncryptBinaryToBinary(byteData, key);
            return byteEncrypt;
        }

        /// <summary>
        /// 字符串对字符串的非对称加密
        /// </summary>
        /// <param name="sourceData">需要加密的字符串</param>
        /// <param name="key">RSA密钥或者使用GenericAsymmetricKey方法产生随机非对称密钥</param>
        /// <returns>加密后的二进制流形式的字节数组</returns>
        public static string AsymmetricEncryptStringToString(string sourceData, string key)
        {

            byte[] byteData = Encoding.GetEncoding(encodingType).GetBytes(sourceData);
            byte[] byteEncrypt = AsymmetricEncryptBinaryToBinary(byteData, key);
            return Convert.ToBase64String(byteEncrypt);

        }

        //--------------------------------------加密部分结束-----------------------------------------------------------
        //--------------------------------------解密部分开始-----------------------------------------------------------

        /// <summary>
        /// 二进制流对二进制流的对称解密
        /// </summary>
        /// <param name="sourceData">加密后的二进制流</param>
        /// <param name="key">对应加密的密钥</param>
        /// <returns>解密后的二进制流</returns>
        public static byte[] SymmetricDecryptBinaryToBinary(byte[] sourceData, string key)
        {
            try
            {
                SymmetricAlgorithm sa = Rijndael.Create();
                sa.Mode = CipherMode.ECB;
                sa.Padding = PaddingMode.Zeros;
                try
                {
                    sa.Key = Convert.FromBase64String(key);
                }
                catch
                {
                    sa.Key = Encoding.GetEncoding(encodingType).GetBytes(key);
                }
                MemoryStream ms = new MemoryStream(sourceData);
                CryptoStream cs = new CryptoStream(ms, sa.CreateDecryptor(), CryptoStreamMode.Read);
                byte[] decryptBinaryData = new byte[sourceData.Length];
                cs.Read(decryptBinaryData, 0, sourceData.Length);
                int lastIndex = decryptBinaryData.Length;
                while (lastIndex > 0 && decryptBinaryData[lastIndex - 1] == (byte)0)
                {
                    lastIndex--;
                }
                byte[] subDecryptData = new byte[lastIndex];
                System.Array.Copy(decryptBinaryData, subDecryptData, lastIndex);
                return subDecryptData;
            }
            catch
            {
                throw new Exception("您所使用的密钥无效!");
            }
        }

        /// <summary>
        /// 二进制流对字符串的对称解密
        /// </summary>
        /// <param name="sourceData">加密后的二进制流</param>
        /// <param name="key">对应加密的密钥</param>
        /// <returns>经过解密后的字符串</returns>
        public static string SymmetricDecryptBinaryToString(byte[] sourceData, string key)
        {
            byte[] decryptBinaryData=SymmetricDecryptBinaryToBinary(sourceData, key);
            string decryptData = Encoding.GetEncoding(encodingType).GetString(decryptBinaryData);
            return decryptData;
        }

        /// <summary>
        /// 字符串对二进制流的对称解密
        /// </summary>
        /// <param name="sourceData">加密后的字符串</param>
        /// <param name="key">对应加密的密钥</param>
        /// <returns>经过解密后的二进制流</returns>
        public static byte[] SymmetricDecryptStringToBinary(string sourceData, string key)
        {
            byte[] encryptBinaryData = Convert.FromBase64String(sourceData);
            byte[] decryptBinaryData = SymmetricDecryptBinaryToBinary(encryptBinaryData, key);
            return decryptBinaryData;
        }

        /// <summary>
        /// 字符串对字符串的对称解密
        /// </summary>
        /// <param name="sourceData">加密后的字符串</param>
        /// <param name="key">对应加密的密钥</param>
        /// <returns>经过解密后的字符串</returns>
        public static string SymmetricDecryptStringToString(string sourceData, string key)
        {
            byte[] encryptBinaryData = Convert.FromBase64String(sourceData);
            byte[] decryptBinaryData = SymmetricDecryptBinaryToBinary(encryptBinaryData, key);
            string decryptData = Encoding.GetEncoding(encodingType).GetString(decryptBinaryData);
            return decryptData;
        }

        /// <summary>
        /// 二进制流对二进制流的非对称解密
        /// </summary>
        /// <param name="sourceData">加密后的二进制流</param>
        /// <param name="key">对应加密的密钥</param>
        /// <returns>解密后的二进制流</returns>
        public static byte[] AsymmetricDecryptBinaryToBinary(byte[] sourceData, string key)
        {
            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.ImportCspBlob(Convert.FromBase64String(key));
                byte[] byteDecrypt = rsa.Decrypt(sourceData, false);
                return byteDecrypt;
            }
            catch
            {
                throw new Exception("您所使用的密钥无效!");
            }
        }

        /// <summary>
        /// 二进制流对字符串的非对称解密
        /// </summary>
        /// <param name="sourceData">加密后的二进制流</param>
        /// <param name="key">对应加密的密钥</param>
        /// <returns>经过解密后的字符串</returns>
        public static string AsymmetricDecryptBinaryToString(byte[] sourceData, string key)
        {
            byte[] byteDecrypt = AsymmetricDecryptBinaryToBinary(sourceData, key);
            return Encoding.GetEncoding(encodingType).GetString(byteDecrypt);
        }

        /// <summary>
        /// 字符串对而二进制流的非对称解密
        /// </summary>
        /// <param name="sourceData">加密后的字符串</param>
        /// <param name="key">对应加密的密钥</param>
        /// <returns>解密后的二进制流</returns>
        public static byte[] AsymmetricDecryptStringToBinary(string sourceData, string key)
        {
            byte[] byteData = Convert.FromBase64String(sourceData);
            byte[] byteDecrypt = AsymmetricDecryptBinaryToBinary(byteData, key);
            return byteDecrypt;
        }

        /// <summary>
        /// 字符串对字符串的非对称解密
        /// </summary>
        /// <param name="sourceData">加密后的字符串</param>
        /// <param name="key">对应加密的密钥</param>
        /// <returns>经过解密后的字符串</returns>
        public static string AsymmetricDecryptStringToString(string sourceData, string key)
        {
            byte[] byteData = Convert.FromBase64String(sourceData);
            byte[] byteDecrypt = AsymmetricDecryptBinaryToBinary(byteData, key);
            return Encoding.GetEncoding(encodingType).GetString(byteDecrypt);
        }

        //----------------------------------------------带签名验证的加密解密---------------------------------------------

        /// <summary>
        /// 二进制流对字符串的带签名的验证加密
        /// </summary>
        /// <param name="sourceData">需要加密的二进制流源数据</param>
        /// <param name="localKey">本地密钥</param>
        /// <param name="remoteKey">远程密钥</param>
        /// <returns>加密后的数据字符串</returns>
        public static string SignatureEncryptBinaryToString(byte[] sourceData, string localKey, string remoteKey)
        {
            try
            {
                RSACryptoServiceProvider rsaLocal = new RSACryptoServiceProvider();
                RSACryptoServiceProvider rsaRemote = new RSACryptoServiceProvider();
                rsaLocal.ImportCspBlob(Convert.FromBase64String(localKey));
                rsaRemote.ImportCspBlob(Convert.FromBase64String(remoteKey));
                SymmetricAlgorithm sa = Rijndael.Create();
                sa.GenerateIV();
                //将对称密钥使用非对称加密算法加密并转换成base64
                string saKey = Convert.ToBase64String(rsaRemote.Encrypt(sa.Key, false));
                //用默认密钥进行对称加密
                sa.Mode = CipherMode.ECB;
                sa.Padding = PaddingMode.Zeros;
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(sourceData, 0, sourceData.Length);
                cs.Close();
                byte[] encryptBinaryData = ms.ToArray();
                ms.Close();
                string encryptData = Convert.ToBase64String(encryptBinaryData);
                //签名
                string sinagture = Convert.ToBase64String(rsaLocal.SignData(encryptBinaryData, new SHA1CryptoServiceProvider()));
                //返回结果:密文+密钥+签名
                return encryptData + "," + saKey + "," + sinagture;
            }
            catch
            {
                throw new Exception("您所使用的密钥无效!");
            }
        }

        /// <summary>
        /// 二进制流对二进制流的带签名的验证加密
        /// </summary>
        /// <param name="sourceData">需要加密的二进制流源数据</param>
        /// <param name="localKey">本地密钥</param>
        /// <param name="remoteKey">远程密钥</param>
        /// <returns>加密后的二进制流数据</returns>
        public static byte[] SignatureEncryptBinaryToBinary(byte[] sourceData, string localKey, string remoteKey)
        {
            string encryptResult = SignatureEncryptBinaryToString(sourceData, localKey, remoteKey);
            return Encoding.GetEncoding(encodingType).GetBytes(encryptResult);
        }

        /// <summary>
        /// 字符串对二进制流的带签名的验证加密
        /// </summary>
        /// <param name="sourceData">需要加密的字符串源数据</param>
        /// <param name="localKey">本地密钥</param>
        /// <param name="remoteKey">远程密钥</param>
        /// <returns>加密后的二进制流数据</returns>
        public static byte[] SignatureEncryptStringToBinary(string sourceData, string localKey, string remoteKey)
        {
            //将要加密的数据转换成二进制数据
            byte[] binaryData = Encoding.GetEncoding(encodingType).GetBytes(sourceData);
            string encryptResult = SignatureEncryptBinaryToString(binaryData, localKey, remoteKey);
            return Encoding.GetEncoding(encodingType).GetBytes(encryptResult);
        }

        /// <summary>
        /// 二进制流对字符串的带签名的验证加密
        /// </summary>
        /// <param name="sourceData">需要加密的字符串源数据</param>
        /// <param name="localKey">本地密钥</param>
        /// <param name="remoteKey">远程密钥</param>
        /// <returns>加密后的字符串数据</returns>
        public static string SignatureEncryptStringToString(string sourceData, string localKey, string remoteKey)
        {
            //将要加密的数据转换成二进制数据
            byte[] binaryData = Encoding.GetEncoding(encodingType).GetBytes(sourceData);
            return SignatureEncryptBinaryToString(binaryData, localKey, remoteKey);
        }

        /// <summary>
        /// 字符串对二进制流的带签名的验证解密
        /// </summary>
        /// <param name="sourceData">需要解密的字符串源数据</param>
        /// <param name="localKey">本地密钥</param>
        /// <param name="remoteKey">远程密钥</param>
        /// <returns>解密后的二进制流数据</returns>
        public static byte[] SignatureDecryptStringToBinary(string sourceData, string localKey, string remoteKey)
        {
            try
            {
                if (string.IsNullOrEmpty(sourceData))
                {
                    return null;
                }
                string[] dataParts = sourceData.Split(',');
                if (dataParts.Length != 3)
                {
                    return null;
                }
                string encryptData = dataParts[0];
                string saKey = dataParts[1];
                string sinature = dataParts[2];
                byte[] encryptBinaryData = Convert.FromBase64String(encryptData);

                RSACryptoServiceProvider rsaLocal = new RSACryptoServiceProvider();
                RSACryptoServiceProvider rsaRemote = new RSACryptoServiceProvider();
                rsaLocal.ImportCspBlob(Convert.FromBase64String(localKey));
                rsaRemote.ImportCspBlob(Convert.FromBase64String(remoteKey));
                SymmetricAlgorithm sa = Rijndael.Create();
                //验证签名
                if (!rsaRemote.VerifyData(encryptBinaryData, new SHA1CryptoServiceProvider(), Convert.FromBase64String(sinature)))
                {
                    throw new Exception("验证签名失败!");
                }
                //还原对称加密Key
                sa.Key = rsaLocal.Decrypt(Convert.FromBase64String(saKey), false);
                //用默认密钥进行对称解密
                sa.Mode = CipherMode.ECB;  //块处理模式
                sa.Padding = PaddingMode.Zeros; //末尾数据块的填充模式
                MemoryStream ms = new MemoryStream(encryptBinaryData);
                CryptoStream cs = new CryptoStream(ms, sa.CreateDecryptor(), CryptoStreamMode.Read);
                byte[] decryptBinaryData = new byte[encryptBinaryData.Length];
                cs.Read(decryptBinaryData, 0, encryptBinaryData.Length);
                int lastIndex = decryptBinaryData.Length;
                while (lastIndex > 0 && decryptBinaryData[lastIndex - 1] == (byte)0)
                {
                    lastIndex--;
                }
                byte[] subDecryptData = new byte[lastIndex];
                System.Array.Copy(decryptBinaryData, subDecryptData, lastIndex);
                return subDecryptData;
            }
            catch
            {
                throw new Exception("您所使用的密钥无效!");
            }
        }

        /// <summary>
        /// 二进制流对二进制流的带签名的验证解密
        /// </summary>
        /// <param name="sourceData">需要解密的二进制流源数据</param>
        /// <param name="localKey">本地密钥</param>
        /// <param name="remoteKey">远程密钥</param>
        /// <returns>解密后的二进制流数据</returns>
        public static byte[] SignatureDecryptBinaryToBinary(byte[] sourceData, string localKey, string remoteKey)
        {
            string encryptData = Encoding.GetEncoding(encodingType).GetString(sourceData);
            byte[] decryptBinaryData = SignatureDecryptStringToBinary(encryptData, localKey, remoteKey);
            return decryptBinaryData;
        }

        /// <summary>
        /// 二进制流对字符串的带签名的验证解密
        /// </summary>
        /// <param name="sourceData">需要解密的二进制流源数据</param>
        /// <param name="localKey">本地密钥</param>
        /// <param name="remoteKey">远程密钥</param>
        /// <returns>解密后的字符串数据</returns>
        public static string SignatureDecryptBinaryToString(byte[] sourceData, string localKey, string remoteKey)
        {
            string encryptData = Encoding.GetEncoding(encodingType).GetString(sourceData);
            byte[] decryptBinaryData = SignatureDecryptStringToBinary(encryptData, localKey, remoteKey);
            string decryptData = Encoding.GetEncoding(encodingType).GetString(decryptBinaryData);
            return decryptData;
        }

        /// <summary>
        /// 字符串对字符串的带签名的验证解密
        /// </summary>
        /// <param name="sourceData">需要解密的字符串源数据</param>
        /// <param name="localKey">本地密钥</param>
        /// <param name="remoteKey">远程密钥</param>
        /// <returns>解密后的字符串数据</returns>
        public static string SignatureDecryptStringToString(string sourceData, string localKey, string remoteKey)
        {
            byte[] decryptBinaryData = SignatureDecryptStringToBinary(sourceData, localKey, remoteKey);
            string decryptData = Encoding.GetEncoding(encodingType).GetString(decryptBinaryData);
            return decryptData;
        }




        public static string RsaEncrypt(string data, string localKey, string remoteKey)
        {
            RSACryptoServiceProvider rsaLocal = new RSACryptoServiceProvider();
            RSACryptoServiceProvider rsaRemote = new RSACryptoServiceProvider();
            rsaLocal.ImportCspBlob(Convert.FromBase64String(localKey));
            rsaRemote.ImportCspBlob(Convert.FromBase64String(remoteKey));
            SymmetricAlgorithm sa = Rijndael.Create();
            sa.GenerateIV();

            //将对称密钥使用非对称加密算法加密并转换成base64
            string saKey = Convert.ToBase64String(rsaRemote.Encrypt(sa.Key, false));

            //将要加密的数据转换成二进制数据
            byte[] binaryData = Encoding.UTF8.GetBytes(data);

            byte[] encryptBinaryData = null;
            string encryptData = null;
            string sinagture = null;

            //对称加密开始
            sa.Mode = CipherMode.ECB;  //块处理模式
            sa.Padding = PaddingMode.Zeros; //末尾数据块的填充模式            
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(binaryData, 0, binaryData.Length);
            cs.Close();
            encryptBinaryData = ms.ToArray();
            ms.Close();
            encryptData = Convert.ToBase64String(encryptBinaryData);
            //对称加密结束


            //签名
            sinagture = Convert.ToBase64String(rsaLocal.SignData(encryptBinaryData, new SHA1CryptoServiceProvider()));

            //返回结果:密文+密钥+签名
            return encryptData + "," + saKey + "," + sinagture;
        }

        public static string RsaDecrypt(string data, string localKey, string remoteKey)
        {
            if (string.IsNullOrEmpty(data))
            {
                return null;
            }
            string[] dataParts = data.Split(',');
            if (dataParts.Length != 3)
            {
                return null;
            }
            string encryptData = dataParts[0];
            string saKey = dataParts[1];
            string sinature = dataParts[2];
            byte[] encryptBinaryData = Convert.FromBase64String(encryptData);

            RSACryptoServiceProvider rsaLocal = new RSACryptoServiceProvider();
            RSACryptoServiceProvider rsaRemote = new RSACryptoServiceProvider();
            rsaLocal.ImportCspBlob(Convert.FromBase64String(localKey));
            rsaRemote.ImportCspBlob(Convert.FromBase64String(remoteKey));
            SymmetricAlgorithm sa = Rijndael.Create();

            //验证签名
            if (!rsaRemote.VerifyData(encryptBinaryData, new SHA1CryptoServiceProvider(), Convert.FromBase64String(sinature)))
            {
                return null;
            }

            //还原对称加密Key
            sa.Key = rsaLocal.Decrypt(Convert.FromBase64String(saKey), false);

            //对称解密开始
            sa.Mode = CipherMode.ECB;  //块处理模式
            sa.Padding = PaddingMode.Zeros; //末尾数据块的填充模式
            MemoryStream ms = new MemoryStream(encryptBinaryData);
            CryptoStream cs = new CryptoStream(ms, sa.CreateDecryptor(), CryptoStreamMode.Read);
            byte[] decryptBinaryData = new byte[encryptBinaryData.Length];
            cs.Read(decryptBinaryData, 0, encryptBinaryData.Length);
            int lastIndex = decryptBinaryData.Length;
            while (lastIndex > 0 && decryptBinaryData[lastIndex - 1] == (byte)0)
            {
                lastIndex--;
            }
            string decryptData = Encoding.UTF8.GetString(decryptBinaryData, 0, lastIndex);
            //对称解密结束
            return decryptData;
        }
    } 

 

本文转载自:http://www.cnblogs.com/kongdf/archive/2012/06/27/2566518.html

Kiddy-Adult
粉丝 0
博文 47
码字总数 1637
作品 0
苏州
高级程序员
私信 提问
java服务端、php、C#客户端rsa

最近公司项目需要用到rsa,故对rsa签名算法的一些概念和不同语言的使用进行了研究(具体实现算法则不钻牛角尖了) 对rsa算法的个人理解 什么是RSA签名、验签、加密解密1、rsa算法为非对称加密...

jason-寒江雪
2015/12/14
1K
1
游戏服务器架构系列 - 网关协议加密

一、概要 网关在游戏服务器架构中充当着很重要的角色,根据不同类型项目承担的功能也不一样,主要的功能有以下几种: 1. 压缩:压缩是一件比较耗时操作,放在网关可以减轻游戏压力; 2. 过滤:过...

MaxwellGames
2018/11/05
0
0
计算机软考网络工程师考试第七章网络安全(4)

  鉴别   鉴别是指可靠地验证某个通信参与方的身份是否与他所声称的身份一致的过程,一般通过某种复杂的身份认证协议来实现。   1、口令技术   身份认证标记:PIN保护记忆卡和挑战响...

第七章
2014/09/01
0
0
shiro之编码加密

一、Shiro 提供了base64和16进制字符串编码/解码的API支持: 二、散列算法 散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如MD5、S...

沉默的懒猫
2016/07/07
855
1
浅谈 PHP 中的多种加密技术及代码示例

同样是一道面试答错的问,面试官问我非对称加密算法中有哪些经典的算法? 当时我愣了一下,因为我把非对称加密与单项散列加密的概念弄混淆了,所以更不用说什么非对称加密算法中有什么经典算...

snowing1990
2016/04/13
45
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周六乱弹 —— 早上儿子问我他是怎么来的

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @凉小生 :#今日歌曲推荐# 少点戾气,愿你和这个世界温柔以待。中岛美嘉的单曲《僕が死のうと思ったのは (曾经我也想过一了百了)》 《僕が死の...

小小编辑
今天
2.1K
14
Excption与Error包结构,OOM 你遇到过哪些情况,SOF 你遇到过哪些情况

Throwable 是 Java 中所有错误与异常的超类,Throwable 包含两个子类,Error 与 Exception 。用于指示发生了异常情况。 Java 抛出的 Throwable 可以分成三种类型。 被检查异常(checked Exc...

Garphy
今天
38
0
计算机实现原理专题--二进制减法器(二)

在计算机实现原理专题--二进制减法器(一)中说明了基本原理,现准备说明如何来实现。 首先第一步255-b运算相当于对b进行按位取反,因此可将8个非门组成如下图的形式: 由于每次做减法时,我...

FAT_mt
昨天
40
0
好程序员大数据学习路线分享函数+map映射+元祖

好程序员大数据学习路线分享函数+map映射+元祖,大数据各个平台上的语言实现 hadoop 由java实现,2003年至今,三大块:数据处理,数据存储,数据计算 存储: hbase --> 数据成表 处理: hive --> 数...

好程序员官方
昨天
61
0
tabel 中含有复选框的列 数据理解

1、el-ui中实现某一列为复选框 实现多选非常简单: 手动添加一个el-table-column,设type属性为selction即可; 2、@selection-change事件:选项发生勾选状态变化时触发该事件 <el-table @sel...

everthing
昨天
20
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部