Date: 2026-04-14

# Fun with bignum: how RSA encryption works

#### 2026-04-14

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: 76164ffc9587a39ddaf0ff499c3b9d80
    ## sha256: d10d20b6772f5da8bcbab00b4ec2f3c7d6bea1008afd5f71f67d22bbd43217ff

    (pubkey <- key$pubkey)

    ## [512-bit rsa public key]
    ## md5: 76164ffc9587a39ddaf0ff499c3b9d80
    ## sha256: d10d20b6772f5da8bcbab00b4ec2f3c7d6bea1008afd5f71f67d22bbd43217ff

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] 9977300712979430664406832770926002223320237568914672797010968692801603820886665417599500699218859742728822722809164150661468128132808495113592539175275527
    ## $p
    ## [b] 102586629937514287341520456750277185906789563455867699320370041509030069009833
    ## $q
    ## [b] 97257320169856672613909275119928398319365860671303049189432174433930832783919
    ## $d
    ## [b] 3167641117764972669397637509569515361713599693248204783365232244245586015551286894287546378093031753673918412894994962645579646616340432169940564492368209
    ## $dp
    ## [b] 85101962400061633274279302869254310789240096379223327417345286581952755876185
    ## $dq
    ## [b] 92201310742835269687385496180800942789297662747883767126042098319729658679293
    ## $qi
    ## [b] 60732755172429905541330682986512295649390492217144841370299801700386293944633

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] 9977300712979430664406832770926002223320237568914672797010968692801603820886665417599500699218859742728822722809164150661468128132808495113592539175275527

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] 2214422790810096436289667762049626933304400635227948894637972545246770884131797462755799706102402467469487822473605665054676702582356649235639414960772697

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

    base64_encode(c)

    ## [1] "KkffhZxt8DldAFL+VkdjHgWfrcEXCB2ZdCldm5qYuvc6WLLrkoI9fKzFB9JwbS72K9j3n3zF1be//Zxm0NVWWQ=="

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.