コード例 #1
0
		public void TestGenerateKey_String_String()
		{
			Console.Out.WriteLine("generateKey");
			RSAService rsa = new RSAService(keysize);
			if (!rsa.AreKeysPresent(privateKeyfile, publicKeyfile))
			{
				Console.Out.WriteLine("Begin Generating RSA Key Pair.");
				using (FileStream fos_private = new FileStream(privateKeyfile, FileMode.Create))
				{
					using (FileStream fos_public = new FileStream(publicKeyfile, FileMode.Create))
					{
						rsa.GenerateKey(fos_private, fos_public);
					}
				}
				Console.Out.WriteLine("Finish Generating RSA Key Pair.");
			}
			Assert.IsTrue(true);
		}
コード例 #2
0
		public static void Test()
		{
			Console.Out.WriteLine("Begin Example_010.");
			
			// Create some data to test with.
			Support.TestData(TESTDATA_FILE);

			/**
			 * Create a public / private RSA key pair.
			 */
			RSAService rsa = new RSAService();
			if (!rsa.AreKeysPresent(privateKeyfile, publicKeyfile))
			{
				Console.Out.WriteLine("Begin Create RSA Keys.");
				using (FileStream os_private = new FileStream(privateKeyfile, FileMode.Create),
					   os_public = new FileStream(publicKeyfile, FileMode.Create))
				{
					rsa.GenerateKey(os_private, os_public);
				}
				Console.Out.WriteLine("End Create RSA Keys.");
			}

			/**
			 * RSA keys are asynchronous; there is a public and private key. Each
			 * key can only decrypt data encrypted with the other key. A client
			 * process would not have both keys, this is only for demonstration
			 * purposes.
			 */

			Console.Out.WriteLine("Begin Read RSA Keys.");
			RSAPrivateKey privateKey = null;
			RSAPublicKey publicKey = null;
			using (FileStream is_private = new FileStream(privateKeyfile, FileMode.Open),
				   is_public = new FileStream(publicKeyfile, FileMode.Open))
			{
				privateKey = rsa.ReadPrivateKey(is_private);
				publicKey = rsa.ReadPublicKey(is_public);
			}
			Console.Out.WriteLine("End Read RSA Keys.");

			/**
			 * Use public key to encrypt a file stream directly to another file
			 * stream.
			 */
			Console.Out.WriteLine("Begin Encrypt Data.");
			using (FileStream outstream = new FileStream(TESTDATA_ENC_FILE, FileMode.Create),
				   instream = new FileStream(TESTDATA_FILE, FileMode.Open))
			{
				rsa.Encode(instream, outstream, publicKey);
			}
			Console.Out.WriteLine("End Encrypt Data.");

			/**
			 * Now decrypt the encrypted file using the private key.
			 */
			Console.Out.WriteLine("Begin Decrypt Data.");
			using (FileStream outstream = new FileStream(TESTDATA_DEC_FILE, FileMode.Create),
				instream = new FileStream(TESTDATA_ENC_FILE, FileMode.Open))
			{
				rsa.Decode(instream, outstream, privateKey);
			}
			Console.Out.WriteLine("End Decrypt Data.");

			/**
			 * Compare the original and decrypted files.
			 */
			string shaOriginal = DigestSHA.Sha256(new FileStream(TESTDATA_FILE, FileMode.Open));
			string shaDecripted = DigestSHA.Sha256(new FileStream(TESTDATA_DEC_FILE, FileMode.Open));
			if (Compare.SafeEquals(UTF8Encoding.UTF8.GetBytes(shaOriginal), UTF8Encoding.UTF8.GetBytes(shaDecripted)))
			{
				Console.Out.WriteLine("Encrypted and decrypted files are the same.");
			}
			else
			{
				Console.Out.WriteLine("Encrypted and decrypted files are NOT the same.");
			}
			Console.Out.WriteLine("End Example_010.");
		}
コード例 #3
0
		public static void Test()
		{
			Console.Out.WriteLine("Begin Example_020.");
			// Create some data to test with.
			Support.TestData(TESTDATA_FILE);

			/**
			 * Create a public / private RS key pair.
			 */
			RSAService rsa = new RSAService();
			if (!rsa.AreKeysPresent(privateKeyfile, publicKeyfile))
			{
				Console.Out.WriteLine("Begin Create RSA Keys.");
				rsa.GenerateKey(privateKeyfile, publicKeyfile);
				Console.Out.WriteLine("End Create RSA Keys.");
			}

			/**
			 * RSA keys are asynchronous; there is a public and private key. Each
			 * key can only decrypt data encrypted with the other key. A client
			 * process would not have both keys, this is only for demonstration
			 * purposes.
			 */
			Console.Out.WriteLine("Begin Read RSA Keys.");
			RSAPrivateKey privateKey = rsa.ReadPrivateKey(privateKeyfile);
			RSAPublicKey publicKey = rsa.ReadPublicKey(publicKeyfile);
			Console.Out.WriteLine("End Read RSA Keys.");

			/**
			 * Read the test data into a byte array. Be sure to use UTF-8 when
			 * converting between strings and byte arrays.
			 */
			Console.Out.WriteLine("Begin Read Data.");
			string testdata = File.ReadAllText(TESTDATA_FILE);
			byte[] testdata_bytes = UTF8Encoding.UTF8.GetBytes(testdata);
			Console.Out.WriteLine("End Read Data.");

			/**
			 * Use public key to encrypt a byte array to another byte array.
			 */
			Console.Out.WriteLine("Begin Encrypt Data.");
			byte[] testdata_enc = rsa.Encode(testdata_bytes, publicKey);
			Console.Out.WriteLine("End Encrypt Data.");

			/**
			 * Now decrypt the encrypted file using the private key.
			 */
			Console.Out.WriteLine("Begin Decrypt Data.");
			byte[] testdata_dec = rsa.Decode(testdata_enc, privateKey);
			Console.Out.WriteLine("End Decrypt Data.");

			/**
			 * Compare the original and decrypted files.
			 */
			String shaOriginal = DigestSHA.Sha256(testdata_bytes);
			String shaDecripted = DigestSHA.Sha256(testdata_dec);
			if (Compare.SafeEquals(UTF8Encoding.UTF8.GetBytes(shaOriginal), UTF8Encoding.UTF8.GetBytes(shaDecripted)))
			{
				Console.Out.WriteLine("Encrypted and decrypted files are the same.");
			}
			else
			{
				Console.Out.WriteLine("Encrypted and decrypted files are NOT the same.");
			}
			Console.Out.WriteLine("End Example_020.");
		}