当前位置: 首页>>代码示例>>C#>>正文


C# SymmetricAlgorithm.CreateDecryptor方法代码示例

本文整理汇总了C#中System.Security.Cryptography.SymmetricAlgorithm.CreateDecryptor方法的典型用法代码示例。如果您正苦于以下问题:C# SymmetricAlgorithm.CreateDecryptor方法的具体用法?C# SymmetricAlgorithm.CreateDecryptor怎么用?C# SymmetricAlgorithm.CreateDecryptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.SymmetricAlgorithm的用法示例。


在下文中一共展示了SymmetricAlgorithm.CreateDecryptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DecryptString

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="value">要解密的值</param>
        /// <returns>解密后值</returns>
        public static string DecryptString(string value)
        {
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            //// 设置加密Key
            string txtKey = "tkGGRmBErvc=";
            //// 设置加密IV
            string txtIV = "Kl7ZgtM1dvQ=";
            mcsp = SetEnc();
            byte[] byt2 = Convert.FromBase64String(txtKey);
            mcsp.Key = byt2;
            byte[] byt3 = Convert.FromBase64String(txtIV);
            mcsp.IV = byt3;

            ct = mcsp.CreateDecryptor(mcsp.Key, mcsp.IV);

            byt = Convert.FromBase64String(value);

            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();

            cs.Close();

            return Encoding.UTF8.GetString(ms.ToArray());
        }
开发者ID:hijushen,项目名称:WindowDemo,代码行数:34,代码来源:EncryptHelper.cs

示例2: Decrypt

 private static string Decrypt(byte[] cipher, string passPhrase, string salt, SymmetricAlgorithm algorithm)
 {
     var saltBytes = Encoding.UTF8.GetBytes(salt);
     algorithm.Padding = PaddingMode.None;
     using (algorithm)
     {
         using (var password = new Rfc2898DeriveBytes(passPhrase, saltBytes))
         {
             algorithm.Key = password.GetBytes(algorithm.KeySize / 8);
             algorithm.IV = password.GetBytes(algorithm.BlockSize / 8);
             using (var memStream = new MemoryStream(cipher))
             {
                 using (
                     var cryptoStream = new CryptoStream(memStream, algorithm.CreateDecryptor(),
                                                         CryptoStreamMode.Read))
                 {
                     using (var sr = new StreamReader(cryptoStream))
                     {
                         return sr.ReadToEnd();
                     }
                 }
             }
         }
     }
 }
开发者ID:gilles1977,项目名称:epayment-integration,代码行数:25,代码来源:CryptoUtils.cs

示例3: DecryptString

        public static string DecryptString(string Value, string parKey)
        {
            mCSP = SetEnc();
            string iv = "PenS8UCVF7s=";
            mCSP.IV = Convert.FromBase64String(iv);
            string key = SetLengthString(parKey.ToString(), 32);
            mCSP.Key = Convert.FromBase64String(key);
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            Byte[] byt = new byte[64];
            try
            {
                ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
                byt = Convert.FromBase64String(Value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();

                return Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                throw (new Exception("An error occurred while decrypting string"));
            }
        }
开发者ID:danygolden,项目名称:gianfratti,代码行数:28,代码来源:Crypto.cs

示例4: DecryptBytes

        public static byte[] DecryptBytes(SymmetricAlgorithm symAlg, byte[] inBytes)
        {
            ICryptoTransform xfrm = symAlg.CreateDecryptor();
            byte[] outBlock = xfrm.TransformFinalBlock(inBytes, 0, inBytes.Length);

            return outBlock;
        }
开发者ID:TheNaked,项目名称:Firewind,代码行数:7,代码来源:Encryption.cs

示例5: DecryptStringFromBytes

        /// <summary>
        /// 解密数据.
        /// </summary>
        /// <param name="service"> 解密处理的服务 </param>
        /// <param name="cipherText"> 被加密的字节数组 </param>
        /// <returns> 解密后的文本信息 </returns>
        public string DecryptStringFromBytes(SymmetricAlgorithm service, byte[] cipherText)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");

            // 预期返回的结果.
            string plaintext = null;

            // 创建对称解密器对象。
            ICryptoTransform decryptor = service.CreateDecryptor();

            // Create the streams used for decryption.
            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        // Read the decrypted bytes from the decrypting stream
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }

            // 返回.
            return plaintext;
        }
开发者ID:mahuidong,项目名称:my-csharp-sample,代码行数:35,代码来源:CommonService.cs

示例6: Desencriptar

 public static byte[] Desencriptar(byte[] mensajeEncriptado,
     SymmetricAlgorithm algoritmo)
 {
     int numeroBytesDesencriptados = 0;
     // La clase SymmetricAlgorithm delega el proceso de desencriptación de datos
     // Una instancia de ICryptoTransform transforma texto plano en texto cifrado o vice versa.
     // Las siguiente sentencia demuestra como crear transformaciones usando CreateDecryptor.
     byte[] mensajeDesencriptado = new
     byte[mensajeEncriptado.Length];
     // Crear una ICryptoTransform que puede ser usada para desencriptar datos
     ICryptoTransform desencriptador =
         algoritmo.CreateDecryptor();
     // Procedemos a descifrar el mensaje
     MemoryStream memoryStream = new
     MemoryStream(mensajeEncriptado);
     // Creamos el CryptoStream
     CryptoStream cryptoStream = new CryptoStream(memoryStream,
         desencriptador, CryptoStreamMode.Read);
     // Decrypting data and get the count of plain text bytes.
     numeroBytesDesencriptados = cryptoStream.Read(mensajeDesencriptado, 0, mensajeDesencriptado.Length);
     // Liberamos recursos.
     memoryStream.Close();
     cryptoStream.Close();
     return mensajeDesencriptado;
 }
开发者ID:sancas,项目名称:ProgramacionIII,代码行数:25,代码来源:Program.cs

示例7: DecryptPassword

 /// <summary>
 ///  Decrypts the offline transaction file using the TripleDES cryptography.
 /// </summary>
 /// <param name="Password"> Password to be decrypted</param>
 /// <returns></returns>
 public static string DecryptPassword(string Password)
 {
     DES = new TripleDESCryptoServiceProvider();
     byte[] encryptedBytes = Convert.FromBase64String(Password);
     byte[] decryptedBytes = DES.CreateDecryptor().TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
     return Encoding.UTF8.GetString(decryptedBytes);
 }
开发者ID:nitinkhannas,项目名称:TCESS.ESales,代码行数:12,代码来源:EncryptDecrypt.cs

示例8: CipherTextStealingMode

        /// <summary>
        /// Initialize CipherTextStealingMode with a specific symmetric algorithm
        /// </summary>
        /// <param name="symmetricAlgorithm">The symmetric algorithm</param>
        public CipherTextStealingMode(SymmetricAlgorithm symmetricAlgorithm)
        {
            // in CTS Mode there is no padding
            symmetricAlgorithm.Padding = PaddingMode.None;

            // set the symmetric algorithm's mode to ECB
            // (for single block encryption and decryption)
            symmetricAlgorithm.Mode = CipherMode.ECB;

            // get the symmetric algorithm's block size in bytes
            blockSize = symmetricAlgorithm.BlockSize / 8;
            if (blockSize != symmetricAlgorithm.IV.Length)
            {
                throw new ArgumentException(
                    "The IV size should equal to the block size.");
            }

            // initialize local IV
            iv = symmetricAlgorithm.IV;

            // initialize cipher state using the symmetric algorithms's IV
            cipherState = new byte[blockSize];
            symmetricAlgorithm.IV.CopyTo(cipherState, 0);

            // create encryptor and decryptor
            encryptor = symmetricAlgorithm.CreateEncryptor();
            decryptor = symmetricAlgorithm.CreateDecryptor();
        }
开发者ID:LiuXiaotian,项目名称:WindowsProtocolTestSuites,代码行数:32,代码来源:CipherTextStealingMode.cs

示例9: Decrypt

        private byte[] Decrypt(SymmetricAlgorithm sa, byte[] source)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("DE:CSP:Key[{0}], IV[{1}]",
                Convert.ToBase64String(this.__cspExternal.Key), Convert.ToBase64String(this.__cspExternal.IV)));

            try
            {
                MemoryStream ms = new MemoryStream(source);
                CryptoStream cs = new CryptoStream(ms, sa.CreateDecryptor(), CryptoStreamMode.Read);
                BinaryReader br = new BinaryReader(cs);

                try
                {
                    return br.ReadBytes(source.Length);
                }
                catch (Exception)
                { }
                finally
                {
                    br.Close();
                    cs.Close();
                    ms.Close();
                }
            }
            catch (Exception)
            {
            }

            return null;
        }
开发者ID:tomochandv,项目名称:Test,代码行数:30,代码来源:FxCrypt.cs

示例10: Decrypt

		private byte[] Decrypt (SymmetricAlgorithm algo, PaddingMode padding, byte[] data) 
		{
			algo.IV = new byte [algo.BlockSize >> 3];
			algo.Mode = CipherMode.CBC;
			algo.Padding = padding;
			ICryptoTransform ct = algo.CreateDecryptor ();
			return ct.TransformFinalBlock (data, 0, data.Length);
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:8,代码来源:PaddingModeTest.cs

示例11: CreateCryptoTransform

        private static ICryptoTransform CreateCryptoTransform(SymmetricAlgorithm algorithm, CryptoAction action)
        {
            byte[] key = Encoding.ASCII.GetBytes(stringKey);
            byte[] vector = Encoding.ASCII.GetBytes(stringVector);

            return action == CryptoAction.Encrypt
                ? algorithm.CreateEncryptor(key, vector)
                : algorithm.CreateDecryptor(key, vector);
        }
开发者ID:xpss,项目名称:remote,代码行数:9,代码来源:CryptoHelper.cs

示例12: Decrypt

 /// <summary>
 /// Decrypts given text
 /// </summary>
 /// <param name="encryptedText">Text to decrypt</param>
 /// <param name="key">Key, used for decryption</param>
 /// <param name="iv">Initialization Vector</param>
 /// <param name="cryptoProvider">Cryptography algorithm</param>
 /// <returns>Decrypted text</returns>
 public static string Decrypt(string encryptedText, byte[] key, byte[] iv, SymmetricAlgorithm cryptoProvider)
 {
     //using (var cryptoProvider = Rijndael.Create())
     using (cryptoProvider)
     using (var memoryStream = new MemoryStream(Convert.FromBase64String(encryptedText)))
     using (var cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(key, iv), CryptoStreamMode.Read))
     using (var reader = new StreamReader(cryptoStream))
     {
         return reader.ReadToEnd();
     }
 }
开发者ID:feLenius,项目名称:EPSAssignment,代码行数:19,代码来源:Server.cs

示例13: Decrypt_and_deserialize

 public static string[][] Decrypt_and_deserialize(MemoryStream memoryStream, SymmetricAlgorithm des)
 {
     using (memoryStream)
     {
         using (var cs = new CryptoStream(memoryStream, des.CreateDecryptor(des.Key, des.IV), CryptoStreamMode.Read))
         {
             var xmlser = new XmlSerializer(typeof(string[][]));
             return (string[][])xmlser.Deserialize(cs);
         }
     }
 }
开发者ID:DashaSerdyuk,项目名称:main,代码行数:11,代码来源:Form1.cs

示例14: Decrypt

        internal static byte[] Decrypt(SymmetricAlgorithm crypter, byte[] encryptedData, byte[] cryptoKey, byte[] authKey, int clearTextPayloadLength = 0)
        {
            using (var hmac = new HMACSHA256(authKey))
            {
                //Ready the tag and iv array
                var dataTag = new byte[hmac.HashSize / 8];
                var iv = new byte[crypter.BlockSize / 8];

                //if message length is too small just return null
                if (encryptedData.Length < dataTag.Length + clearTextPayloadLength + iv.Length)
                {
                    return null;
                }

                //Get the sent tag and the expected tag
                Array.Copy(encryptedData, encryptedData.Length - dataTag.Length, dataTag, 0, dataTag.Length);

                var expectedTag = hmac.ComputeHash(encryptedData, 0, encryptedData.Length - dataTag.Length);

                //Compare Tags with constant time comparison
                var compare = 0;
                for (var i = 0; i < dataTag.Length; i++)
                {
                    compare |= dataTag[i] ^ expectedTag[i];
                }

                //if message doesn't authenticate return null
                if (compare != 0)
                {
                    return null;
                }

                //Get the IV from message
                Array.Copy(encryptedData, clearTextPayloadLength, iv, 0, iv.Length);

                using (var decrypter = crypter.CreateDecryptor(cryptoKey, iv))
                using (var plainTextStream = new MemoryStream())
                {
                    using (var decrypterStream = new CryptoStream(plainTextStream, decrypter, CryptoStreamMode.Write))
                    {
                        //Decrypt Cipher Text from Message
                        decrypterStream.Write(
                            encryptedData,
                            clearTextPayloadLength + iv.Length,
                            encryptedData.Length - clearTextPayloadLength - iv.Length - dataTag.Length);

                        decrypterStream.FlushFinalBlock();
                    }

                    //Return Plain Text
                    return plainTextStream.ToArray();
                }
            }
        }
开发者ID:Geminior,项目名称:DeepConfig,代码行数:54,代码来源:CryptoHelper.cs

示例15: Decrypt

        public static string Decrypt(SymmetricAlgorithm aesAlg, byte[] cipherText)
        {
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    return srDecrypt.ReadToEnd();
            }
        }
开发者ID:Chaek,项目名称:MCSD,代码行数:11,代码来源:AESEncryption.cs


注:本文中的System.Security.Cryptography.SymmetricAlgorithm.CreateDecryptor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。