Exemplo n.º 1
0
        /// <summary>
        /// RSA解密
        /// </summary>
        /// <param name="encryptData">密文</param>
        /// <param name="privateKey">RSA私钥</param>
        /// <param name="isBase64">密文数据是否用Base64编码</param>
        /// <returns></returns>
        public static string RSADecrypt(string encryptData, string privateKey = DefaultRSAPublicKey, bool isBase64 = false)
        {
            byte[] bData;
            if (isBase64)
            {
                bData = Convert.FromBase64String(encryptData);
            }
            else
            {
                bData = ByteStrConvertHelper.ToBytes(encryptData);
            }
            var rsa = new RSACryptoServiceProvider();

            rsa.FromXmlString(privateKey);
            var cipherbytes = rsa.Decrypt(bData, false);

            return(Encoding.UTF8.GetString(cipherbytes));
        }
Exemplo n.º 2
0
        /// <summary>
        /// RC2解密
        /// </summary>
        /// <param name="encryptStr">明文字符串</param>
        /// <param name="key">加密密钥支持40~128长度,可以每8位递增(5到16个长度的字符串)</param>
        /// <param name="iv">加密向量64位以上(8个字节上去字符串)</param>
        /// <param name="isBase64Code">是否是Base64编码,否则是16进制编码</param>
        /// <param name="mode">加密模式</param>
        /// <param name="padding">填充模式</param>
        /// <returns>明文</returns>
        public static string RC2Decrypt(string encryptStr, string key = DefaultRC2Key, string iv = DefaultRC2IV, bool isBase64Code = true, CipherMode mode = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7)
        {
            byte[] bKey = Encoding.UTF8.GetBytes(key);
            byte[] bIV  = Encoding.UTF8.GetBytes(iv);
            byte[] byteArray;
            if (isBase64Code)
            {
                byteArray = Convert.FromBase64String(encryptStr);
            }
            else
            {
                byteArray = ByteStrConvertHelper.ToBytes(encryptStr);
            }

            string decrypt = null;
            var    rc2     = new RC2CryptoServiceProvider();

            try
            {
                rc2.Mode    = mode;
                rc2.Padding = padding;
                using (var mStream = new MemoryStream())
                {
                    using (var cStream = new CryptoStream(mStream, rc2.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
                    {
                        cStream.Write(byteArray, 0, byteArray.Length);
                        cStream.FlushFinalBlock();
                        decrypt = Encoding.UTF8.GetString(mStream.ToArray());
                    }
                }
            }
            catch { }
            rc2.Clear();

            return(decrypt);
        }
Exemplo n.º 3
0
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="encryptStr">密文字符串</param>
        /// <param name="key">加密密钥支持128(16字节)、192(24字节)、256位(32字节)的key</param>
        /// <param name="iv">加密向量(16到19字节)</param>
        /// <param name="isBase64Code">是否是Base64编码,否则是16进制编码</param>
        /// <param name="mode">加密模式</param>
        /// <param name="padding">填充模式</param>
        /// <returns>明文字节数组</returns>
        public static byte[] AESDecryptToBytes(string encryptStr, string key = DefaultAESKey, string iv = DefaultAESIV, bool isBase64Code = true, CipherMode mode = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7)
        {
            byte[] bKey = Encoding.UTF8.GetBytes(key);
            byte[] bIV  = Encoding.UTF8.GetBytes(iv);
            byte[] byteArray;
            if (isBase64Code)
            {
                byteArray = Convert.FromBase64String(encryptStr);
            }
            else
            {
                byteArray = ByteStrConvertHelper.ToBytes(encryptStr);
            }

            byte[] decrypt = null;
            var    aes     = Rijndael.Create();

            try
            {
                aes.Mode    = mode;
                aes.Padding = padding;
                using (var mStream = new MemoryStream())
                {
                    using (var cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
                    {
                        cStream.Write(byteArray, 0, byteArray.Length);
                        cStream.FlushFinalBlock();
                    }
                    decrypt = mStream.ToArray();
                }
            }
            catch { }
            aes.Clear();

            return(decrypt);
        }