Good day, I'm generating a message on C# that has to be passed to a RPC server in python 3.7 which will sign it and give it back to C# to verify.
I just recently started to learn cryptography and python and whatever I sign can't then be verified in C#, I followed every doc I could find and I think I'm using the same algorithms.
This is how I generate the keys, since the .Net library generates xml keys I then convert the private one to .pem with BouncyCastle utilities to work with python's pycryptodome library:
using (DSA dsa = new DSACng(2048))
{
var privateKey = dsa.ToXmlString(true);
var publicKey = dsa.ToXmlString(false);
File.WriteAllText("privateKey.xml", privateKey);
File.WriteAllText("publicKey.xml", publicKey);
AsymmetricCipherKeyPair dsaKey = DotNetUtilities.GetDsaKeyPair(dsa);
using (StreamWriter sw = new StreamWriter("privateKey.pem"))
{
PemWriter pw = new PemWriter(sw);
pw.WriteObject(dsaKey);
}
}
Then in python:
def sign_message(par1, par2, par3):
message = _generate_message(par1, par2, par3).encode('utf-8')
h = SHA256.new(message)
b64Message = base64.b64encode(h.digest())
f = open("privateKey.pem", "r")
key = DSA.import_key(f.read())
signer = DSS.new(key, 'fips-186-3')
signature = signer.sign(h)
b64Signature = base64.b64encode(signature)
return [b64Message, b64Signature]
But then if I verify the message in C# it only prints out "Not verified":
using(DSA dsa = new DSACng(2048)
{
byte[] message = response[0];
byte[] signature = response[1];
var publicKey = File.ReadAllText("publicKey.xml");
dsa.FromXmlString(publicKey);
if (dsa.VerifyData(message, signature, HashAlgorithmName.SHA256))
Console.WriteLine("Verified");
else
Console.WriteLine("Not verified");
}
What is wrong in my code? Any help is appreciated