Beispiel #1
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);
            }
        }
Beispiel #2
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);
        }
Beispiel #3
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();
        }
Beispiel #4
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);
                }
            }
        }
Beispiel #5
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);
            }
        }