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


C# PasswordDeriveBytes.CryptDeriveKey方法代码示例

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


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

示例1: Main

    public static void Main() {
        string PlainText = "Titan";
        byte[] PlainBytes = new byte[5];
        PlainBytes = Encoding.ASCII.GetBytes(PlainText.ToCharArray());
        PrintByteArray(PlainBytes);
        byte[] CipherBytes = new byte[8];
        PasswordDeriveBytes pdb = new PasswordDeriveBytes("Titan", null);
        byte[] IV = new byte[8];
        byte[] Key = pdb.CryptDeriveKey("RC2", "SHA1", 40, IV);
        PrintByteArray(Key);
        PrintByteArray(IV);

        // Now use the data to encrypt something
        RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
        Console.WriteLine(rc2.Padding);
        Console.WriteLine(rc2.Mode);
        ICryptoTransform sse = rc2.CreateEncryptor(Key, IV);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs1 = new CryptoStream(ms, sse, CryptoStreamMode.Write);
        cs1.Write(PlainBytes, 0, PlainBytes.Length);
        cs1.FlushFinalBlock();
        CipherBytes = ms.ToArray();
        cs1.Close();
        Console.WriteLine(Encoding.ASCII.GetString(CipherBytes));
        PrintByteArray(CipherBytes);

        ICryptoTransform ssd = rc2.CreateDecryptor(Key, IV);
        CryptoStream cs2 = new CryptoStream(new MemoryStream(CipherBytes), ssd, CryptoStreamMode.Read);
        byte[] InitialText = new byte[5];
        cs2.Read(InitialText, 0, 5);
        Console.WriteLine(Encoding.ASCII.GetString(InitialText));
    	PrintByteArray(InitialText);
    }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:33,代码来源:DeriveBytesTest.cs

示例2: TestRepeated

	public static Boolean TestRepeated()
	{
		Boolean bRes = true;
		int l, key_size;
		Char[] ach;
		String s;
		Byte[] salt, the_key, temp_key, iv = new Byte[8];
		PasswordDeriveBytes pdb;

		for(int i=0; i<NO_PASSES; i++) {
			l = Rnd.Next(MAX_PASS_LEN)+1;
			ach = new Char[l];
			for(int k=0; k<l; k++) ach[k] = (Char)(Rnd.Next(26)+65);
			s = new String(ach);
			salt = new Byte[Rnd.Next(MAX_SALT_LEN)];
			Rnd.NextBytes(salt);
			key_size = Rnd.Next(128);
			Rnd.NextBytes(iv);

			pdb = new PasswordDeriveBytes(s, salt);
			the_key = pdb.CryptDeriveKey("RC2", "SHA1", /*key_size*/ 128, iv);

			Console.WriteLine("--------------------------------------");
			PrintByteArray(the_key);

			for (int j=0; j<MAX_COMP;j++) {
				temp_key = pdb.CryptDeriveKey("RC2", "SHA1", /*key_size*/ 128, iv);
				Console.WriteLine("--------------------------------------");
				PrintByteArray(temp_key);
				if (!Compare(the_key, temp_key)) {
					bRes = false;
					Console.WriteLine("Two passes of CryptDeriveKey yielded different results");
					break;
				}
			}
			if (bRes == false) break;
		}

		return bRes;
	}
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:40,代码来源:CryptDeriveKey.cs

示例3: GetScopeKey

	// Get the encryption key to use to protect memory for a scope.
	private static byte[] GetScopeKey(MemoryProtectionScope scope, byte[] salt)
			{
				String key;
				PasswordDeriveBytes derive;
				if(scope == MemoryProtectionScope.SameLogon)
				{
					key = Environment.UserName;
				}
				else
				{
					key = Environment.UserName + "/" + Environment.MachineName;
				}
				if(salt == null)
				{
					salt = new byte [16];
				}
				derive = new PasswordDeriveBytes(key, salt);
				return derive.CryptDeriveKey("Rijndael", "SHA1", 16, null);
			}
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:20,代码来源:ProtectedData.cs

示例4: Encrypt

 private static string Encrypt(string strText)
 {
     string key = "&%#@?,:*";
     TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
     des.IV = new byte[8];
     PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, new byte[-1 + 1]);
     des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
     MemoryStream ms = new MemoryStream((strText.Length * 2) - 1);
     CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
     byte[] plainBytes = Encoding.UTF8.GetBytes(strText);
     encStream.Write(plainBytes, 0, plainBytes.Length);
     encStream.FlushFinalBlock();
     byte[] encryptedBytes = new byte[(int)ms.Length - 1 + 1];
     ms.Position = 0;
     ms.Read(encryptedBytes, 0, (int)ms.Length);
     encStream.Close();
     return Convert.ToBase64String(encryptedBytes);
 }
开发者ID:rupendra-sharma07,项目名称:MainTrunk,代码行数:18,代码来源:UserRegistration.aspx.cs

示例5: TestKnownValue_CryptDeriveKey

        private static byte[] TestKnownValue_CryptDeriveKey(HashAlgorithmName hashName, string password, string alg, int keySize, byte[] salt, byte[] expected)
        {
            byte[] output;
            byte[] iv = new byte[8];

            using (var deriveBytes = new PasswordDeriveBytes(password, salt))
            {
                output = deriveBytes.CryptDeriveKey(alg, hashName.Name, keySize, iv);
            }

            Assert.Equal(expected, output);

            // For these tests, the returned IV is always zero
            Assert.Equal(new byte[8], iv);

            return output;
        }
开发者ID:chcosta,项目名称:corefx,代码行数:17,代码来源:PasswordDeriveBytesTests.cs

示例6: CryptDeriveKey_Invalid_IV

 public static void CryptDeriveKey_Invalid_IV()
 {
     using (var deriveBytes = new PasswordDeriveBytes(TestPassword, s_testSalt))
     {
         Assert.Throws<CryptographicException>(() => deriveBytes.CryptDeriveKey("RC2", "SHA1", 128, null));
         Assert.Throws<CryptographicException>(() => deriveBytes.CryptDeriveKey("RC2", "SHA1", 128, new byte[1]));
     }
 }
开发者ID:chcosta,项目名称:corefx,代码行数:8,代码来源:PasswordDeriveBytesTests.cs

示例7: CryptDeriveKey_Invalid_HashAlgorithm

 public static void CryptDeriveKey_Invalid_HashAlgorithm()
 {
     using (var deriveBytes = new PasswordDeriveBytes(TestPassword, s_testSalt))
     {
         Assert.Throws<CryptographicException>(() => deriveBytes.CryptDeriveKey("RC2", "BADALG", 128, s_testSalt));
     }
 }
开发者ID:chcosta,项目名称:corefx,代码行数:7,代码来源:PasswordDeriveBytesTests.cs

示例8: CryptDeriveKey_Invalid_KeyLength

 public static void CryptDeriveKey_Invalid_KeyLength()
 {
     using (var deriveBytes = new PasswordDeriveBytes(TestPassword, s_testSalt))
     {
         Assert.ThrowsAny<CryptographicException>(() => deriveBytes.CryptDeriveKey("RC2", "SHA1", 127, s_testSalt));
         Assert.ThrowsAny<CryptographicException>(() => deriveBytes.CryptDeriveKey("RC2", "SHA1", 129, s_testSalt));
     }
 }
开发者ID:chcosta,项目名称:corefx,代码行数:8,代码来源:PasswordDeriveBytesTests.cs

示例9: TestKnown

	public static Boolean TestKnown()
	{
		Boolean bRes = true;
		Byte[] IV = new Byte[8];
		Byte[] PlainText = {0,1,2,3,4,5,6,7};
		Byte[] KnownVector = {0x7A, 0x50, 0x39, 0x82, 0xB5, 0x0E, 0xB0, 0x0D, 0x1F, 0x37, 0x9D, 0xC8, 0x36, 0x09, 0xD3, 0xFF};

		PasswordDeriveBytes pdb = new PasswordDeriveBytes("simplepassword", null);
		Byte[] the_key = pdb.CryptDeriveKey("RC2", "MD5", 40, IV);
		
		RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
        ICryptoTransform sse = rc2.CreateEncryptor(the_key, IV);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
        cs.Write(PlainText,0,PlainText.Length);
        cs.FlushFinalBlock();
        byte[] ciphertext = ms.ToArray();
        cs.Close();

		Console.WriteLine("--- Cipher Text : ----");
		PrintByteArray(ciphertext);
		Console.WriteLine("--- Known vector : ----");
		PrintByteArray(KnownVector);
		
		if(!Compare(ciphertext, KnownVector)) {
			Console.WriteLine("Known and calculated values differ!");
			bRes = false;
		}

		return bRes;
	}
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:31,代码来源:CryptDeriveKey.cs

示例10: DeriveKeyNullIv

	/// <summary>
	///		Derive key, null IV
	/// </summary>
	private static void DeriveKeyNullIv()
	{
		string password = "pwd";
		byte[] salt = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };

		PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt);
		pdb.CryptDeriveKey("RC2", "MD5", 21, null);
		return;
	}
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:12,代码来源:PwdDrvBytes.cs


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