-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathexample.R
49 lines (38 loc) · 978 Bytes
/
example.R
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
normalise<-function(text){
return(tolower(gsub(" ", "", text)))
}
lookupindex<-function(normalisedtext){
letterslist <- strsplit(normalisedtext, "")[[1]]
return(match(letterslist, letters) - 1)
}
gcd <- function(x, y) {
r <- x %% y
return(ifelse(r, gcd(y, r), y))
}
mmi <- function(a, m) {
a <- a %% m
for (x in 1:m) {
if ((a * x) %% m == 1) {
return(x)
}
}
return(1)
}
encrypt <- function(plaintext, a, b) {
m <- 26
if (gcd(a, m) != 1) {
stop(paste('a=',a,' and m=',m,'is coprime'))
}
normalisedplaintext<-normalise(plaintext)
x<-lookupindex(normalisedplaintext)
return(paste(letters[ ((a * x + b) %% m) + 1], collapse = ""))
}
decrypt <- function(encryption, a, b) {
m <- 26
if (gcd(a, m) != 1) {
stop("a and 26 must be co-prime")
}
normalisedencryption<-normalise(encryption)
y<-lookupindex(normalisedencryption)
return(paste(letters[((mmi(a, m) * (y - b)) %% m) + 1], collapse = ""))
}