我正在尝试创建一个 R 函数,该函数将采用种子和密钥长度来生成 RC4 密钥流。
以下是我到目前为止所拥有的:
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"
希望得到一些帮助来解决这个问题以运行正确的 rc4 encrpytion