RC4加密功能

RC4加密功能,r,encryption,rc4-cipher,stream-cipher,R,Encryption,Rc4 Cipher,Stream Cipher,我试图创建一个R函数,该函数将采用种子和密钥长度来生成RC4密钥流 以下是我目前掌握的情况: library(numbers) library(seqinr) library(compositions) rc4_genkey <- function(seed,keylength){ keystream <- vector(mode="integer", length=keylength) # initialized S vector

我试图创建一个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"
库(数字)
图书馆(seqinr)
图书馆(作文)

rc4_genkey我认为您在这里犯了不少错误:rc4是一个对称密码,因此您的rc4函数应该获取消息和密钥(而不是密钥长度),并返回字节流


我猜您已经尝试将此函数从较低级别的零索引语言转换为R。例如,您创建的状态向量
s
(在rc4中应该以0到255开头)应该是
s,我认为您在这里犯了不少错误:rc4是一个对称密码,因此,rc4函数应该获取消息和密钥(而不是密钥长度),并返回字节流


我猜您已经尝试将此函数从较低级别的零索引语言转换为R。例如,您创建的状态向量
s
(在rc4中应该以0到255开始)应该是
s请说明所有非基
library
调用,并将代码问题修复为
object'j'not found
,以便有人可以复制您的问题。我不是
r
中的程序员,但如果我是,我希望找到缩进和间隔良好的代码。你试图在一个函数中做的太多了,把它分开!请注意,到目前为止,RC4主要具有历史意义。这是一个易于实现的密码,我想这是对流密码的一个很好的介绍。但请不要在现代协议/用例中使用它。它有偏见,一个棘手的密钥设置,不允许单独的IV…@MaartenBodewes我同意,但不幸的是,对于某些任务,如读取旧的加密pdf文件,仍然需要rc4实现。我认为较新的版本使用AES。请说明所有非基
调用,并将代码问题修复为
未找到对象“j”
,以便有人可以复制您的问题。我不是
r
中的程序员,但如果我是,我希望找到缩进和间隔良好的代码。你试图在一个函数中做的太多了,把它分开!请注意,到目前为止,RC4主要具有历史意义。这是一个易于实现的密码,我想这是对流密码的一个很好的介绍。但请不要在现代协议/用例中使用它。它有偏见,一个棘手的密钥设置,不允许单独的IV…@MaartenBodewes我同意,但不幸的是,对于某些任务,如读取旧的加密pdf文件,仍然需要rc4实现。我认为较新的使用AES
rc4 <- function(message, key)
{
  if(is.raw(message)) message <- as.integer(message)
  if(is.character(message)) message <- utf8ToInt(message)  
  key            <- utf8ToInt(key)
  key_length     <- length(key)
  message_length <- length(message)
  s              <- 0:255
  a <- b <- x <- y <- 0 

  if(key_length == 0) stop("No key given")
  if(message_length == 0) return("")

  for (i in seq_along(s))
  {
    b <- (key[a + 1] + s[i] + b) %% 256
    tmp <- s[i]
    s[i] <- s[b + 1]
    s[b + 1] <- tmp
    a <- (a + 1) %% key_length;
  }

  for (k in seq(message_length))
  {
    x1 <- x <- (x + 1) %% 256
    y1 <- y <- (s[x + 1] + y) %% 256
    tmp <- s[x1]
    s[x1] <- s[y1]
    tmp <- s[y1]
    message[k] = bitwXor(message[k], s[(s[x1 + 1] + s[y1 + 1]) %% 256]);
  }
  if(any(message < 9 | message > 127)) return(as.raw(message))
  return(intToUtf8(message))
}
encrypted_message <- rc4("hello", "world")
encrypted_message
#> [1] b7 31 74 99 98
rc4(encrypted_message, "world")
#> [1] "hello"