Пример #1
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);
                }
            }
        }
Пример #2
0
        public ProtectedSource Get([FromBody] ProtectedSourceRequest request)
        {
            //read source code
            string sourceCode = System.IO.File.ReadAllText(Path.Combine(SOURCE_FILES_DIR, request.FileName));

            //generate key pair and derive shared secret
            byte[] dhServerPublic;
            byte[] dhServerPrivate;
            keyGen.GenerateKeyPair(out dhServerPrivate, out dhServerPublic);
            byte[] sharedSecret = diffieHelman.SharedSecret(dhServerPrivate, request.DhClientPublic);

            //send encrypted and signed source back to client;
            //send also servers public key so client can derive common secret
            return(new ProtectedSource()
            {
                SourceCode = AesHandler.EncryptStringToBytes_Aes(sourceCode, sharedSecret),
                DhPublicServer = dhServerPublic,
                Signature = signatureMaker.Signature(sourceCode, signatureKey)
            });
        }