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


C# Rfc2898DeriveBytes.Reset方法代码示例

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


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

示例1: ToKeyDevination

        /// <summary>
        /// Deviate byte data based on supported types.
        /// </summary>
        /// <param name="data">Data of any encoding type.</param>
        /// <param name="salt">Divination salt.</param>
        /// <param name="length">Divination returned byte size.</param>
        /// <returns>SaltedData</returns>
        public static SaltedData ToKeyDevination(this byte[] data, byte[] salt = null, int length = 32)
        {
            var salting = salt ?? 16.ToRandomBytes();
            var pbkdf2 = new Rfc2898DeriveBytes(data, salting, PBKDF2_ITERATIONS);
            var derived = pbkdf2.GetBytes(length);
            pbkdf2.Reset();

            return new SaltedData() { Salt = salting, Data = derived };
        }
开发者ID:TimothyMeadows,项目名称:Blackfeather,代码行数:16,代码来源:KeyDevination.cs

示例2: SplitCalls_Reset

		public void SplitCalls_Reset ()
		{
			Rfc2898DeriveBytes keygen = new Rfc2898DeriveBytes ("password", salt, 12345);
			byte [] big = keygen.GetBytes (48);

			keygen.Reset ();
			byte [] a = keygen.GetBytes (16);
			byte [] b = keygen.GetBytes (32);

			CompareBuffers (big, a, b);
		}
开发者ID:calumjiao,项目名称:Mono-Class-Libraries,代码行数:11,代码来源:Rfc2898DeriveBytesTest.cs

示例3: RFC3211_TC2_Reset

		public void RFC3211_TC2_Reset () 
		{
			byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE };
			Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
			byte[] key1 = pkcs5.GetBytes (8);
			Assert.AreEqual (expected, key1, "RFC3211_TC2_part1");
			pkcs5.Reset ();
			byte[] key2 = pkcs5.GetBytes (8);
			Assert.AreEqual (expected, key2, "RFC3211_TC2_part2");
		}
开发者ID:calumjiao,项目名称:Mono-Class-Libraries,代码行数:10,代码来源:Rfc2898DeriveBytesTest.cs

示例4: MsdnExample

		public void MsdnExample()
		{
			const string pwd1 = "[email protected]!123";
			const int myIterations = 1000;

			// data1 can be a string or contents of a file.
			const string data1 = "Some test data";

			// Create a byte array to hold the random value. 
			byte[] randomSalt = new byte[8];
			using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider())
			{
				// Fill the array with a random value.
				rngCsp.GetBytes(randomSalt);
			}

			////	try
			////	{
			// The default iteration count is 1000 so the two methods use the same iteration count.
			Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(pwd1, randomSalt, myIterations);
			Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(pwd1, randomSalt);

			// Encrypt the data.
			SymmetricAlgorithm encAlg = TripleDES.Create();
			encAlg.Key = k1.GetBytes(16);

			MemoryStream encryptionStream = new MemoryStream();
			CryptoStream encrypt = new CryptoStream(
				encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write);
			byte[] utfD1 = new UTF8Encoding(false).GetBytes(data1);

			encrypt.Write(utfD1, 0, utfD1.Length);
			encrypt.FlushFinalBlock();
			encrypt.Close();
			byte[] edata1 = encryptionStream.ToArray();

			// Erase memory for optimum secrecy?
			k1.Reset();

			// Try to decrypt, thus showing it can be round-tripped.
			SymmetricAlgorithm decAlg = TripleDES.Create();
			decAlg.Key = k2.GetBytes(16);

			// Relay the one-time INITIALIZATION VECTOR
			// generated by the original encryption.
			// Could this have been initialized via the Rfc k1/k2?
			decAlg.IV = encAlg.IV;

			MemoryStream decryptionStreamBacking = new MemoryStream();
			CryptoStream decrypt = new CryptoStream(
				decryptionStreamBacking, decAlg.CreateDecryptor(), CryptoStreamMode.Write);
			decrypt.Write(edata1, 0, edata1.Length);
			decrypt.Flush();
			decrypt.Close();

			// Erase memory for optimum secrecy?
			k2.Reset();

			string data2 = new UTF8Encoding(false).GetString(decryptionStreamBacking.ToArray());

			if (!data1.Equals(data2))
			{
				Console.WriteLine("Error: The two values are not equal.");
			}
			else
			{
				Console.WriteLine("The two values are equal.");
				Console.WriteLine("k1 iterations: {0}", k1.IterationCount);
				Console.WriteLine("k2 iterations: {0}", k2.IterationCount);
			}
			////	}
			////	catch (Exception e)
			////	{
			////		Console.WriteLine("Error: {0}", e);
			////	}

			Assert.AreEqual(data2, data1);
			Assert.IsTrue(data1.Equals(data2));
		}
开发者ID:jimkropa,项目名称:misc.corlib,代码行数:79,代码来源:Rfc2898DeriveBytesTests.cs

示例5: Decrypt

        /// <summary>
        /// Decripts a byte array with a given password and salt.
        /// </summary>
        /// <param name="encryptedData">A byte array containing the data you would like decrypted.</param>
        /// <param name="password">The password used to derive the key.</param>
        /// <param name="salt">The key salt used to derive the key.</param>
        /// <returns>A string containing the decrypted data.</returns>
        public static string Decrypt(byte[] encryptedData, string password, string salt)
        {
            AesManaged aes = null;
            MemoryStream memoryStream = null;
            CryptoStream cryptoStream = null;
            string decryptedText = "";
            try
            {
                //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator 
                var rfc2898 = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));

                //Create AES algorithm with 256 bit key and 128-bit block size 
                aes = new AesManaged();
                aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
                rfc2898.Reset(); //neede to be WinRT compatible
                aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);

                //Create Memory and Crypto Streams 
                memoryStream = new MemoryStream();
                cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);

                //Decrypt Data 
                cryptoStream.Write(encryptedData, 0, encryptedData.Length);
                cryptoStream.FlushFinalBlock();

                //Return Decrypted String 
                var decryptBytes = memoryStream.ToArray();
                decryptedText = Encoding.Unicode.GetString(decryptBytes, 0, decryptBytes.Length);
            }
            catch (Exception eDecrypt)
            {

            }
            finally
            {
                if (cryptoStream != null)
                    cryptoStream.Close();

                if (memoryStream != null)
                    memoryStream.Close();

                if (aes != null)
                    aes.Clear();
            }
            return decryptedText;
        }
开发者ID:x-skywalker,项目名称:XamlEssentials,代码行数:53,代码来源:EncryptionHelper.cs

示例6: Encrypt

        /// <summary>
        /// Encrypts a string with a given password and salt.
        /// </summary>
        /// <param name="plainText">The information to encrypt.</param>
        /// <param name="password">The password used to derive the key.</param>
        /// <param name="salt">The key salt used to derive the key.</param>
        /// <exception cref="System.ArgumentException">
        /// The specified salt size is smaller than 8 bytes or the iteration count is less than 1.
        /// </exception>
        /// <returns>A byte array with the encrypted version of the plainText.</returns>
        public static byte[] Encrypt(string plainText, string password, string salt)
        {
            AesManaged aes = null;
            MemoryStream memoryStream = null;
            CryptoStream cryptoStream = null;

            try
            {
                //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator 
                var rfc2898 = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));

                //Create AES algorithm with 256 bit key and 128-bit block size 
                aes = new AesManaged();
                aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
                rfc2898.Reset(); //needed for WinRT compatibility
                aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);

                //Create Memory and Crypto Streams 
                memoryStream = new MemoryStream();
                cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);

                //Encrypt Data 
                var data = Encoding.Unicode.GetBytes(plainText);
                cryptoStream.Write(data, 0, data.Length);
                cryptoStream.FlushFinalBlock();

                //Return encrypted data 
                return memoryStream.ToArray();

            }
            catch (Exception eEncrypt)
            {
                return null;
            }
            finally
            {
                if (cryptoStream != null)
                    cryptoStream.Close();

                if (memoryStream != null)
                    memoryStream.Close();

                if (aes != null)
                    aes.Clear();

            }
        }
开发者ID:x-skywalker,项目名称:XamlEssentials,代码行数:57,代码来源:EncryptionHelper.cs


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