File: bignum.R

package info (click to toggle)
r-cran-openssl 2.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,336 kB
  • sloc: ansic: 3,158; sh: 20; makefile: 5
file content (52 lines) | stat: -rw-r--r-- 1,489 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
## ----setup, include=FALSE-----------------------------------------------------
library(openssl)
knitr::opts_chunk$set(echo = TRUE)

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

# size grows
print(y * z)

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

## -----------------------------------------------------------------------------
(key <- rsa_keygen(512))
(pubkey <- key$pubkey)

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

## -----------------------------------------------------------------------------
key$data

## -----------------------------------------------------------------------------
pubkey$data

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

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

## -----------------------------------------------------------------------------
base64_encode(c)

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