Why do RSA keys look like they do?

112 views Asked by At

Im trying to get a better understanding on RSA encryption so i have my program display the key it generates, but according to my understanding of RSA encryption, the keys should be only numbers, but my program has a long string of numbers, characters, and letters.

this is my code:

using System;
using System.Security.Cryptography;
using System.Text;

class Program
{
    static void Main()
    {
        // Get user input for plaintext
        Console.WriteLine("Enter the plaintext:");
        string plaintext = Console.ReadLine();

        // Generate RSA key pair (for simplicity, we use a fixed key size of 2048 bits)
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
        {
            // Display the public key
            Console.WriteLine("Public Key:");
            Console.WriteLine(rsa.ToXmlString(false));

            // Encrypt the plaintext
            byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext);
            byte[] encryptedBytes = rsa.Encrypt(plaintextBytes, fOAEP: false);

            // Display the encrypted string
            string encryptedString = Convert.ToBase64String(encryptedBytes);
            Console.WriteLine("Encrypted Text:");
            Console.WriteLine(encryptedString);
        }

        Console.ReadKey();
    }
}

when my program displays the public key it looks like this

<RSAKeyValue>
<Modulus>m/DXR9Ld2vAAIS3sATZGx2z4lsZeeImu7qQzAW6j+EGIcU6ToGBOVK3kCYcbv+o884HT/hDj9M/FV9Tc/apaFREqucSw973pJyNXnp2bvO8DwO9XohBPX5lWognAWr4KDybMn9oBuqR4fAYck2ym1uYtXxGetSgf3+qgX9RcbZGi7ifafJyw1hGNLzpA6d5pZkyXUvrpDz4YLT5vbJ8xoFEFfxYV6zA5EZcEi/9w8IJaU/ypFpdmZhMtmDSXKSBJ9MVVridnAjwahwWuZNCp9rRsbsTiGEvPPvbnW1WXrbOxT41IrDuGr/gNX2GEkAv+SNVXUN8z2LcHtIHRTj7/GQ==</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>

if the keys are generated purely with math and numbers, i dont understand how or why the key looks like it does? my only idea would be the line Console.WriteLine(rsa.ToXmlString(false)); but what does that line mean exactly?

1

There are 1 answers

6
AKX On

As documented, RSA.ToXmlString returns an XML representation of the keypair (either just the public part, by default, or both the public and private parts).

The cryptic-looking text is Base64 encoded binary data; AQAB is the encoding of the bytes '01 00 01', which in turn, when interpreted as a big-endian integer, encode the exponent 65537:

$ python3
>>> import base64
>>> int.from_bytes(base64.b64decode(b"AQAB"), "big")
65537

The same idea stands for Modulus, which is a pretty big number (the decimal form is 616 digits long).

As for what these numbers mean and how they're used, you'll have to refer to Wikipedia.