public void Properties()
        {
            RSAOAEPKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter();

            keyex.SetKey(key);
            Assert.IsNull(keyex.Parameter, "RSAOAEPKeyExchangeFormatter.Parameter");
            Assert.IsNull(keyex.Parameters, "RSAOAEPKeyExchangeFormatter.Parameters");
            Assert.IsNull(keyex.Rng, "RSAOAEPKeyExchangeFormatter.Rng");
            Assert.AreEqual("System.Security.Cryptography.RSAOAEPKeyExchangeFormatter", keyex.ToString());
        }
Example #2
0
        public void Properties()
        {
            RSAOAEPKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter();

            keyex.SetKey(key);
            Assertion.AssertNull("RSAOAEPKeyExchangeFormatter.Parameter", keyex.Parameter);
            Assertion.AssertNull("RSAOAEPKeyExchangeFormatter.Parameters", keyex.Parameters);
            Assertion.AssertNull("RSAOAEPKeyExchangeFormatter.Rng", keyex.Rng);
            Assertion.AssertEquals("RSAOAEPKeyExchangeFormatter.ToString()", "System.Security.Cryptography.RSAOAEPKeyExchangeFormatter", keyex.ToString());
        }
Example #3
0
        public byte[] EncryptSessionKeyByRSA(byte[] sessionKey, string keyToUse)
        {
            AsymmetricAlgorithm rsa = algo;

            if (!String.IsNullOrEmpty(keyToUse))
            {
                rsa.FromXmlString(keyToUse);
            }
            RSAOAEPKeyExchangeFormatter forma = new RSAOAEPKeyExchangeFormatter();

            forma.SetKey(rsa);
            byte[] exchangeData = forma.CreateKeyExchange(sessionKey);
            return(exchangeData);
        }
Example #4
0
    // Create an RSAOAEPKeyExchangeFormatter object with a new RSA key.
    // Display its properties to the console.
    private void ConstructFormatter()
    {
        // Construct an empty Optimal Asymmetric Encryption Padding (OAEP)
        // key exchange.
        //<Snippet1>
        RSAOAEPKeyExchangeFormatter rsaFormatter =
            new RSAOAEPKeyExchangeFormatter();
        //</Snippet1>

        // Create an RSA and set it into the specified
        // RSAOAEPKeyExchangeFormatter.
        //<Snippet5>
        RSA key = RSA.Create();

        rsaFormatter.SetKey(key);
        //</Snippet5>

        // Create a random number using the RNGCryptoServiceProvider provider.
        //<Snippet6>
        RNGCryptoServiceProvider ring = new RNGCryptoServiceProvider();

        rsaFormatter.Rng = ring;
        //</Snippet6>

        // Export InverseQ and set it into the RSAOAEPKeyExchangeFormatter.
        //<Snippet7>
        rsaFormatter.Parameter = key.ExportParameters(true).InverseQ;
        //</Snippet7>

        Console.WriteLine();
        Console.WriteLine("**" + rsaFormatter.ToString() + "**");
        Console.Write("The following random number was generated using the ");
        Console.WriteLine("class:");
        Console.WriteLine(rsaFormatter.Rng);

        Console.WriteLine();
        Console.Write("The RSA formatter contains the following InverseQ");
        Console.WriteLine(" parameter:");
        Console.WriteLine(Encoding.ASCII.GetString(rsaFormatter.Parameter));

        Console.WriteLine();
        //<Snippet13>
        string xmlParameters = rsaFormatter.Parameters;

        //</Snippet13>

        Console.WriteLine("The RSA formatter has the following parameters:");
        Console.WriteLine(xmlParameters);
    }
    public static Boolean Test()
    {
        Boolean bRes = true;

        RSAPKCS1KeyExchangeFormatter   pcef1 = new RSAPKCS1KeyExchangeFormatter(RSA.Create());
        RSAPKCS1KeyExchangeDeformatter pced1 = new RSAPKCS1KeyExchangeDeformatter(RSA.Create());

        Console.WriteLine("pcef1 parameters: " + pcef1.Parameters + "\npced1 parameters: " + pced1.Parameters);

        bRes = TestKeyExchange(pcef1, pced1, false) && bRes;

        RSA rsa = RSA.Create();
        RandomNumberGenerator          rng   = new RNGCryptoServiceProvider();
        RSAPKCS1KeyExchangeFormatter   pcef2 = new RSAPKCS1KeyExchangeFormatter();
        RSAPKCS1KeyExchangeDeformatter pced2 = new RSAPKCS1KeyExchangeDeformatter(rsa);
        RSA rsa1 = RSA.Create();

        rsa1.ImportParameters(rsa.ExportParameters(false));
        pcef2.SetKey(rsa1);
        pcef2.Rng = rng;
        pced2.RNG = rng;
        Console.WriteLine("pcef2 parameters: " + pcef2.Parameters + "\npced2 parameters: " + pced2.Parameters);

        bRes = TestKeyExchange(pcef2, pced2, true) && bRes;

        RSAOAEPKeyExchangeFormatter   ocef1 = new RSAOAEPKeyExchangeFormatter(RSA.Create());
        RSAOAEPKeyExchangeDeformatter oced1 = new RSAOAEPKeyExchangeDeformatter(RSA.Create());

        Console.WriteLine("ocef1 parameters: " + ocef1.Parameters + "\noced1 parameters: " + oced1.Parameters);

        bRes = TestKeyExchange(ocef1, oced1, false) && bRes;

        rsa = RSA.Create();
        rng = new RNGCryptoServiceProvider();
        RSAOAEPKeyExchangeFormatter   ocef2 = new RSAOAEPKeyExchangeFormatter();
        RSAOAEPKeyExchangeDeformatter oced2 = new RSAOAEPKeyExchangeDeformatter(rsa);

        rsa1 = RSA.Create();
        rsa1.ImportParameters(rsa.ExportParameters(false));
        ocef2.SetKey(rsa1);
        ocef2.Rng = rng;
//		oced2.RNG = rng;
        Console.WriteLine("ocef2 parameters: " + ocef2.Parameters + "\noced2 parameters: " + oced2.Parameters);

        bRes = TestKeyExchange(ocef2, oced2, true) && bRes;

        return(bRes);
    }
    public static Boolean Test()
    {
        Boolean bRes = true;

		RSAPKCS1KeyExchangeFormatter pcef1 = new RSAPKCS1KeyExchangeFormatter(RSA.Create());
		RSAPKCS1KeyExchangeDeformatter pced1 = new RSAPKCS1KeyExchangeDeformatter(RSA.Create());
		Console.WriteLine("pcef1 parameters: " + pcef1.Parameters + "\npced1 parameters: " + pced1.Parameters);

		bRes = TestKeyExchange(pcef1, pced1, false) && bRes;

		RSA rsa = RSA.Create();
		RandomNumberGenerator rng = new RNGCryptoServiceProvider();
		RSAPKCS1KeyExchangeFormatter pcef2 = new RSAPKCS1KeyExchangeFormatter();
		RSAPKCS1KeyExchangeDeformatter pced2 = new RSAPKCS1KeyExchangeDeformatter(rsa);
		RSA rsa1 = RSA.Create();
		rsa1.ImportParameters(rsa.ExportParameters(false));
		pcef2.SetKey(rsa1);
		pcef2.Rng = rng;
		pced2.RNG = rng;
		Console.WriteLine("pcef2 parameters: " + pcef2.Parameters + "\npced2 parameters: " + pced2.Parameters);

		bRes = TestKeyExchange(pcef2, pced2, true) && bRes;

		RSAOAEPKeyExchangeFormatter ocef1 = new RSAOAEPKeyExchangeFormatter(RSA.Create());
		RSAOAEPKeyExchangeDeformatter oced1 = new RSAOAEPKeyExchangeDeformatter(RSA.Create());
		Console.WriteLine("ocef1 parameters: " + ocef1.Parameters + "\noced1 parameters: " + oced1.Parameters);

		bRes = TestKeyExchange(ocef1, oced1, false) && bRes;

		rsa = RSA.Create();
		rng = new RNGCryptoServiceProvider();
		RSAOAEPKeyExchangeFormatter ocef2 = new RSAOAEPKeyExchangeFormatter();
		RSAOAEPKeyExchangeDeformatter oced2 = new RSAOAEPKeyExchangeDeformatter(rsa);
		rsa1 = RSA.Create();
		rsa1.ImportParameters(rsa.ExportParameters(false));
		ocef2.SetKey(rsa1);
		ocef2.Rng = rng;
//		oced2.RNG = rng;
		Console.WriteLine("ocef2 parameters: " + ocef2.Parameters + "\noced2 parameters: " + oced2.Parameters);

		bRes = TestKeyExchange(ocef2, oced2, true) && bRes;

        return bRes;
    }