# Fun with bignum: how RSA encryption works

#### 2026-02-26

Primitive types such as `int` or `double` store numbers in exactly 4 or 8 bytes, with finite precision. This suffices for most applications, but cryptography requires arithmetic on very large numbers, without loss of precision. Therefore OpenSSL uses a bignum data type which holds arbitrary sized integers and implements all basic arithmetic and comparison operators such as `+`, `-`, `*`, `^`, `%%`, `%/%`, `==`, `!=`, `<`, `<=`, `>` and `>=`.

One special case, the modular exponent `a^b %% m` can be calculated using `bignum_mod_exp`, even when `b` is too large for calculating `a^b`.

    # create a bignum
    y <- bignum("123456789123456789")
    z <- bignum("D41D8CD98F00B204E9800998ECF8427E", hex = TRUE)

    # size grows
    print(y * z)

    ## [b] 34808613111804879213872650915812112647840354642904626774

    # Basic arithmetic
    div <- z %/% y
    mod <- z %% y
    z2 <- div * y + mod
    stopifnot(z2 == z)
    stopifnot(div < z)

RSA involves a public key and a private key. The public key should be known by everyone and is used for encrypting messages. Messages encrypted with the public key can only be decrypted in a reasonable amount of time using the private key. In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers.

### RSA key generation

An RSA key-pair is generated as follows (adapted from wikipedia):

- Choose two distinct prime numbers \(p\) and \(q\). Keep these secret.
- Compute the product \(n = p*q\). This \(n\) value is public and used as the modulus.
- Compute \(\phi(n) = (p − 1)(q − 1)\).
- Choose an integer \(e\) smaller than \(\phi(n)\) such that \(e\) and \(\phi(n)\) are coprime. OpenSSL always uses \(65537\).
- Compute a value for \(d\) such that \((d * e)\pmod{\phi(n)} = 1\).

OpenSSL has a key generator that does these things for us.

    (key <- rsa_keygen(512))

    ## [512-bit rsa private key]
    ## md5: 97c92ed63d4b0db9c9a5a8a2e9b829e9
    ## sha256: 8a981deb8eb1e63363b146ea874c3df1820cff86e5f9602e0572f873181c4c46

    (pubkey <- key$pubkey)

    ## [512-bit rsa public key]
    ## md5: 97c92ed63d4b0db9c9a5a8a2e9b829e9
    ## sha256: 8a981deb8eb1e63363b146ea874c3df1820cff86e5f9602e0572f873181c4c46

Usually we would use `rsa_encrypt` and `rsa_decrypt` to perform the encryption:

    msg <- charToRaw("hello world")
    ciphertext <- rsa_encrypt(msg, pubkey)
    rawToChar(rsa_decrypt(ciphertext, key))

    ## [1] "hello world"

Let’s look at how this works under the hood.

### How RSA encryption works

The `data` field of the private key extracts the underlying bignum integers:

    key$data

    ## $e
    ## [b] 65537
    ## $n
    ## [b] 9034715522603516380976228920972904919040188792142326463806332262548921171552447299859147850601664041963908360527425870201650196050885810141350651358431017
    ## $p
    ## [b] 102812285379678032899619966910093275586536754247565567026608017153556384136061
    ## $q
    ## [b] 87875835939634955608718370152323410621836484973208932513904138031976779207197
    ## $d
    ## [b] 4543343752055237337196294864710072591311892549592362379536220644472966340400807500389758687674394197543829619469536369491902077392430031672643602855722833
    ## $dp
    ## [b] 30716458607108898548523108352527981689494326077900022924164746263433388793873
    ## $dq
    ## [b] 54397283797473953859500672272298220021015370842090928531311731934926315755325
    ## $qi
    ## [b] 17785266509405250172158132874139817938805532803081547369387607757600980297

You can verify that the equations above hold for this key. The public key is simply a subset of the key which only contains \(n\) and \(e\):

    pubkey$data

    ## $e
    ## [b] 65537
    ## $n
    ## [b] 9034715522603516380976228920972904919040188792142326463806332262548921171552447299859147850601664041963908360527425870201650196050885810141350651358431017

In order to encrypt a message into ciphertext we have to treat the message data as an integer. The message cannot be larger than the key size. For example convert the text `hello world` into an integer:

    m <- bignum(charToRaw("hello world"))
    print(m)

    ## [b] 126207244316550804821666916

To encrypt this message \(m\) into ciphertext \(c\) we calculate \(c = m^e\pmod n\). Using the public key from above:

    e <- pubkey$data$e
    n <- pubkey$data$n
    c <- (m ^ e) %% n
    print(c)

    ## [b] 4648071729271841031294110790369316396129682984094567571248293586759900637050587581770470217906593619348108189959520026183666653599495753268267404770957657

This number represents our encrypted message! It is usually exchanged using base64 notation for human readability:

    base64_encode(c)

    ## [1] "WL9NJX/xsg3ES3qUa1DaPFjeMvaauMQDWFT41y3sTUBau3bBlwKzi/eDA93xg6pF4t8Dh3W0mdIPrDZhWbRZWQ=="

The ciphertext can be decrypted using \(d\) from the corresponding private key via \(m = c^d \pmod{n}\). Note that `c^d` is too large to calculate directly so we need to use `bignum_mod_exp` instead.

    d <- key$data$d
    out <- bignum_mod_exp(c, d, n)
    rawToChar(out)

    ## [1] "hello world"

The only difference with the actual `rsa_encrypt` and `rsa_decrypt` functions is that these add some additional padding to the data.