c# - .NET MD5 signature does not match OpenSSL MD5 signature -
i have sign messages web service. service uses openssl md5 signatures. generate openssl 1 pem private key file:
openssl.exe dgst -md5 -hex -out signature.txt -sign privkey.pem texttosign.txt
which generates next signature:
7078388bd081d4b805feb020ab47352320919e4638e36b59d66a684c9bb12a745aaf172e4da2686c3e3750bf627c980a19700f6d8bbd0b62d8714a965a34be2e9f4147ac054c4af1050cdcebd9b475f0ef28e520681b0d67104b8e633ee592bb3ec2c517fb8cf7b13bd86424f00c89518e063d55e7922adab7cf607c85920862
i want implement in code. actual code looks (the private key in pfx file same 1 in pem file):
private static string sign(string stringtosign) { x509certificate2 cert = new x509certificate2("keys.pfx", "", x509keystorageflags.exportable); byte[] info = encoding.utf8.getbytes(stringtosign); byte[] signeddata; using (md5 hasher = md5.create()) using (rsacryptoserviceprovider rsa = (rsacryptoserviceprovider)cert.privatekey) { signeddata = rsa.signdata(data, hasher); } homecoming convert.tobase64string(signeddata); }
this code produces next signature...
chg4i9cb1lgf/ragq0c1iycrnky442tz1mpotjuxknrarxcutajobd43ul9ifjgkgxapbyu9c2lycuqwwjs+lp9br6wfterxbqzc69m0dfdvkougabsnzxbljmm+5zk7pslff/um97e72gqk8ayjuy4gpvxnkirat89gfiwscgi=
...which not match opensll one. should match openssl one? tried utilize openssl.net, did not find resources/tutoral signing.
solved
the right code is:
private static string sign(string stringtosign) { x509certificate2 cert = new x509certificate2("#02299991.pfx", "#02299991", x509keystorageflags.exportable); byte[] info = encoding.utf8.getbytes(stringtosign); byte[] signeddata; using (md5 hasher = md5.create()) using (rsacryptoserviceprovider rsa = (rsacryptoserviceprovider)cert.privatekey) { signeddata = rsa.signdata(data, hasher); } homecoming bytearraytostring(signeddata); //convert.tobase64string(signeddata); } public static string bytearraytostring(byte[] signedbytes) { stringbuilder hex = new stringbuilder(signedbytes.length * 2); foreach (byte b in signedbytes) hex.appendformat("{0:x2}", b); homecoming hex.tostring(); }
your openssl output hex string:
openssl.exe dgst -md5 -hex -out signature.txt -sign privkey.pem texttosign.txt ^
while c# output in base64:
return convert.tobase64string(signeddata); ^
convert byte[]
output hex string:
c# cryptography openssl md5
No comments:
Post a Comment