1## ----setup, include=FALSE-----------------------------------------------------
2library(openssl)
3knitr::opts_chunk$set(echo = TRUE)
4
5## -----------------------------------------------------------------------------
6# create a bignum
7y <- bignum("123456789123456789")
8z <- bignum("D41D8CD98F00B204E9800998ECF8427E", hex = TRUE)
9
10# size grows
11print(y * z)
12
13# Basic arithmetic
14div <- z %/% y
15mod <- z %% y
16z2 <- div * y + mod
17stopifnot(z2 == z)
18stopifnot(div < z)
19
20## -----------------------------------------------------------------------------
21(key <- rsa_keygen(512))
22(pubkey <- key$pubkey)
23
24## -----------------------------------------------------------------------------
25msg <- charToRaw("hello world")
26ciphertext <- rsa_encrypt(msg, pubkey)
27rawToChar(rsa_decrypt(ciphertext, key))
28
29## -----------------------------------------------------------------------------
30key$data
31
32## -----------------------------------------------------------------------------
33pubkey$data
34
35## -----------------------------------------------------------------------------
36m <- bignum(charToRaw("hello world"))
37print(m)
38
39## -----------------------------------------------------------------------------
40e <- pubkey$data$e
41n <- pubkey$data$n
42c <- (m ^ e) %% n
43print(c)
44
45## -----------------------------------------------------------------------------
46base64_encode(c)
47
48## -----------------------------------------------------------------------------
49d <- key$data$d
50out <- bignum_mod_exp(c, d, n)
51rawToChar(out)
52
53