Пример #1
0
		public void CreateDecryptorInvalid()
		{
			using (SymmetricAlgorithm salsa20 = new Salsa20())
			{
				Assert.Throws<ArgumentNullException>(() => salsa20.CreateDecryptor(null, new byte[8]));
				Assert.Throws<ArgumentNullException>(() => salsa20.CreateDecryptor(new byte[16], null));
				Assert.Throws<CryptographicException>(() => salsa20.CreateDecryptor(new byte[15], new byte[8]));
				Assert.Throws<CryptographicException>(() => salsa20.CreateDecryptor(new byte[16], new byte[7]));
			}
		}
Пример #2
0
		public void EncryptDecrypt()
		{
			// generate data to encrypt
			byte[] input;
			using (MemoryStream stream = new MemoryStream())
			using (BinaryWriter writer = new BinaryWriter(stream))
			{
				for (short value = -1024; value <= 1023; value++)
					writer.Write(value);
				writer.Flush();

				input = stream.ToArray();
			}

			using (SymmetricAlgorithm salsa20 = new Salsa20())
			{
				byte[] encrypted = new byte[input.Length];
				using (ICryptoTransform encrypt = salsa20.CreateEncryptor())
					encrypt.TransformBlock(input, 0, input.Length, encrypted, 0);

				byte[] decrypted = new byte[input.Length];
				using (ICryptoTransform decrypt = salsa20.CreateDecryptor())
					decrypt.TransformBlock(encrypted, 0, encrypted.Length, decrypted, 0);

				CollectionAssert.AreEqual(input, decrypted);
			}
		}