sign a request using SHA1 and private.key

MaxPlatinum

Member
Joined
Aug 4, 2023
Messages
18
Programming Experience
1-3
Hello, I have private key like:
C#:
string privateKey = @"-----BEGIN PRIVATE KEY-----
MIICd...............................
hqhPg==
-----END PRIVATE KEY-----";
And request like:
C#:
string request = "<?xml version=\"1.0\" encoding=\"windows-1251\"?><request>...............</request>";
.

I need to write code in C# based on Openssl commands:
Rich (BB code):
openssl dgst –sha1 -out request.sign –sign private.key request.txt
openssl base64 -in request.sign –out request.enc

How to code these commands in C#? I tried a lot of exmaples and didnt get result. Could anyone code it please? How to read private key?
 
Found this online converter: RSA PEM TO XML - RSAKeyConverter
Then this works in .Net Framework:
C#:
using (var rsa = RSA.Create())
{
    rsa.FromXmlString(File.ReadAllText(@"X:\privkey.xml"));
    var data = System.Text.Encoding.UTF8.GetBytes(requestData);
    var signed = rsa.SignData(data, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
    var b64 = Convert.ToBase64String(signed);
    
}
 
Found this online converter: RSA PEM TO XML - RSAKeyConverter
Then this works in .Net Framework:
C#:
using (var rsa = RSA.Create())
{
    rsa.FromXmlString(File.ReadAllText(@"X:\privkey.xml"));
    var data = System.Text.Encoding.UTF8.GetBytes(requestData);
    var signed = rsa.SignData(data, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
    var b64 = Convert.ToBase64String(signed);
   
}

Thanks, I did it, but the result is different as generated by openssl:
request.enc, generated by openssl:

Rich (BB code):
Pya7wHvOpzG..............................ieTzzUGHck=

this generated by code you gave:

C#:
3Ucn7Yg4NwoA6LRJjLg7e..............................ZWPQARI9/g1gRng=
 
Ok, I get the same results as openssl.
 
What do you mean? Xml format is just a way for .Net RSA to read it, the key is not changed of course.
 
What do you mean? Xml format is just a way for .Net RSA to read it, the key is not changed of course.

You know the situation is:
1) I did:
openssl genrsa -out private.key 1024 openssl rsa -in private.key –pubout –out public.key
2) I registered public key in the server
3) I have to do:
openssl dgst –sha1 -out request.sign –sign private.key request.txt openssl base64 -in request.sign –out request.enc
When I use the key which generated from your code, I think maybe it will be conflict with generated public key in openssl. Or there will not be any problem?
 
My code doesn't generate any key, it signs with same private key used with openssl. I get same bytes in variable signed as in request.sign file from openssl, and same base64 string.
 
1691423158657.png


Why request is not getting value inside the file? Am I giving wrong path? even I did var request = "request.txt" is not getting value inside the file
My code doesn't generate any key, it signs with same private key used with openssl. I get same bytes in variable signed as in request.sign file from openssl, and same base64 string.
 
Use File.ReadAllText if you want to read the text content of file.
 
Use File.ReadAllText if you want to read the text content of file.

Sorry for bothering you a lot. Am I converting Xml correct:

C#:
static void Main(string[] args)
        {
            var request = @"<?xml version=""1.0"" encoding=""windows - 1251""?><request><agents><getBalance/></agents></request>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(request);
            using (var rsa = RSA.Create())
            {
                rsa.FromXmlString(File.ReadAllText(@"privkey.xml"));
                var data = System.Text.Encoding.UTF8.GetBytes("how about here");    //////////////////////////////////////////////////////////////
                var signed = rsa.SignData(data, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
                var b64 = Convert.ToBase64String(signed);
                Console.WriteLine(b64);
                Console.ReadLine();
            }
        }
 
Now that you have the text you want to sign in request variable it is GetBytes(request) as before.

In the text the xml encoding should be "windows-1251", not "windows - 1251"
 
Also note that everything discussed so far has been signing the XML file as if it were any other kind of file. There is a separate standard for correctly signing XML files:
 
Now that you have the text you want to sign in request variable it is GetBytes(request) as before.

In the text the xml encoding should be "windows-1251", not "windows - 1251"

thank you so much! I appriciate a lot. Everything you said perfect and it works.
 
Back
Top Bottom