Digital Signature using the RSA encryption algorithm

1/12/2021 7:16:15 AM
Gravatar
Total Posts 2

Digital Signature using the RSA encryption algorithm

Good day!
Thank you for your attention, I really need help.

Before using NKryptoki, I had the following signature code:

    private static string _GenerateDigitalSignature (string body, string privateKeyText)
    {
        var hash = HashBody (body);

        byte [] signedHash;
        using (var privateKey = ImportPrivateKey (privateKeyText))
        {
            signedHash = privateKey.SignHash (hash, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
        }

        var encodedHash = Convert.ToBase64String (signedHash);

        return encodedHash;
    }

        private static byte [] HashBody (string content)
    {
        var contentBytes = Encoding.UTF8.GetBytes (content);

        using (var provider = new SHA256Managed ())
        {
            var hash = provider.ComputeHash (contentBytes);

            return hash;
        }
    }

    private static RSACryptoServiceProvider ImportPrivateKey (string pem)
    {
        StringReader sr = new StringReader (pem);
        PemReader pr = new PemReader (sr);
        AsymmetricKeyParameter Key;
        Key = (AsymmetricKeyParameter) pr.ReadObject ();
        pr.Reader.Close ();
        sr.Close ();
        var rsaParameters = DotNetUtilities.ToRSAParameters ((RsaPrivateCrtKeyParameters) Key);
        var csp = new RSACryptoServiceProvider ();
        csp.ImportParameters (rsaParameters);
        return csp;
    }

Which signature mechanism should I choose so that the results match?
Very much I ask for your advice!

1/12/2021 7:27:34 AM
Gravatar
Total Posts 2

Re: Digital Signature using the RSA encryption algorithm

I think I need to find a match for "HashAlgorithmName.SHA256 and RSASignaturePadding.Pkcs1"?