Exemplo n.º 1
0
        /// <summary>
        /// Generate a key pair
        /// </summary>
        public void Generate()
        {
            if (KeyProvider == "RSA")
            {
                using (var rsa = new RSA())
                {
                    BigNumber exponent = 0x10001; // this needs to be a prime number
                    rsa.GenerateKeys(KeySize, exponent, null, null);

                    KeyPair = new CryptoKey(rsa);
                }
            }
            //else if (keyprovider.StartsWith("ECDSA"))
            //{
            //    //  var x=    new Al.Security.Crypto.Generators.DsaKeyPairGenerator();
            //    X9ECParameters ecP = NistNamedCurves.GetByName(keyprovider.Replace("ECDSA_","").Insert(1,"-"));
            //    ECDomainParameters ecSpec = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed());
            //    ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator("ECDSA");

            //    keyPairGenerator.Init(new ECKeyGenerationParameters(ecSpec, new SecureRandom()));
            //    KeyPair = keyPairGenerator.GenerateKeyPair();
            //}
            else
            {
                using (var dsa = new DSA(KeySize, null, null))
                {
                    //   BigNumber exponent = 0x10001; // this needs to be a prime number
                    dsa.GenerateKeys();

                    KeyPair = new CryptoKey(dsa);
                }
            }
        }
Exemplo n.º 2
0
        private void DoTest(DSA dsa)
        {
            Console.WriteLine("seed");
            Console.WriteLine(BitConverter.ToString(seed));
            Console.WriteLine("counter={0} h={1}", dsa.Counter, dsa.H);

            Console.WriteLine(dsa);

            Assert.AreEqual(105, dsa.Counter);
            Assert.AreEqual(2, dsa.H.ToInt32());

            using (BigNumber q = BigNumber.FromArray(this.out_q)) {
                Assert.IsTrue(q == dsa.Q);
            }

            using (BigNumber p = BigNumber.FromArray(this.out_p)) {
                Assert.IsTrue(p == dsa.P);
            }

            using (BigNumber g = BigNumber.FromArray(this.out_g)) {
                Assert.IsTrue(g == dsa.G);
            }

            byte[] msg = Encoding.ASCII.GetBytes(str1);

            dsa.ConstantTime = true;
            dsa.GenerateKeys();

            byte[] sig = dsa.Sign(msg);
            Assert.IsTrue(dsa.Verify(msg, sig));

            dsa.ConstantTime = false;
            dsa.GenerateKeys();
            sig = dsa.Sign(msg);
            Assert.IsTrue(dsa.Verify(msg, sig));
        }
Exemplo n.º 3
0
        private void DoTest(DSA dsa)
        {
            Console.WriteLine("seed");
            Console.WriteLine(BitConverter.ToString(seed));
            Console.WriteLine("counter={0} h={1}", dsa.Counter, dsa.H);

            Console.WriteLine(dsa);

            if (dsa.Counter != 105)
            {
                throw new Exception("counter should be 105");
            }

            if (dsa.H != 2)
            {
                throw new Exception("h should be 2");
            }

            using (BigNumber q = BigNumber.FromArray(this.out_q))
            {
                if (dsa.Q != q)
                {
                    throw new Exception("q value is wrong");
                }
            }

            using (BigNumber p = BigNumber.FromArray(this.out_p))
            {
                if (dsa.P != p)
                {
                    throw new Exception("p value is wrong");
                }
            }

            using (BigNumber g = BigNumber.FromArray(this.out_g))
            {
                if (dsa.G != g)
                {
                    throw new Exception("g value is wrong");
                }
            }

            byte[] msg = Encoding.ASCII.GetBytes(str1);

            dsa.ConstantTime = true;
            dsa.GenerateKeys();

            byte[] sig = dsa.Sign(msg);
            if (!dsa.Verify(msg, sig))
            {
                throw new Exception("DSA signature failed to verify");
            }

            dsa.ConstantTime = false;
            dsa.GenerateKeys();
            sig = dsa.Sign(msg);
            if (!dsa.Verify(msg, sig))
            {
                throw new Exception("DSA signature failed to verify");
            }
        }