Signature with RSA-SHA256 - c#

I've been since yesterday trying a lot of the "solutions" on StackOverflow, but none seems to work.
Given a .pfx certificate with a private key I need to sign a byte array (firmware version of the tracking unit we use).
Here are the things I've tried:
private byte[] generateSignature(byte[] data, X509Certificate2 certificate)
{
RSACryptoServiceProvider key = new RSACryptoServiceProvider();
key.FromXmlString(certificate.PrivateKey.ToXmlString(true));
return key.SignData(data, CryptoConfig.MapNameToOID("SHA256"));
}
And also:
private byte[] generateSignature(byte[] data, X509Certificate2 certificate)
{
string alg = CryptoConfig.MapNameToOID("SHA256");
RSACryptoServiceProvider rsaProvider = (RSACryptoServiceProvider)certificate.PrivateKey;
return rsaProvider.SignData(orig, alg);
}
And also:
private byte[] generateSignature(byte[] data, X509Certificate2 certificate)
{
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
byte[] hash;
using (SHA256 sha256 = SHA256.Create())
{
hash = sha256.ComputeHash(data);
}
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(rsa);
RSAFormatter.SetKey(certificate.PrivateKey);
RSAFormatter.SetHashAlgorithm("SHA256");
return RSAFormatter.CreateSignature(hash);
}
And finally:
private byte[] generateSignature(byte[] data, X509Certificate2 certificate)
{
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)certificate.PrivateKey;
SHA1Managed sha1 = new SHA1Managed();
SHA256Managed sha256 = new SHA256Managed();
byte[] hash = sha256.ComputeHash(data);
csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA256"));
}
On all of these I get the error:
System.Security.Cryptography.CryptographicException:
'Invalid algorithm specified.'
I know my key is 256hash compatible because I've used the command:
openssl x509 -in C:\cert.pfx -text -noout
And the Signature Algorithm was sha256WithRSAEncryption.

Related

Get byte array of ECC encryption certificate

I'm using self signed certificate ECDH_secP384r1 for signing token. Here is the PowerShell that I create the certificate:
$Cert = New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname $Certname -NotAfter $ExpireDate -KeyAlgorithm ECDH_secP384r1
Now in my .net core application first I load the certificate:
private readonly string _certificateSubjectName;
public X509Certificate2 GetSigningCertificate()
{
using (var store = new X509Store(StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadOnly);
var certificates = store.Certificates.Find(X509FindType.FindBySubjectName, _certificateSubjectName, false);
return certificates[0];
}
}
And also I can Get the ECDsa private key like
ECDsa privateKey = signingCertificate.GetECDsaPrivateKey();
ECDsa publicKey = signingCertificate.GetECDsaPublicKey()
But how could I have byte array of these keys?
For Rsa I could use:
public byte[] GetPrivateKey(X509Certificate2 certificate)
{
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)certificate.PrivateKey;
MemoryStream memoryStream = new MemoryStream();
TextWriter streamWriter = new StreamWriter(memoryStream);
PemWriter pemWriter = new PemWriter(streamWriter);
AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetRsaKeyPair(rsa);
pemWriter.WriteObject(keyPair.Private);
streamWriter.Flush();
byte[] byteArray = memoryStream.GetBuffer();
return byteArray;
}
But how about ECDsa? any idea?

RSA Signing with .Net and verifying with OpenSSL command

I'm trying to sign a simple data with C# using Sha1 hash and RSA and then verify it with the OpenSSL command.
For my tests, I've taken the localhost certificate that is preexisting on Windows.
Here's the code used with C#:
static void Main(string[] args)
{
string sValTest = "sampledata";
byte[] signature = Sign(sValTest, "localhost");
string b64 = Convert.ToBase64String(signature);
bool verified = Verify(sValTest, signature, #"pathToCer");
}
static byte[] Sign(string text, string certSubject)
{
X509Store my = new X509Store(StoreName.My, StoreLocation.LocalMachine);
my.Open(OpenFlags.ReadOnly);
RSACryptoServiceProvider csp = null;
foreach (X509Certificate2 cert in my.Certificates)
{
if (cert.Subject.Contains(certSubject))
{
csp = (RSACryptoServiceProvider)cert.PrivateKey;
break;
}
}
SHA1Managed sha1 = new SHA1Managed();
byte[] data = Enconding.ASCII.GetBytes(text);
byte[] hash = sha1.ComputeHash(data);
return csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
}
static bool Verify(string text, byte[] signature, string certPath)
{
X509Certificate2 cert = new X509Certificate2(certPath);
RSACryptoServiceProvider csp = RSACryptoServiceProvider)cert.PublicKey.Key;
SHA1Managed sha1 = new SHA1Managed();
byte[] data = Encoding.ASCII.GetBytes(text);
byte[] hash = sha1.ComputeHash(data);
return csp.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), signature);
}
(In that example, verified yields true)
I sent the public part of the PFX to my linux computer as well as the signature as base 64.
I then did the following:
base64 --decode sig.b64 > sig
openssl x509 -inform der -in localhost.cer -out localhost.pem
openssl x509 -pubkey -noout -in localhost.pem > localhost.pub.pem
echo -n "sampledata" | openssl dgst -verify localhost.pub.pem -signature sig
Which ends with a Verification Failure.
I checked the serial number of the certificates on both sides and it matches.
Just in case, I also checked the md5 of the signature in both stations, all clear.
Any pointers on where is the obvious fail?
You are missing the hash algorithm identifier into the openssl dgst command, which defaults to MD5.
Your correct last line is
echo -n "sampledata" | openssl dgst -verify localhost.pub.pem -signature sig -sha1

What is the difference between signing by OpenSSL and Microsoft Cryptography libraries?

I wrote two methods for signing using RSA and SHA256, the first one with OpenSSL library and the second one with Microsoft Cryptography library.
OpenSSL implementation:
private string PasswordHandler(bool verify, object userdata)
{
return userdata.ToString();
}
private string Sign(string signParams)
{
var privateCertPath = HttpContext.Current.Server.MapPath(#"~\certificate.pem");
string privateKey;
using (StreamReader sr = new StreamReader(privateCertPath))
{
privateKey = sr.ReadToEnd();
}
OpenSSL.Crypto.RSA rsa = OpenSSL.Crypto.RSA.FromPrivateKey(new BIO(privateKey), PasswordHandler, _password);
//hash method
MessageDigest md = MessageDigest.SHA1;
BIO b = new BIO(signParams);
CryptoKey ck = new CryptoKey(rsa);
byte[] res1 = MessageDigestContext.Sign(md, b, ck);
return Uri.EscapeDataString(System.Convert.ToBase64String(res1));
}
Cryptography implementation:
private string Sign(string data)
{
var privateCertPath = HttpContext.Current.Server.MapPath(#"~\certificate.pfx");
X509Certificate2 privateCert = new X509Certificate2(privateCertPath, _password, X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider privateKey = (RSACryptoServiceProvider)privateCert.PrivateKey;
RSACryptoServiceProvider privateKey1 = new RSACryptoServiceProvider();
privateKey1.ImportParameters(privateKey.ExportParameters(true));
// Get the bytes to be signed from the string
var bytes = System.Text.Encoding.UTF8.GetBytes(data);
//const string sha256Oid = "2.16.840.1.101.3.4.2.1";
//HashAlgorithm algorithm = new SHA256CryptoServiceProvider();
//byte[] hashBytes = algorithm.ComputeHash(bytes);
//byte[] signature = privateKey1.SignHash(hashBytes, sha256Oid);
byte[] signature = privateKey1.SignData(bytes, "SHA256");
// Base 64 encode the sig so its 8-bit clean
return Convert.ToBase64String(signature);
}
Signing with OpenSSL works, generates valid digital signature but signing with Cryptography lib generates invalid signature so my question is what I implemented wrong?
I tried to use different encoding but it did not help. Certificates are generated correctly.
It might by also useful to tell basic info about the .pem certificate:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC

.NET MD5 signature does not match OpenSSL MD5 signature

I have to sign messages for a web service. The service uses OpenSSL MD5 signatures. I generate the OpenSSL one from a PEM private key file:
openssl.exe dgst -md5 -hex -out signature.txt -sign privKey.pem textToSign.txt
Which generates the following signature:
7078388bd081d4b805feb020ab47352320919e4638e36b59d66a684c9bb12a745aaf172e4da2686c3e3750bf627c980a19700f6d8bbd0b62d8714a965a34be2e9f4147ac054c4af1050cdcebd9b475f0ef28e520681b0d67104b8e633ee592bb3ec2c517fb8cf7b13bd86424f00c89518e063d55e7922adab7cf607c85920862
I want to implement this in my code. My actual code looks like (the private key in the pfx file is the same as the one in the pem file):
private static string Sign(string stringToSign)
{
X509Certificate2 cert = new X509Certificate2("keys.pfx", "", X509KeyStorageFlags.Exportable);
byte[] data = Encoding.UTF8.GetBytes(stringToSign);
byte[] signedData;
using (MD5 hasher = MD5.Create())
using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey)
{
signedData = rsa.SignData(data, hasher);
}
return Convert.ToBase64String(signedData);
}
This code produces the following signature...
cHg4i9CB1LgF/rAgq0c1IyCRnkY442tZ1mpoTJuxKnRarxcuTaJobD43UL9ifJgKGXAPbYu9C2LYcUqWWjS+Lp9BR6wFTErxBQzc69m0dfDvKOUgaBsNZxBLjmM+5ZK7PsLFF/uM97E72GQk8AyJUY4GPVXnkirat89gfIWSCGI=
...which does not match the OpenSLL one.
What should I do to match the OpenSSL one? Also I tried to use OpenSSL.NET, but I did not find any resources/tutoral for signing.
SOLVED
The correct code is:
private static string Sign(string stringToSign)
{
X509Certificate2 cert = new X509Certificate2("#02299991.pfx", "#02299991", X509KeyStorageFlags.Exportable);
byte[] data = Encoding.UTF8.GetBytes(stringToSign);
byte[] signedData;
using (MD5 hasher = MD5.Create())
using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey)
{
signedData = rsa.SignData(data, hasher);
}
return 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);
return hex.ToString();
}
Your OpenSSL output is a hex string:
openssl.exe dgst -md5 -hex -out signature.txt -sign privKey.pem textToSign.txt
^
While your C# output is in Base64:
return Convert.ToBase64String(signedData);
^
Convert your byte[] output to a hex string:
How do you convert Byte Array to Hexadecimal String, and vice versa?

Digital signature with itextsharp

Can someone explain the following code..
What will that return statement do.
public byte[] sign(string text)
{
string password = "1234";
X509Certificate2 cert = new X509Certificate2("c:\\certificate.pfx", password);
RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey;
SHA1Managed sha1 = new SHA1Managed();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(text);
byte[] hash = sha1.ComputeHash(data);
return crypt.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
}
public byte[] sign(string text)
{
//Password for the PFX certificate
string password = "1234";
//Importing the PFX certificate that contains the private key which will be used for creating the digital signature
X509Certificate2 cert = new X509Certificate2("c:\\certificate.pfx", password);
//declaring RSA cryptographic service provider
RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey;
//cryptographic hash of type SHA1
SHA1Managed sha1 = new SHA1Managed();
//encoding the data to be signed
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(text);
//generate Hash
byte[] hash = sha1.ComputeHash(data);
//sign Hash
return crypt.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
}
The SignHash(byte[], string) method will compute the signature for the hash value you pass as the first argument based on the private key read from your certificate. See here:
RSACryptoServiceProvider.SignHash Method
The result of this (which is subsequently returned) will be a byte[] containing the signature which you can send along with your data so that the signature can be verified by someone else using your public key.

Categories