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); } } }
/// <summary> /// - Gets specific encrypted source file from server. /// - Encryption and Decription using AES symetric and commonSecret derived from EC Diffie-Helman key Exchange /// - Decrypts source files /// - verifies digital signature of source code /// </summary> /// <param name="sourceFileInfo">the file to get</param> /// <returns>verified and decrypted source file or null if operation unsuccessfull</returns> private string DecryptSourceFromServer(SourceFileInfo sourceFileInfo) { byte[] dhClientPublic; byte[] dhClientPrivate; _keyGen.GenerateKeyPair(out dhClientPrivate, out dhClientPublic); string uri = (TroubleShooterClient.SERVICE_PATH + "/source"); ProtectedSourceRequest request = new ProtectedSourceRequest() { DhClientPublic = dhClientPublic, FileName = sourceFileInfo.FileName }; HttpResponseMessage response = _client.PostAsJsonAsync(uri, request).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { ProtectedSource source = response.Content.ReadAsAsync <ProtectedSource>().GetAwaiter().GetResult(); byte[] sharedSecret = _diffieHelman.SharedSecret(dhClientPrivate, source.DhPublicServer); string decryptedSource = AesHandler.DecryptStringFromBytes_Aes(source.SourceCode, sharedSecret); if (_verifier.VerifySignature(decryptedSource, source.Signature, _signatureKey)) { return(decryptedSource); } else { return(null); } } return(null); }