Skip to content

Instantly share code, notes, and snippets.

@slowkow
Created October 21, 2020 18:48
Show Gist options
  • Save slowkow/2e42a4de6a2da245e4044e1b5851d39e to your computer and use it in GitHub Desktop.
Save slowkow/2e42a4de6a2da245e4044e1b5851d39e to your computer and use it in GitHub Desktop.
A toy example of RSA encryption and decryption
#!/usr/bin/env Rscript
# rsa-example.R
#
# Inspired by Alex Van de Sande
# https://twitter.com/avsa/status/1318672796415819776
last_digit <- function(x) {
x_char <- as.character(x)
as.numeric(substr(x_char, nchar(x_char), nchar(x_char)))
}
# Pick a number between 0-9 for input
input <- 2
private_key <- 7
encrypted <- input ^ 3
output <- last_digit(last_digit(encrypted) ^ private_key)
input == output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment