Public Key Hash

Kuler Master 406 Reputation points
2021-07-28T19:28:16.92+00:00

Hello,

At the moment I only have created the public key (Exported as XML) e.g.

using (var provider = new RSACryptoServiceProvider(2048))  
{                  
    publicKey = provider.ToXmlString(false);  
}  

The result is as following:

<RSAKeyValue>  
  <Modulus>1e1........xk=</Modulus>  
  <Exponent>AQAB</Exponent>  
</RSAKeyValue>  

What I need to do now is to turn it into values similar to those from the picture (Hexadecimals and Hashes)

118802-publickey.png

Thank you so much

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
Developer technologies | C#
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-07-30T02:29:05.153+00:00

    What we want to convert is the Base64 string in the Modulus node, so the first step is to extract it and then convert it.

            static void Main(string[] args)  
            {  
                using (var provider = new RSACryptoServiceProvider(2048))  
                {  
                    //  
                    var publicKey = provider.ToXmlString(false);  
                    XmlDocument xd = new XmlDocument();  
                    xd.LoadXml(publicKey);  
                    string base64PK = xd.SelectSingleNode("descendant::Modulus").InnerText;  
      
                    byte[] bytes = Convert.FromBase64String(base64PK);  
                    string hex = BitConverter.ToString(bytes);  
      
                    string hash = ComputeSha256Hash(base64PK);  
                    Console.WriteLine();  
                }  
      
      
                 
            }  
            static string ComputeSha256Hash(string rawData)  
            {  
                // Create a SHA256     
                using (SHA256 sha256Hash = SHA256.Create())  
                {  
                    // ComputeHash - returns byte array    
                    byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));  
      
                    // Convert byte array to a string     
                    StringBuilder builder = new StringBuilder();  
                    for (int i = 0; i < bytes.Length; i++)  
                    {  
                        builder.Append(bytes[i].ToString("X2"));  
                    }  
                    return builder.ToString();  
                }  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.