Esempio n. 1
0
 /// <summary>
 /// Creates provider for specified eliptic curve technology.
 /// </summary>
 /// <param name="curve"></param>
 public ECProvider(ElipticCurve curve)
 {
     this.curve = curve;
     calculator = new PointCalculator(curve);
     serialiser = new PointSerialiser(curve);
     rnd        = new RandomBigNumbers();
 }
Esempio n. 2
0
        private static void runTest()
        {
            int iterations = 10;

            DiffieHelmanTest dht = new DiffieHelmanTest();

            dht.TestDiffieHelman(ElipticCurve.secp521r1(), iterations);
            dht.TestDiffieHelman(ElipticCurve.Secp112r1(), iterations);
            dht.TestDiffieHelman(ElipticCurve.secp160r1(), iterations);
            dht.TestDiffieHelman(ElipticCurve.secp192r1(), iterations);
            dht.TestDiffieHelman(ElipticCurve.secp256r1(), iterations);
            dht.TestDiffieHelman(ElipticCurve.TestCurve(), iterations);

            CryptographyTests ct = new CryptographyTests();

            ct.TestEncryption(ElipticCurve.secp521r1(), iterations, Encoding.UTF32);
            ct.TestEncryption(ElipticCurve.secp521r1(), iterations, Encoding.Unicode);
            ct.TestEncryption(ElipticCurve.Secp112r1(), iterations, Encoding.Unicode);
            ct.TestEncryption(ElipticCurve.secp160r1(), iterations, Encoding.UTF32);
            ct.TestEncryption(ElipticCurve.secp192r1(), iterations, Encoding.UTF32);
            ct.TestEncryption(ElipticCurve.secp256r1(), iterations, Encoding.Unicode);

            DigitalSignatureTests st = new DigitalSignatureTests();

            st.TestSignature(ElipticCurve.secp521r1(), iterations);
            st.TestSignature(ElipticCurve.Secp112r1(), iterations);
            st.TestSignature(ElipticCurve.secp160r1(), iterations);
            st.TestSignature(ElipticCurve.secp192r1(), iterations);
            st.TestSignature(ElipticCurve.secp256r1(), iterations);
            st.TestSignature(ElipticCurve.TestCurve(), iterations);
        }
Esempio n. 3
0
        public void TestDiffieHelman(ElipticCurve curve, int iterations)
        {
            ECKeysGenerator keyGen = new ECKeysGenerator(curve);
            ECDiffieHelman  diffie = new ECDiffieHelman(curve);

            //generovanie klucoveho paru
            byte[] privateKeyAlice;
            byte[] publicKeyAlice;
            byte[] privateKeyBob;
            byte[] publicKeyBob;
            for (int i = 0; i < iterations; i++)
            {
                Console.WriteLine($"diffie-helman curve {curve.Name} test {i}... ");
                keyGen.GenerateKeyPair(out privateKeyAlice, out publicKeyAlice);
                keyGen.GenerateKeyPair(out privateKeyBob, out publicKeyBob);
                string commnonAlices = Convert.ToBase64String(diffie.SharedSecret(privateKeyAlice, publicKeyBob));
                string commnonBobs   = Convert.ToBase64String(diffie.SharedSecret(privateKeyBob, publicKeyAlice));
                Console.WriteLine(commnonAlices);
                if (commnonAlices != commnonBobs)
                {
                    throw new Exception("Fatal Error");
                }
                Write("OK", ConsoleColor.Green);
            }
        }
Esempio n. 4
0
 private static void generateKeys()
 {
     generateKey(ElipticCurve.Secp112r1());
     generateKey(ElipticCurve.secp160r1());
     generateKey(ElipticCurve.secp192r1());
     generateKey(ElipticCurve.secp256r1());
     generateKey(ElipticCurve.secp521r1());
     generateKey(ElipticCurve.TestCurve());
 }
Esempio n. 5
0
        /// <summary>
        /// initialise updater.
        /// </summary>
        /// <param name="updateDir"></param>
        public UpdateController(string updateDir, TroubleShooterClient client)
        {
            this._client = client;
            _updateDir   = updateDir;
            ElipticCurve curve = ElipticCurve.secp160r1();

            _keyGen       = new ECKeysGenerator(curve);
            _verifier     = new ECSignature(curve);
            _diffieHelman = new ECDiffieHelman(curve);
        }
Esempio n. 6
0
 private static void generateKey(ElipticCurve curve)
 {
     Console.ForegroundColor = ConsoleColor.Yellow;
     Console.WriteLine(curve.Name);
     Console.ResetColor();
     byte[] privateKey;
     byte[] publicKey;
     new ECKeysGenerator(curve).GenerateKeyPair(out privateKey, out publicKey);
     Console.WriteLine($"private:{Convert.ToBase64String(privateKey)}");
     Console.WriteLine($"public :{Convert.ToBase64String(publicKey)}");
 }
Esempio n. 7
0
        /// <summary>
        /// <see cref="TroubleshooterController"/>
        /// </summary>
        public TroubleshooterController(ServisContext ctx)
        {
            this.ctx = ctx;
            ElipticCurve curve = ElipticCurve.secp160r1();

            signatureMaker = new ECSignature(curve);
            keyGen         = new ECKeysGenerator(curve);
            diffieHelman   = new ECDiffieHelman(curve);
            sourceFiles    = SourceFileInfoBuilder.GetSourceFiles(SOURCE_FILES_DIR);
            rnd            = new Random();
        }
Esempio n. 8
0
        public void TestSignature(ElipticCurve curve, int iterations)
        {
            ECKeysGenerator keyGen    = new ECKeysGenerator(curve);
            ECSignature     signature = new ECSignature(curve);

            for (int i = 0; i < iterations; i++)
            {
                Console.WriteLine($"signature curve {curve.Name} test {i}... ");
                //generovanie klucoveho paru
                byte[] privateKey1;
                byte[] publicKey1;
                keyGen.GenerateKeyPair(out privateKey1, out publicKey1);
                byte[] privateKey2;
                byte[] publicKey2;
                keyGen.GenerateKeyPair(out privateKey2, out publicKey2);

                string str1 = RandomString(random.Next(100));
                string str2 = RandomString(random.Next(100));
                while (str1 == str2)
                {
                    str2 = RandomString(random.Next(100));
                }

                byte[] sign1 = signature.Signature(str1, privateKey1);
                byte[] sign2 = signature.Signature(str2, privateKey1);

                Console.WriteLine(Convert.ToBase64String(sign1));

                if (!signature.VerifySignature(str1, sign1, publicKey1))
                {
                    Write($"Signature should be valid !!! ", ConsoleColor.Red);
                    throw new Exception("Fatal error");
                }
                if (signature.VerifySignature(str1, sign1, publicKey2))
                {
                    Write($"Signature should not be valid because of wrong public key !!!", ConsoleColor.DarkYellow);
                }
                if (signature.VerifySignature(str2, sign1, publicKey1))
                {
                    Console.WriteLine();
                    Write($"Signature should not be valid because of changed message!!!", ConsoleColor.DarkYellow);
                }
                else
                {
                    Write("OK", ConsoleColor.Green);
                }
            }
        }
Esempio n. 9
0
        public void GenerateCurveAndPointCheck()
        {
            ElipticCurve curve = new ElipticCurve(64);

            curve.GenerateRandomCurve();

            Point p1 = curve.GenerateRandomPointOnCurve();

            Assert.IsTrue(curve.CheckIfPointBelongsToCurve(p1));

            Point p2 = curve.GenerateRandomPointOnCurve();

            Assert.IsTrue(curve.CheckIfPointBelongsToCurve(p1));

            Assert.IsTrue(curve.CheckIfPointBelongsToCurve(p1 + p1));

            Assert.IsTrue(curve.CheckIfPointBelongsToCurve(p1 + p2));
        }
Esempio n. 10
0
        public void ScalarMultiplicationTests()
        {
            ElipticCurve curve = new ElipticCurve(64);

            curve.GenerateRandomCurve();
            Point randomPoint = curve.GenerateRandomPointOnCurve();

            Point testPoint = curve.ScalarMultiplication(randomPoint, 1);

            Assert.IsTrue(curve.CheckIfPointBelongsToCurve(testPoint));

            testPoint = curve.ScalarMultiplication(randomPoint, 5);
            Assert.IsTrue(curve.CheckIfPointBelongsToCurve(testPoint));

            testPoint = curve.ScalarMultiplication(randomPoint, 100);
            Assert.IsTrue(curve.CheckIfPointBelongsToCurve(testPoint));

            testPoint = curve.ScalarMultiplication(randomPoint, BigInteger.Parse("999999999999999999999999999999999999999999999999999999999999"));

            Assert.IsTrue(curve.CheckIfPointBelongsToCurve(testPoint));
        }
Esempio n. 11
0
        public void TestEncryption(ElipticCurve curve, int iterations, Encoding enc)
        {
            ECKeysGenerator keyGen = new ECKeysGenerator(curve);
            ECEncryption    crypto = new ECEncryption(curve);

            //generovanie klucoveho paru
            byte[] privateKey;
            byte[] publicKey;
            for (int i = 0; i < iterations; i++)
            {
                Console.WriteLine($"encrtyption curve {curve.Name} test {i}... ");
                keyGen.GenerateKeyPair(out privateKey, out publicKey);
                string test   = RandomString(random.Next(100));
                byte[] cipher = crypto.Encrypt(test, publicKey, enc);
                Console.WriteLine(Convert.ToBase64String(cipher));
                string decoded = crypto.Decrypt(cipher, privateKey, enc);
                if (test != decoded)
                {
                    throw new Exception("Fatal Error");
                }
                Write("OK", ConsoleColor.Green);
            }
        }
Esempio n. 12
0
        public void CheckIfStaticPointsBelongsToCurve()
        {
            BigInteger   a     = 6;
            BigInteger   p     = 11;
            ElipticCurve curve = new ElipticCurve(a, 4, p);
            Point        p1    = new Point(7, 9, a, p);
            Point        p2    = new Point(3, 4, a, p);

            bool result = curve.CheckIfPointBelongsToCurve(p1);

            Assert.IsTrue(result);

            result = curve.CheckIfPointBelongsToCurve(p2);
            Assert.IsTrue(result);

            result = curve.CheckIfPointBelongsToCurve(p1 + p1);
            Assert.IsTrue(result);

            result = curve.CheckIfPointBelongsToCurve(p1 + p2);
            Assert.IsTrue(result);

            result = curve.CheckIfPointBelongsToCurve(Point.NeutralElement(a, p));
            Assert.IsTrue(result);
        }