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


C# AesCryptoServiceProvider.CreateDecryptor方法代码示例

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


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

示例1: DecryptData

        /// <summary>
        /// Decrypts a ProcessedPacket.
        /// </summary>
        /// <param name="InitializationVector">Initialization vec to be used by AES.</param>
        /// <param name="PrivateKey">Private key to be used.</param>
        /// <param name="PubKeyBlob">Public key blob to be used.</param>
        /// <param name="StreamToDecrypt">The stream to decrypt.</param>
        /// <returns>A decrypted stream.</returns>
        public static byte[] DecryptData(byte[] InitializationVector, CngKey PrivateKey, byte[] PubKeyBlob,
            byte[] DataToDecrypt)
        {
            using (var Algorithm = new ECDiffieHellmanCng(PrivateKey))
            {
                using (CngKey PubKey = CngKey.Import(PubKeyBlob,
                      CngKeyBlobFormat.EccPublicBlob))
                {
                    byte[] SymmetricKey = Algorithm.DeriveKeyMaterial(PubKey);
                    Console.WriteLine("DecryptedStream: Created symmetric key with " +
                        "public key information: {0}", Convert.ToBase64String(SymmetricKey));

                    AesCryptoServiceProvider AES = new AesCryptoServiceProvider();
                    AES.Key = SymmetricKey;
                    AES.IV = InitializationVector;
                    int NBytes = AES.BlockSize >> 3; //No idea...

                    using (ICryptoTransform Decryptor = AES.CreateDecryptor())
                    {
                        using (MemoryStream DecryptedStream = new MemoryStream())
                        {
                            var cs = new CryptoStream(DecryptedStream, Decryptor, CryptoStreamMode.Write);
                            cs.Write(DataToDecrypt, NBytes, DataToDecrypt.Length - NBytes);
                            cs.FlushFinalBlock();

                            return DecryptedStream.ToArray();
                        }
                    }
                }
            }
        }
开发者ID:ddfczm,项目名称:Project-Dollhouse,代码行数:39,代码来源:Cryptography.cs

示例2: DecryptString

        public static string DecryptString(byte[] encryptedString, byte[] encryptionKey)
        {
            using (var provider = new AesCryptoServiceProvider())
            {
                provider.Key = encryptionKey;
                using (var ms = new MemoryStream(encryptedString))
                {
                    // Read the first 16 bytes which is the IV.
                    byte[] iv = new byte[16];
                    ms.Read(iv, 0, 16);
                    provider.IV = iv;

                    using (var decryptor = provider.CreateDecryptor())
                    {
                        using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                        {
                            using (var sr = new StreamReader(cs))
                            {
                                return sr.ReadToEnd();
                            }
                        }
                    }
                }
            }
        }
开发者ID:KLIM8D,项目名称:ucn-4semproject-dm79-group2,代码行数:25,代码来源:Encryption.cs

示例3: Unprotect

 public byte[] Unprotect(byte[] protectedData)
 {
     byte[] output = null;
     using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
     {
         aesAlg.Key = _key;
         aesAlg.IV = _IV;
         ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
         using (MemoryStream msDecrypt = new MemoryStream(protectedData))
         {
             using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
             {
                 byte[] buffer = new byte[8];
                 using (MemoryStream msOutput = new MemoryStream())
                 {
                     int read;
                     while ((read = csDecrypt.Read(buffer, 0, buffer.Length)) > 0)
                     {
                         msOutput.Write(buffer, 0, read);
                     }
                     output = msOutput.ToArray();
                 }
             }
         }
     }
     return output;
 }
开发者ID:asifashraf,项目名称:proSignalR,代码行数:27,代码来源:AesDataProtector.cs

示例4: CreateDecryptor

 ICryptoTransform CreateDecryptor(byte[] key, byte[] iv)
 {
     using (var provider = new AesCryptoServiceProvider {Padding = _paddingMode})
     {
         return provider.CreateDecryptor(key, iv);
     }
 }
开发者ID:kotvisbj,项目名称:MassTransit,代码行数:7,代码来源:AesCryptoStreamProvider.cs

示例5: BobReceiveData

        private static void BobReceiveData(byte[] encryptData)
        {
            byte[] rawdata = null;
            var aes = new AesCryptoServiceProvider();
            int nBytes = aes.BlockSize >> 3; // bit to Byte, need to devide 8
            byte[] iv = new byte[nBytes];

            for (int i = 0; i < iv.Length; i++)
                iv[i] = encryptData[i];
            using (var bobAlgorithm = new ECDiffieHellmanCng(bobKey))
            using (CngKey alicePubKey = CngKey.Import(alicePubKeyBlob, CngKeyBlobFormat.EccPublicBlob))
            {
                byte[] symmKey = bobAlgorithm.DeriveKeyMaterial(alicePubKey);
                Console.WriteLine(Convert.ToBase64String(symmKey));
                aes.Key = symmKey;
                aes.IV = iv;
            }
            using (ICryptoTransform decryptor = aes.CreateDecryptor())
            using (MemoryStream ms = new MemoryStream())
            {
                var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);
                cs.Write(encryptData, nBytes, encryptData.Length - nBytes);
                cs.Close();
                rawdata = ms.ToArray();
                Console.WriteLine(Encoding.UTF8.GetString(rawdata));
            }
            aes.Clear();
        }
开发者ID:niujiale,项目名称:SecurityTransportation,代码行数:28,代码来源:Program.cs

示例6: DecryptMessage

        private Stream DecryptMessage(Stream outputStream, string keyString)
        {
            var key = Convert.FromBase64String(keyString);
            using (var provider = new AesCryptoServiceProvider())
            {
                provider.Key = key;
                provider.Mode = CipherMode.CBC;
                provider.Padding = PaddingMode.PKCS7;

                var ms = new MemoryStream();
                outputStream.CopyTo(ms);

                ms.Seek(0, SeekOrigin.Begin);
                ms.Position = 0;
                byte[] iv = new byte[16];
                ms.Read(iv, 0, 16);

                provider.IV = iv;
                Trace.WriteLine("IV decrypt " + provider.IV);
                var decryptor = provider.CreateDecryptor(provider.Key, provider.IV);

                return new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

            }
        }
开发者ID:kakkerlakgly,项目名称:EncryptMessage,代码行数:25,代码来源:EncryptMessageTests.cs

示例7: Decrypt

        static Dictionary<string, string> Decrypt(string cipherText, string encryptionPassword, string salt)
        {
            using (var algorithm = new AesCryptoServiceProvider
            {
                Key =GetEncryptionKey(encryptionPassword),
                IV = Convert.FromBase64String(salt)
            })
            using (var decryptor = algorithm.CreateDecryptor())
            using (var decryptedTextStream = new MemoryStream())
            using (var stringReader = new StreamReader(decryptedTextStream, Encoding.UTF8))
            using (var jsonReader = new JsonTextReader(stringReader))
            {
                try
                {
                    using (var cryptoStream = new CryptoStream(decryptedTextStream, decryptor, CryptoStreamMode.Write))
                    {
                        var cipherTextBytes = Convert.FromBase64String(cipherText);
                        cryptoStream.Write(cipherTextBytes, 0, cipherTextBytes.Length);
                        cryptoStream.FlushFinalBlock();

                        var dictionary = new Dictionary<string, string>();
                        var serializer = new JsonSerializer();
                        decryptedTextStream.Position = 0;
                        serializer.Populate(jsonReader, dictionary);
                        return dictionary;
                    }
                }
                catch (CryptographicException cryptoException)
                {
                    throw new CommandException(
                        "Cannot decrypt sensitive-variables. Check your password is correct.\nError message: " +
                        cryptoException.Message);
                }
            }
        }
开发者ID:NoesisLabs,项目名称:Calamari,代码行数:35,代码来源:SensitiveVariables.cs

示例8: Decription

        public string Decription(byte[] encryptedData, byte[] alicePubKeyBlob)
        {
            byte[] rawData = null;

            var aes = new AesCryptoServiceProvider();

            int nBytes = aes.BlockSize >> 3;
            byte[] iv = new byte[nBytes];
            for (int i = 0; i < iv.Length; i++)
                iv[i] = encryptedData[i];

            using (var bobAlgorithm = new ECDiffieHellmanCng(privKey))
            using (CngKey alicePubKey = CngKey.Import(alicePubKeyBlob,
                  CngKeyBlobFormat.EccPublicBlob))
            {
                byte[] symmKey = bobAlgorithm.DeriveKeyMaterial(alicePubKey);
                aes.Key = symmKey;
                aes.IV = iv;

                using (ICryptoTransform decryptor = aes.CreateDecryptor())
                using (MemoryStream ms = new MemoryStream())
                {
                    var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);
                    cs.Write(encryptedData, nBytes, encryptedData.Length - nBytes);
                    cs.Close();

                    rawData = ms.ToArray();
                }
                aes.Clear();
            }
            return Encoding.UTF8.GetString(rawData);
        }
开发者ID:mohankumarcm,项目名称:sse554-project3,代码行数:32,代码来源:EncriptionDecription.cs

示例9: DecryptAesStream

        /// <summary>
        /// Decrypts data from one stream to another stream.
        /// </summary>
        /// <param name="input">Stream containing encrypted data</param>
        /// <param name="output">Stream that will receive the decrypted data</param>
        /// <param name="encryptionKey">Key to decrypt the data with</param>
        /// <param name="IV">IV to decrypt the data with</param>
        internal static void DecryptAesStream(Stream input, Stream output, byte[] encryptionKey, byte[] IV)
        {
            using (var aes = new AesCryptoServiceProvider {Padding = PaddingMode.ISO10126})
            using (var decryptor = aes.CreateDecryptor(encryptionKey, IV))
            {
                var lengthBytes = new byte[AesHeaderSize];
                input.Read(lengthBytes, 0, AesHeaderSize);
                var length = BitConverter.ToInt32(lengthBytes, 0);

                using (var cs = new CryptoStream(input, decryptor, CryptoStreamMode.Read))
                {
                    long position = BufferSize;
                    var buffer = new byte[BufferSize];

                    while (position<length)
                    {
                        cs.Read(buffer, 0, BufferSize);
                        output.Write(buffer, 0, BufferSize);

                        position += BufferSize;
                    }

                    int offset = length % BufferSize;
                    if (offset>0)
                    {
                        cs.Read(buffer, 0, offset);
                        output.Write(buffer, 0, offset);
                    }
                    output.Flush();
                }
            }
        }
开发者ID:zr40,项目名称:kyru-dotnet,代码行数:39,代码来源:Crypto.cs

示例10: Decrypt

        /// <summary>
        /// Decrypts the specified crypt text.
        /// </summary>
        /// <param name="cryptoText">The crypto text.</param>
        /// <returns>The plain text.</returns>
        public string Decrypt(byte[] cryptoText)
        {
            string clearText;

            using (var aes = new AesCryptoServiceProvider())
            {
                aes.Key = this.Key;
                aes.IV = this.InitalizationVector;

                var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                using (var memoryStream = new MemoryStream(cryptoText))
                {
                    using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        using (var streamReader = new StreamReader(cryptoStream))
                        {
                            clearText = streamReader.ReadToEnd();
                        }
                    }
                }
            }

            return clearText;
        }
开发者ID:famstutz,项目名称:YAEM,代码行数:30,代码来源:AesCryptoProvider.cs

示例11: Decrypt

        public static string Decrypt(string input)
        {
            //byte[] inputBytes = Convert.FromBase64String(input); //Encoding.UTF8.GetBytes(input);
            string[] sInput = input.Split("-".ToCharArray());
            byte[] inputBytes = new byte[sInput.Length];
            for (int i = 0; i < sInput.Length; i++)
            {
                inputBytes[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
            }
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32));
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Key = keyBytes;
                aesAlg.IV = AesIv;

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream(inputBytes))
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srEncrypt = new StreamReader(csEncrypt))
                        {
                            return srEncrypt.ReadToEnd();
                        }
                    }
                }
            }
        }
开发者ID:JIANGSHUILANG,项目名称:TestSelfblog,代码行数:28,代码来源:Enctyption.cs

示例12: SetUp

 public void SetUp()
 {
     using ( AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider() ) {
         m_Encryptor = aesAlg.CreateEncryptor();
         m_Decryptor = aesAlg.CreateDecryptor();
     }
 }
开发者ID:overmind900,项目名称:Utilities,代码行数:7,代码来源:TestCryptography.cs

示例13: Decrypt

        public void Decrypt(string filename)
        {
            FileStream fsInput = new FileStream(filename, FileMode.Open, FileAccess.Read);
            FileStream fsOutput = new FileStream(filename + ".crypt", FileMode.Create, FileAccess.Write);
            AesCryptoServiceProvider Aes = new AesCryptoServiceProvider();

            Aes.KeySize = 128;

            byte[] input = new byte[256];

            int count = fsInput.Read(input, 0, 256);
            Aes.Key = _algorithm_asym.Decrypt(input, false);
            count = fsInput.Read(input, 0, 256);
            Aes.IV = _algorithm_asym.Decrypt(input, false);

            ICryptoTransform desencrypt = Aes.CreateDecryptor();
            CryptoStream cryptostream = new CryptoStream(fsOutput, desencrypt, CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[fsInput.Length - 1];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);

            fsInput.Close();
            fsOutput.Close();
        }
开发者ID:Jupotter,项目名称:virologie,代码行数:25,代码来源:FileEncryption.cs

示例14: Button2Click

        void Button2Click(object sender, EventArgs e)
        {
            if (openfile.ShowDialog() == DialogResult.OK)
            {
                    FileStream fsFileIn = File.OpenRead(textBox2.Text);

                    FileStream fsKeyFile = File.OpenRead(openfile.FileName);

                    FileStream fsFileOut = File.Create(textBox2.Text+"_decrypt");

                    AesCryptoServiceProvider cryptAlgorithm = new AesCryptoServiceProvider();
                    BinaryReader brFile = new BinaryReader(fsKeyFile);
                    //textBox1.Text = key.ToString();
                    StreamWriter writer = File.AppendText(@"key.txt");
                    writer.WriteLine(key.Length);
                    cryptAlgorithm.Key = key;
                    cryptAlgorithm.IV = iv;

                    CryptoStream csEncrypt = new CryptoStream(fsFileIn, cryptAlgorithm.CreateDecryptor(), CryptoStreamMode.Read);

                    StreamReader srStream = new StreamReader(csEncrypt);
                    StreamWriter swStream = new StreamWriter(fsFileOut);

                    swStream.WriteLine(srStream.ReadToEnd());

                    swStream.Close();
                    fsFileOut.Close();
                    srStream.Close();

            }
        }
开发者ID:phantasy0ne,项目名称:CourseWork,代码行数:31,代码来源:MainForm.cs

示例15: DecryptAES

        public static string DecryptAES(byte[] data, byte[] key, byte[] IV)
        {
            string retVal = string.Empty;

            var tsp = new System.Security.Cryptography.AesCryptoServiceProvider();

            // default padding and mode
            tsp.Mode = CipherMode.CBC;
            tsp.Padding = PaddingMode.PKCS7;

            // get a crypt transform interface
            ICryptoTransform ct = tsp.CreateDecryptor(key, IV);

            // setup a memory stream
            MemoryStream ms = new MemoryStream();
            using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
            {
                // write the string through the crypto stream
                cs.Write(data, 0, data.Length);

                // flush
                cs.FlushFinalBlock();

                // convert the data in the memory stream to base64 text
                ms.Seek(0, SeekOrigin.Begin);

                var enc = ms.ToArray();//.TakeWhile(c => c > 16).ToArray();
                retVal = System.Text.Encoding.ASCII.GetString(enc, 0, enc.Length);
            }

            return retVal;
        }
开发者ID:akois,项目名称:OracleService,代码行数:32,代码来源:CryptoHelper.cs


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