Created
October 21, 2020 18:48
-
-
Save slowkow/2e42a4de6a2da245e4044e1b5851d39e to your computer and use it in GitHub Desktop.
A toy example of RSA encryption and decryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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