Signing data with private key in c#

前端 未结 1 454
迷失自我
迷失自我 2020-12-01 17:27

I need to sign some data with one private key using Algorithm SHA1RSA ,Rsa Key length 2048 with 64 base encoding.My code is like this

       string sPayload          


        
相关标签:
1条回答
  • You have computed the SHA-1 hash of sPayload, not the RSA-SHA1 signature.

    If you have an X509Certificate2:

    using (RSA rsa = cert.GetRSAPrivateKey())
    {
        return rsa.SignData(sPayload, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
    }
    

    If you already have a raw RSA key then just leave off the using statement.

    If you have to compute the hash of sPayload for some other reason you can do it like

    byte[] hash;
    byte[] signature;
    
    using (HashAlgorithm hasher = SHA1.Create())
    using (RSA rsa = cert.GetRSAPrivateKey())
    {
        hash = hasher.ComputeHash(sPayload);
        signature = rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
    }
    

    SignHash still requires the HashAlgorithmName value because the algorithm identifier is embedded within the signature.

    0 讨论(0)
提交回复
热议问题