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


C# CryptoStream.CopyTo方法代码示例

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


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

示例1: DecryptToStream

        public void DecryptToStream(IFile file, Stream outputStream)
        {
            if (file == null)
                throw new ArgumentNullException(nameof(file));
            if (outputStream == null)
                throw new ArgumentNullException(nameof(outputStream));

            //Read IV
            int ivSize = _encryptionProvider.BlockSize / 8;
            byte[] iv = new byte[ivSize];
            using (Stream fileData = file.Read())
            {
                fileData.Seek(0, SeekOrigin.Begin);
                fileData.Read(iv, 0, iv.Length);

                //Write decrypted data
                byte[] key = _key.GenerateBlock(_encryptionProvider.BlockSize);
                ICryptoTransform decryptor = _encryptionProvider.CreateDecryptor(key, iv);

                using (var cryptoStream = new CryptoStream(fileData, decryptor, CryptoStreamMode.Read))
                {
                    cryptoStream.CopyTo(outputStream);
                }
            }
        }
开发者ID:AdrianLThomas,项目名称:altCrypt,代码行数:25,代码来源:StreamEncryptor.cs

示例2: ArgumentNullException

        HttpContent IHttpContentEncryptor.Decrypt(HttpContent encryptedContent)
        {
            if (encryptedContent == null)
            {
                throw new ArgumentNullException("encryptedContent");
            }

            var encodedString = encryptedContent.ReadAsStringAsync().Result;

            using (var encryptedData = new MemoryStream(Convert.FromBase64String(encodedString)))
            {
                if (encryptedData.Length == 0)
                {
                    return encryptedContent;
                }

                this.algorithm.Key = this.keyProvider.Key;
                this.algorithm.IV = this.keyProvider.IV;

                using (var decryptor = algorithm.CreateDecryptor())
                {
                    var cryptoStream = new CryptoStream(encryptedData, decryptor, CryptoStreamMode.Read);
                    var originData = new MemoryStream((int)encryptedData.Length);
                    cryptoStream.CopyTo(originData);
                    originData.Flush();
                    originData.Position = 0;

                    var originContent = new StreamContent(originData);
                    originContent.Headers.ContentType = encryptedContent.Headers.ContentType;

                    return originContent;
                }
            }
        }
开发者ID:jacktsai,项目名称:WebApiSurvey,代码行数:34,代码来源:AesHttpContentEncryptor.cs

示例3: DecryptAes

        public static void DecryptAes(Stream inStream, Stream outStream, string password)
        {
            if (inStream == null)
            {
                throw new ArgumentNullException("inStream");
            }
            if (outStream == null)
            {
                throw new ArgumentNullException("outStream");
            }
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            // generate an encryption key with the shared secret and salt
            using (var key = new Rfc2898DeriveBytes(password, Salt))
            {
                using (var aesAlg = new RijndaelManaged())
                {
                    aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
                    aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);

                    using (var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV))
                    {
                        using (var csEncrypt = new CryptoStream(inStream, decryptor, CryptoStreamMode.Read))
                        {
                            csEncrypt.CopyTo(outStream);
                        }
                    }
                }
            }
        }
开发者ID:xsburg,项目名称:ssh-tunnel-manager-2,代码行数:33,代码来源:CryptoHelper.cs

示例4: EncryptAndUpload

        public static void EncryptAndUpload(string file, string awsPath, string key)
        {
            if (bool.Parse(ConfigurationManager.AppSettings["ManagedEncryption"]))
            {
                Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(key, appKey);
                using (var aes = new AesCryptoServiceProvider())
                {
                    aes.Key = deriveBytes.GetBytes(aes.KeySize / 8);
                    aes.IV = deriveBytes.GetBytes(aes.BlockSize / 8);
                    using (var temp = new FileStream(file + "_encrypted", FileMode.Create))
                    {
                        using (var stream = new CryptoStream(new FileStream(file, FileMode.Open), aes.CreateEncryptor(), CryptoStreamMode.Read))
                        {
                            stream.CopyTo(temp);
                        }
                    }

                    UploadFile(file + "_encrypted", awsPath);

                    File.Delete(file + "_encrypted");
                }
            }
            else
                UploadFile(file, awsPath);
        }
开发者ID:stormbreakerbg,项目名称:SecureShare,代码行数:25,代码来源:AWSHelper.cs

示例5: TripleDesCreate

        public static void TripleDesCreate()
        {
            byte[] inputBytes = Encoding.ASCII.GetBytes("This is a secret message and is a sentence that is longer than a block, it ensures that multi-block functions work.");
            TripleDES tripleDes = TripleDES.Create();

            byte[] encryptedBytes;
            using (MemoryStream input = new MemoryStream(inputBytes))
            using (CryptoStream cryptoStream = new CryptoStream(input, tripleDes.CreateEncryptor(), CryptoStreamMode.Read))
            using (MemoryStream output = new MemoryStream())
            {
                cryptoStream.CopyTo(output);
                encryptedBytes = output.ToArray();
            }

            Assert.NotEqual(inputBytes, encryptedBytes);

            byte[] decryptedBytes;
            using (MemoryStream input = new MemoryStream(encryptedBytes))
            using (CryptoStream cryptoStream = new CryptoStream(input, tripleDes.CreateDecryptor(), CryptoStreamMode.Read))
            using (MemoryStream output = new MemoryStream())
            {
                cryptoStream.CopyTo(output);
                decryptedBytes = output.ToArray();
            }

            Assert.Equal(inputBytes, decryptedBytes);
        }
开发者ID:Rayislandstyle,项目名称:corefx,代码行数:27,代码来源:TripleDesTests.cs

示例6: TransformEntireBlock

		public static byte[] TransformEntireBlock(this ICryptoTransform transform, byte[] data)
		{
			using (var input = new MemoryStream(data))
			using (var output = new CryptoStream(input, transform, CryptoStreamMode.Read))
			using (var result = new MemoryStream())
			{
				output.CopyTo(result);
				return result.ToArray();
			}
		}
开发者ID:cocytus,项目名称:ravendb,代码行数:10,代码来源:CryptoTransformExtensions.cs

示例7: Decrypt

        static void Decrypt(string[] args)
        {
            //Precheck if a tmp file already exists.
            //If so delete it.
            if (File.Exists(args[0] + ".tmp"))
            {
                Display("Destroying old .tmp files.");
                DestroyFile(args[0] + ".tmp");
                Display("Done.");
            }

            //SET UP STREAMS//
            FileStream Output;
            FileStream Input;
            CryptoStream CryptStream;

            //SET UP SERVICE PROVIDERS//
            AesCryptoServiceProvider ACSP = new AesCryptoServiceProvider();

            //AES PARAMETERS//
            ACSP.Padding = PaddingMode.PKCS7;
            Display("Creating keys.");
            ACSP.IV = GenerateIV(args[2]);      //initialization vector
            ACSP.Key = GenerateKey(args[2]);    //32 byte key
            Display("Done.");

            Input = new FileStream(args[0], FileMode.Open);
            Output = new FileStream(args[0] + ".tmp", FileMode.CreateNew);
            CryptStream = new CryptoStream(Input, ACSP.CreateDecryptor(), CryptoStreamMode.Read);

            Display("Starting decryption.");
            CryptStream.CopyTo(Output);
            Display("Done.");
            Input.Flush();
            Input.Dispose();
            CryptStream.Flush();
            //CryptStream.Dispose(); <-- Is disposed with input.
            Output.Flush();
            Output.Dispose();

            Display("Destroying old raw file.");
            DestroyFile(args[0]);
            Display("Done.");

            //Rename .tmp to the original file.
            Display("Renaming new file.");
            File.Move(args[0] + ".tmp", args[0]);
            Display("Done.");

            ACSP.Dispose();

            Display("Decryption process completed.");
            Console.ReadKey();
            return;
        }
开发者ID:Cr1ppin,项目名称:CommandLineEncryption,代码行数:55,代码来源:Program.cs

示例8: Decrypt

 public static Byte[] Decrypt(this AesManaged aes, Byte[] source, Byte[] key, Byte[] iv)
 {
     var bytesKey = ResizeBytesArray(key, aes.Key.Length);
     var bytesIv = ResizeBytesArray(iv, aes.IV.Length);
     using (var msIn = new MemoryStream(source))
     using (var cryptStreem = new CryptoStream(msIn, aes.CreateDecryptor(bytesKey, bytesIv), CryptoStreamMode.Read))
     {
         MemoryStream m = new MemoryStream();
         cryptStreem.CopyTo(m);
         return m.ToArray();
     }
 }
开发者ID:fengweijp,项目名称:higlabo,代码行数:12,代码来源:AesManagedExtensions.cs

示例9: Decrypt

		public string Decrypt(string name) {
			using (var ms = new MemoryStream(Base64Decode(name))) {
				byte ivId = (byte)ms.ReadByte();
				cipher.IV = GetIV(ivId);

				var result = new MemoryStream();
				using (var stream = new CryptoStream(ms, cipher.CreateDecryptor(), CryptoStreamMode.Read))
					stream.CopyTo(result);

				return Encoding.UTF8.GetString(result.ToArray());
			}
		}
开发者ID:EmilZhou,项目名称:ConfuserEx,代码行数:12,代码来源:ReversibleRenamer.cs

示例10: ResolveAddonStream

		public static Stream ResolveAddonStream (string path, FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read) {
			var stream = File.OpenRead(FindScaledAsset(Path.Combine(_userPath, path)));

			if (_key != null && _iv != null) {
				var ms = new MemoryStream();

				using (var cs = new CryptoStream(stream, _rm.CreateDecryptor(_key, _iv), CryptoStreamMode.Read)) {
					cs.CopyTo(ms);
				}
				ms.Position = 0;
				return ms;
			} else
				return stream;
		}
开发者ID:jpernst,项目名称:GameStack,代码行数:14,代码来源:Assets.cs

示例11: Decrypt

        public static void Decrypt(string source, string key, string destination, string algorithmName)
        {
            using (var algorithm = SymmetricAlgorithm.Create(algorithmName))
            {
                using (var inputStream = new FileStream(key, FileMode.Open, FileAccess.Read))
                using (var streamReader = new StreamReader(inputStream))
                {
                    algorithm.Key = Convert.FromBase64String(streamReader.ReadLine());
                    algorithm.IV = Convert.FromBase64String(streamReader.ReadLine());
                }

                using (var inputStream = new FileStream(source, FileMode.Open, FileAccess.Read))
                using (var cryptoStream = new CryptoStream(inputStream, algorithm.CreateDecryptor(), CryptoStreamMode.Read))
                using (var outputStream = new FileStream(destination, FileMode.Create, FileAccess.Write))
                    cryptoStream.CopyTo(outputStream);
            }
        }
开发者ID:yuukos,项目名称:nsudotnet,代码行数:17,代码来源:Coder.cs

示例12: MakeDycrypt

        private void MakeDycrypt()
        {

            var autofc = new Autofc();
            _container = autofc.Container;
            var currentAlgorithm = _container.ResolveKeyed<SymmetricAlgorithm>(_algorithm);
            currentAlgorithm.Padding = PaddingMode.None;
            //var currentAlgorithm = new AesCryptoServiceProvider {Padding = PaddingMode.None};
            ReadKey();
            var decryptor = currentAlgorithm.CreateDecryptor(_key, _iv);
            using (var cryptoStream = new CryptoStream(_reader, decryptor, CryptoStreamMode.Read))
            {
                cryptoStream.CopyTo(_writer);
            }
            _reader.Dispose();
            _writer.Dispose();
        }
开发者ID:GoodBoris,项目名称:DOT.NET,代码行数:17,代码来源:Decrypt.cs

示例13: EncryptData

 public static byte[] EncryptData(byte[] data, string key)
 {
     using (var aesAlg = new AesManaged())
     {
         aesAlg.Key = new UTF8Encoding().GetBytes(key);
         ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, new byte[] {0x00});
         using (var msEncrypt = new MemoryStream())
         using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
         {
             using (var memoryStream = new MemoryStream())
             {
                 csEncrypt.CopyTo(memoryStream);
                 return memoryStream.ToArray();
             }
         }
     }
 }
开发者ID:JamieH,项目名称:SnapSharp,代码行数:17,代码来源:Crypto.cs

示例14: Encrypt

        public static void Encrypt(string source, string destination, string algorithmName)
        {
            using (var algorithm = SymmetricAlgorithm.Create(algorithmName))
            {
                using (var inputStream = new FileStream(source, FileMode.Open, FileAccess.Read))
                using (var cryptoStream = new CryptoStream(inputStream, algorithm.CreateEncryptor(), CryptoStreamMode.Read))
                using (var outputStream = new FileStream(destination, FileMode.Create, FileAccess.Write))
                    cryptoStream.CopyTo(outputStream);

                using (var outputStream = new FileStream(String.Format("{0}.key", source), FileMode.Create, FileAccess.Write))
                using (var streamWriter = new StreamWriter(outputStream))
                {
                    streamWriter.WriteLine(Convert.ToBase64String(algorithm.Key));
                    streamWriter.WriteLine(Convert.ToBase64String(algorithm.IV));
                }
            }
        }
开发者ID:yuukos,项目名称:nsudotnet,代码行数:17,代码来源:Coder.cs

示例15: DecryptData

        public static byte[] DecryptData(byte[] cipherText)
        {
            var decryptor = CurrProvider.CreateDecryptor();

            byte[] plainText;
            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        csDecrypt.CopyTo(memoryStream);
                        plainText = memoryStream.ToArray();
                    }
                }
            }
            return plainText;
        }
开发者ID:kallex,项目名称:Caloom,代码行数:18,代码来源:EncryptionSupport.cs


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