【问题标题】:RC4 Encryption FunctionRC4加密功能
【发布时间】:2020-03-06 08:48:06
【问题描述】:

我正在尝试创建一个 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 加密

【问题讨论】:

  • 请将所有非基本的library 调用声明为object 'j' not found 并修复代码问题,以便有人可以复制您的问题。
  • 我不是r 的程序员,但如果我是,我希望能找到缩进和间隔良好的代码。你试图在一个函数中做太多事情,把它分开!
  • 请注意,目前 RC4 主要具有历史意义。我猜这是一个易于实现的密码,这使它成为流密码的一个很好的介绍。但请不要在现代协议/用例中使用它。它有偏见、棘手的密钥设置,并且不允许单独的 IV...
  • @MaartenBodewes 我同意,但不幸的是,对于某些任务,例如读取旧的加密 pdf 文件,仍然需要 rc4 实现。我认为较新的使用 AES

标签: r encryption rc4-cipher stream-cipher


【解决方案1】:

我认为您在这里犯了很多错误:rc4 是对称密码,因此您的 rc4 函数应该接受消息和密钥(而不是密钥长度),并返回字节流。

我猜您已经尝试将此函数从较低级别的零索引语言翻译成 R。例如,您创建的状态向量 s(在 rc4 中应该从 0 到 255 开始) ) 应该只是s &lt;- 0:255,而不是用循环编写。

我之前写过rc4的C++实现,这里给大家翻译成R。

如果结果中有非 Ascii 元素,此函数将返回原始向量,否则返回字符串,因此大多数您的加密消息将是原始格式且未加密message 将是一个字符串。修改它以仅接受和返回原始向量很容易。

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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多