public static bool TestKeyExchange(AsymmetricKeyExchangeFormatter f, AsymmetricKeyExchangeDeformatter d, bool expct)
    {
        bool bRes = true;

        Random rnd = new Random();
        int    len = rnd.Next(12) + 5;

        byte[] data = new byte[len];

        byte[] exc  = f.CreateKeyExchange(data);
        byte[] exct = f.CreateKeyExchange(data, typeof(RC2CryptoServiceProvider));

        try
        {
            byte[] res  = d.DecryptKeyExchange(exc);
            byte[] rest = d.DecryptKeyExchange(exct);

            if (!Compare(res, data))
            {
                Console.WriteLine("KeyExchangeFormatter/Deformatter failed to roundtrip #1");
                bRes = false;
            }

            if (!Compare(rest, data))
            {
                Console.WriteLine("KeyExchangeFormatter/Deformatter failed to roundtrip #2");
                bRes = false;
            }
        }
        catch (CryptographicException e)
        {
            Console.Write("EXCEPTION: " + e.Message);
            bRes = false;
        }

        bRes = (bRes == expct);

        if (bRes)
        {
            Console.WriteLine("OK  (expct was " + expct + ")");
        }
        else
        {
            Console.WriteLine("FAIL  (expct was " + expct + ")");
        }

        return(bRes);
    }
Beispiel #2
0
        private static void VerifyDecryptKeyExchange(
            AsymmetricKeyExchangeFormatter formatter,
            AsymmetricKeyExchangeDeformatter deformatter)
        {
            byte[] encrypted = formatter.CreateKeyExchange(TestData.HelloBytes);
            byte[] decrypted = deformatter.DecryptKeyExchange(encrypted);
            Assert.Equal(TestData.HelloBytes, decrypted);

            encrypted[encrypted.Length - 1] ^= 0xff;

            try
            {
                byte[] invalidMessage = deformatter.DecryptKeyExchange(encrypted);

                // RSAEncryptionPadding.Pkcs1 has loose integrity checking, recognizing ~1/110000
                // messages as decryptable. So we only have a logic problem in our code if we produce
                // the original input again. (The odds of a random payload producing "Hello" for a
                // 2048-bit key are 1 in 49 quintillion (4.869e19)).
                //
                // Since we're basing "invalid" off of "valid" the odds will be different than true
                // random, but it's not obvious if they're better or worse.
                if (invalidMessage.SequenceEqual(TestData.HelloBytes))
                {
                    string msg = $"Decrypt was unexpectedly successful: {encrypted.ByteArrayToHex()}";

                    // Just in case the exception text gets trimmed from test logs, Console.WriteLine it.
                    Console.WriteLine(msg);
                    throw new InvalidOperationException(msg);
                }
            }
            catch (CryptographicException)
            {
                // Equivalent to Assert.ThrowsAny<CryptographicException>
            }
        }
Beispiel #3
0
        private static void VerifyDecryptKeyExchange(AsymmetricKeyExchangeFormatter formatter, AsymmetricKeyExchangeDeformatter deformatter)
        {
            byte[] encrypted = formatter.CreateKeyExchange(TestData.HelloBytes);
            byte[] decrypted = deformatter.DecryptKeyExchange(encrypted);
            Assert.Equal(TestData.HelloBytes, decrypted);

            encrypted[encrypted.Length - 1] ^= 0xff;
            Assert.ThrowsAny <CryptographicException>(() => deformatter.DecryptKeyExchange(encrypted));
        }
        private static void VerifyDecryptKeyExchange(AsymmetricKeyExchangeFormatter formatter, AsymmetricKeyExchangeDeformatter deformatter)
        {
            byte[] encrypted = formatter.CreateKeyExchange(TestData.HelloBytes);
            byte[] decrypted = deformatter.DecryptKeyExchange(encrypted);
            Assert.Equal(TestData.HelloBytes, decrypted);

            encrypted[encrypted.Length - 1] ^= 0xff;
            Assert.ThrowsAny<CryptographicException>(() => deformatter.DecryptKeyExchange(encrypted));
        }
	public static bool TestKeyExchange(AsymmetricKeyExchangeFormatter f, AsymmetricKeyExchangeDeformatter d, bool expct)
	{
		bool bRes = true;

		Random rnd = new Random();
		int len = rnd.Next(12)+5;
        byte[] data = new byte[len];

		byte[] exc = f.CreateKeyExchange(data);
		byte[] exct = f.CreateKeyExchange(data, typeof(RC2CryptoServiceProvider));

		try 
		{
			byte[] res = d.DecryptKeyExchange(exc);
			byte[] rest = d.DecryptKeyExchange(exct);

			if (!Compare(res, data)) 
			{
				Console.WriteLine("KeyExchangeFormatter/Deformatter failed to roundtrip #1");
				bRes = false;
			}

			if (!Compare(rest, data)) 
			{
				Console.WriteLine("KeyExchangeFormatter/Deformatter failed to roundtrip #2");
				bRes = false;
			}
		}
		catch(CryptographicException e)
		{
			Console.Write("EXCEPTION: " + e.Message);
			bRes = false;
		}

		bRes = (bRes==expct);

		if (bRes)
			Console.WriteLine("OK  (expct was " + expct + ")");
		else
			Console.WriteLine("FAIL  (expct was " + expct + ")");

		return bRes;
	}
Beispiel #6
0
 private static void InvalidFormatterArguments(AsymmetricKeyExchangeFormatter formatter)
 {
     Assert.Throws <ArgumentNullException>(() => formatter.SetKey(null));
     Assert.Throws <CryptographicUnexpectedOperationException>(() => formatter.CreateKeyExchange(new byte[] { 0, 1, 2, 3 }));
 }
 private static void InvalidFormatterArguments(AsymmetricKeyExchangeFormatter formatter)
 {
     Assert.Throws<ArgumentNullException>(() => formatter.SetKey(null));
     Assert.Throws<CryptographicUnexpectedOperationException>(() => formatter.CreateKeyExchange(new byte[] { 0, 1, 2, 3 }));
 }