Question

I am using the below code to generate public and private keys:

using (var rsaProvider = new RSACryptoServiceProvider(cspParams))
{
    try
    {
        // Export public key
        publicKey = rsaProvider.ToXmlString(false);
        // Write public key to file
        publicKeyFile = File.CreateText(publicKeyFileName);
        publicKeyFile.Write(publicKey);

        // Export private/public key pair 
        //privateKey = rsaProvider.ToXmlString(true);
        privateKey = rsaProvider.ToXmlString(true);

        // Write private/public key pair to file
        privateKeyFile = File.CreateText(privateKeyFileName);
        privateKeyFile.Write(privateKey);
    }
    catch (Exception ex)
    {
    }
    finally
    {
        rsaProvider.PersistKeyInCsp = false; // dont srotre in windows key container
    }
}

The public key that is generated contains a key and an expontent:

<RSAKeyValue
    <Modulus>rx39KaNggx7IFO/CT2XTi/aHdgEnZq0f6E2lKFVbObZ9ws0wCsRftfomOOy2z8/jFXn7NLOuiC+MvO6AsSb8nSCO0PY08TklmFBzsFxsq7CLWubL8TDV0wq7AtVpovdJkywc+O4wnz8ZTn38nAgNnQ6Ga+hIJspFCYZS01jkx30=    
    </Modulus>
    <Exponent>AQAB</Exponent>
</RSAKeyValue>

But when I look at the public key of a certificate from Internet Explorer it shows:

RSA (2048 Bits).

30 82 01 0a 02 82 01 01 00 bf 61 0a 29 10 1f 5e
fe 34 37 51 08 f8 1e fb 22 ed 61 be 0b 0d 70 4c
50 63 26 75 15 b9 41 88 97 b6 f0 a0 15 bb 08 60
e0 42 e8 05 29 10 87 36 8a 28 65 a8 ef 31 07 74
6d 36 97 2f 28 46 66 04 c7 2a 79 26 7a 99 d5 8e
c3 6d 4f a0 5e ad bc 3d 91 c2 59 7b 5e 36 6c c0
53 cf 00 08 32 3e 10 64 58 10 13 69 c7 0c ee 9c
42 51 00 f9 05 44 ee 24 ce 7a 1f ed 8c 11 bd 12
a8 f3 15 f4 1c 7a 31 69 01 1b a7 e6 5d c0 9a 6c
7e 09 9e e7 52 44 4a 10 3a 23 e4 9b b6 03 af a8
9c b4 5b 9f d4 4b ad 92 8c ce b5 11 2a aa 37 18
8d b4 c2 b8 d8 5c 06 8c f8 ff 23 bd 35 5e d4 7c
3e 7e 83 0e 91 96 05 98 c3 b2 1f e3 c8 65 eb a9
7b 5d a0 2c cc fc 3c d9 6d ed cc fa 4b 43 8c c9
d4 b8 a5 61 1c b2 40 b6 28 12 df b9 f8 5f fe d3
b2 c9 ef 3d b4 1e 4b 7c 1c 4c 99 36 9e 3d eb ec
a7 68 5e 1d df 67 6e 5e fb 02 03 01 00 01

Is this the same type of key containing an exponent or is it encoded, or simply a different type of encryption?

Was it helpful?

Solution

It's just a different encoding of the same information. One is XML, using base-64 encoding for the modulus and exponent numbers. The other is a hexadecimal encoding of the information using the Distinguished Encoding Rules (DER) common with PKI protocols. It's a "SEQUENCE" containing two "INTEGER" elements: the modulus and the exponent, as defined in the PKCS #1 standard.

30 - SEQUENCE tag
    82 01 0a - length is 266 bytes
  02 - INTEGER tag (modulus)
      82 01 01 - length is 257 bytes
    00 bf 61 0a 29 10 1f 5e
    fe 34 37 51 08 f8 1e fb 22 ed 61 be 0b 0d 70 4c
    50 63 26 75 15 b9 41 88 97 b6 f0 a0 15 bb 08 60
    e0 42 e8 05 29 10 87 36 8a 28 65 a8 ef 31 07 74
    6d 36 97 2f 28 46 66 04 c7 2a 79 26 7a 99 d5 8e
    c3 6d 4f a0 5e ad bc 3d 91 c2 59 7b 5e 36 6c c0
    53 cf 00 08 32 3e 10 64 58 10 13 69 c7 0c ee 9c
    42 51 00 f9 05 44 ee 24 ce 7a 1f ed 8c 11 bd 12
    a8 f3 15 f4 1c 7a 31 69 01 1b a7 e6 5d c0 9a 6c
    7e 09 9e e7 52 44 4a 10 3a 23 e4 9b b6 03 af a8
    9c b4 5b 9f d4 4b ad 92 8c ce b5 11 2a aa 37 18
    8d b4 c2 b8 d8 5c 06 8c f8 ff 23 bd 35 5e d4 7c
    3e 7e 83 0e 91 96 05 98 c3 b2 1f e3 c8 65 eb a9
    7b 5d a0 2c cc fc 3c d9 6d ed cc fa 4b 43 8c c9
    d4 b8 a5 61 1c b2 40 b6 28 12 df b9 f8 5f fe d3
    b2 c9 ef 3d b4 1e 4b 7c 1c 4c 99 36 9e 3d eb ec
    a7 68 5e 1d df 67 6e 5e fb
  02 - INTEGER tag (public exponent)
      03 - length is 3 bytes
    01 00 01 (65,537 [F4])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top