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


C# DESCryptoServiceProvider.GenerateKey方法代码示例

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


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

示例1: Main

	static int Main ()
	{
		string filename = Path.Combine (AppDomain.CurrentDomain.BaseDirectory,
			"encrypt.tmp");
		string data = "this is sensitive data";

		DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
		des.GenerateIV ();
		des.GenerateKey ();

		// -----------  WRITING ENCRYPTED SERIALIZED DATA ------------------
		Stream stream = new FileStream (filename, FileMode.Create, FileAccess.Write);
		stream = new CryptoStream (stream, des.CreateEncryptor (), CryptoStreamMode.Write);
		BinaryFormatter bformatter = new BinaryFormatter ();
		bformatter.Serialize (stream, data);
		stream.Close ();

		stream = null;
		bformatter = null;
		data = string.Empty;

		// -----------  READING ENCRYPTED SERIALIZED DATA ------------------
		stream = new FileStream (filename, FileMode.Open, FileAccess.Read);
		stream = new CryptoStream (stream, des.CreateDecryptor (), CryptoStreamMode.Read);
		bformatter = new BinaryFormatter ();
		data = (string) bformatter.Deserialize (stream);
		stream.Close ();

		//----------- CHECK RESULTS ----------------
		if (data != "this is sensitive data")
			return 1;

		return 0;
	}
开发者ID:mono,项目名称:gert,代码行数:34,代码来源:test.cs

示例2: Test

    static Boolean Test()
    {

        Byte[]  PlainText = {0, 1, 2, 3, 4, 5, 6, 7}; //, 8, 9, 10, 11, 12, 13, 14, 15};
        Byte[]  Key = {1, 1, 1, 1, 1, 1, 1, 1};
        Byte[]  IV = {1, 1, 1, 1, 1, 1, 1, 1};
        
        Console.WriteLine("Encrypting the following bytes:");
        PrintByteArray(PlainText);
        
        DESCryptoServiceProvider     des = new DESCryptoServiceProvider();
        des.Mode = CipherMode.ECB;
//        des.FeedbackSize = 0;
		des.Padding = PaddingMode.PKCS7;


		
		int depth = 10;


        Stream[] encStream_arr = new Stream[depth];
        Stream[] decStream_arr = new Stream[depth];
        ICryptoTransform[] decTrans_array = new ICryptoTransform[depth];

        encStream_arr[0] = new MemoryStream();
        decStream_arr[0] = new MemoryStream();

        for (int i=1; i<depth; i++) {
            des.GenerateKey();
            des.GenerateIV();
            encStream_arr[i] = new CryptoStream(encStream_arr[i-1],des.CreateEncryptor(),CryptoStreamMode.Write);
            decTrans_array[i] = des.CreateDecryptor();
        }

        for (int j=1; j<depth; j++) {
            decStream_arr[j] = new CryptoStream(decStream_arr[j-1],decTrans_array[depth-j],CryptoStreamMode.Write);
        }


        Console.WriteLine("DES default key size = " + des.KeySize);
        Console.WriteLine("SSE mode = " + des.Mode);

        encStream_arr[depth-1].Write(PlainText,0,PlainText.Length);
        ((CryptoStream)encStream_arr[depth-1]).FlushFinalBlock();
        byte[] CipherText = ((MemoryStream)encStream_arr[0]).ToArray();
        encStream_arr[depth-1].Close();

        Console.WriteLine("Cyphertext:");
        PrintByteArray(CipherText);
        

        Console.WriteLine("Decrypting...");

        Console.WriteLine("SSD mode = " + des.Mode);

        decStream_arr[depth-1].Write(CipherText,0,CipherText.Length);
        ((CryptoStream)decStream_arr[depth-1]).FlushFinalBlock();
        byte[] NewPlainText = ((MemoryStream)decStream_arr[0]).ToArray();
        decStream_arr[depth-1].Close();

        PrintByteArray(NewPlainText);
        
        if (!Compare(PlainText, NewPlainText)) {
        	Console.WriteLine("ERROR: roundtrip failed");
        	return false;
        }
        
        return true;
    }
开发者ID:koson,项目名称:.NETMF_for_LPC17xx,代码行数:69,代码来源:SimpleEncDecMul_DES.cs


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