I am trying to create a R function that will take a seed,and key length to generate a RC4 keystream.
The following is what I have so far:
library(numbers)
library(seqinr)
library(compositions)
rc4_genkey <- function(seed,keylength){
keystream <- vector(mode="integer", length=keylength)
# initialized S vector
s <- vector(mode="integer", length=255)
for(i in 1:255){
s[i + 1] = i+1
}
# initialize k vector with seed
key <- utf8ToInt(seed)
n <- length(key)
k <- vector(mode="integer", length=256)
for (i in 1:255){
k[i + 1] = key[mod(i+1, n)+1]
}
# Rc4 algorithm randomize 2 with 256 iterations
for (i in 1:255){
j <- (mod(j + s[i+1] + k[i+1], 256))
swap(s[i + 1], s[j])
}
# generate keystream of keystream length
for(i in 0:length(keystream)){
i <- mod((i + 1),256)
j <- mod((j + s[i]), 256)
swap(s[i+1],s[j+1])
t <- mod((s[i] + s[j]),256)
k[i] <- s[t]
keystream[i] <- k[i]
}
}
Now every time I run the function, it keeps telling me
"s[i + 1] <- s[j + 1] : replacement has length zero"
Hoping to get a little help to fix up this to run a proper rc4 encrpytion