简体   繁体   中英

Encrypt String with SHA256 Algorithm and sign with private key and base64 string resulted in c#

I want to do a process of encryption for my application according to below list: 1- First I want encrypt my simple string with sha256 algorithm 2- second I want to sign Encrypted text with private key that I have 3- Third I want to convert the resulted byte array to base64 for passing as a header in my api request

below is my code that doesnt create a correct signed string:

byte[] signedBytes, originalData;
string temp_inBase64;
using (SHA256 hash = SHA256Managed.Create())
{
Encoding enc = Encoding.UTF8;
originalData = hash.ComputeHash(enc.GetBytes(message));
}
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString("xxxxxx"); //Final
try
{
signedBytes = rsa.SignData(originalData, new SHA256CryptoServiceProvider());
temp_inBase64 = Convert.ToBase64String(signedBytes);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
return temp_inBase64;

the original data is my message that I passed to our function,, But the verified resulted not correct,,

can anyone help me?

I think you're misunderstanding what a hash is - that part isn't encryption. A hash is a way to calculate a 'fingerprint' for a block of data. Changing any byte of the data (even a little bit) will completely change the 'fingerprint'. The rsa signing process itself calculates the hash of your data, and signs the hash. This allows the receiver to ensure that both the data hasn't been tampered with (they can calculate the hash and make sure it matches) and that it was sent by you (the hash is encrypted with your key).

So rather than calculate the hash yourself and pass it into the RSA signing function, you need to pass your data to be signed, and ensure that the sign function is configured to use SHA256.

The process of signing algorithm is true but if the resulted string doesnt work I suggested you to try this function for hashing sha256 and after that signing the byte array converted to base 64 with private key,,

below is the sample code for your reuest:

 // Generate a new RSA key pair
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            // Get the private key
            rsa.FromXmlString("xxxxx"); //Final ;
            

            // The string to sign
            string originalString = message;

            // Sign the string
            byte[] signedData = rsa.SignData(System.Text.Encoding.UTF8.GetBytes(originalString), CryptoConfig.MapNameToOID("SHA256"));



            // Verify the signature
            bool signatureVerified = rsa.VerifyData(System.Text.Encoding.UTF8.GetBytes(originalString), CryptoConfig.MapNameToOID("SHA256"), signedData);
            var temp_inBase64 = Convert.ToBase64String(signedData);

as you can see at the end of the code I return base 64 format of signed data and dont use verified signed data,, The resulted message worked For me but you can test verified sign data,,

best regard

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM