Ruby错误:在`进程中的块';中:未定义的方法`^';至于;“4”字:字符串(NoMethodError)

Ruby错误:在`进程中的块';中:未定义的方法`^';至于;“4”字:字符串(NoMethodError),ruby,rc4-cipher,Ruby,Rc4 Cipher,试图运行一个rc4算法但它不识别XOR方法?还是发生了什么事?当它到达def进程(文本)时,我得到了错误 错误: rc4.rb:26:inblock-in-process:“4”的未定义方法:字符串(NoMethodError) 从rc4.rb:26:in到' 来自rc4.rb:26:inprocess' 从rc4.rb:21:inencrypt' 来自rc4.rb:48:in' 代码: class Rc4 def initialize(str) @q1, @q2 = 0, 0

试图运行一个rc4算法但它不识别XOR方法?还是发生了什么事?当它到达def进程(文本)时,我得到了错误

错误:

rc4.rb:26:in
block-in-process:“4”的未定义方法:字符串(NoMethodError)
从rc4.rb:26:in
到'
来自rc4.rb:26:in
process' 从rc4.rb:21:in
encrypt'
来自rc4.rb:48:in
'

代码:

class Rc4

def initialize(str)
    @q1, @q2 = 0, 0
    @key = []
    str.each_byte {|elem| @key << elem} while @key.size < 256
    @key.slice!(256..@key.size-1) if @key.size >= 256
    @s = (0..255).to_a
    j = 0 
    0.upto(255) do |i| 
      j = (j + @s[i] + @key[i] )%256
      @s[i], @s[j] = @s[j], @s[i]
    end    
  end

  def encrypt!(text)
    process text
  end  

  def encrypt(text)
    process text.dup
  end 

  private

  def process(text)
    0.upto(text.length-1) {|i| text[i] = text[i] ^ round}
    text
  end

  def round
    @q1 = (@q1 + 1)%256
    @q2 = (@q2 + @s[@q1])%256
    @s[@q1], @s[@q2] = @s[@q2], @s[@q1]
    @s[(@s[@q1]+@s[@q2])%256]  
  end

end

puts "Enter key."
    keyInput = gets.chomp
    keyInput = keyInput.to_s
    encryptInstance = Rc4.new(keyInput)
    decryptInstance = Rc4.new(keyInput)

  puts "Enter plaintext."
    plainInput = gets.chomp
    plainInput = plainInput.to_s
    cipherText = encryptInstance.encrypt(plainInput)

  puts "Plaintext is: " + plainInput

  puts "Ciphertext is: " + cipherText

  decryptedText = decryptInstance.encrypt(cipherText)

  puts "Decrypted text is: " + decryptedText
Rc4级
def初始化(str)
@q1,@q2=0,0
@键=[]
str.each_字节{| elem |@key=256
@s=(0..255)。到
j=0
0.高达(255)do|i|
j=(j+@s[i]+@key[i])%256
@s[i],@s[j]=@s[j],@s[i]
结束
结束
def加密!(文本)
处理文本
结束
def加密(文本)
进程text.dup
结束
私有的
def过程(文本)
0.最多(text.length-1){i|text[i]=text[i]^round}
文本
结束
def轮
@q1=(@q1+1)%256
@q2=(@q2+@s[@q1])%256
@s[@q1]、@s[@q2]=@s[@q2]、@s[@q1]
@s[(@s[@q1]+@s[@q2])%256]
结束
结束
输入“回车键”
keyInput=gets.chomp
keyInput=keyInput.to_s
encryptInstance=Rc4.new(keyInput)
decryptInstance=Rc4.new(keyInput)
放入“输入纯文本”
plainInput=gets.chomp
plainInput=plainInput.to_s
cipherText=encryptInstance.encrypt(明文输入)
puts“纯文本为:”+纯输入
放置“密文为:”+密文
decryptedText=decryptInstance.encrypt(密文)
puts“解密文本为:”+decryptedText
text[i]
这里是一个字符串。使用
text[i]。to\u i

这应该行得通

0.upto(text.length-1){i|text[i]=(text[i].ord^round.chr}


由于您正在进行加密,将“4”转换为4将是一个很大的错误。我们对编码进行操作并将其转换回。

您的问题是什么?为什么我会出现此错误/为什么它不会运行?我以前处理过RC4,您可以在此处看到代码:
0.upto(text.length-1){I | text[I]=text[I]^round}
将不起作用,因为您试图在表达式中使用字符串:
text[i]
text[i]^round
它应该是
text[i]。to_i^round
或其他什么。尝试了该操作后,我在下面的其他响应的注释中发布了另一个错误。rc4.rb:27:in
[]=':没有将Fixnum隐式转换为字符串(打字错误)从rc4.rb:27:in
block in process'从rc4.rb:27:in
upto'从rc4.rb:27:in
process'从rc4.rb:21:in
encrypt'从rc4.rb:49:in
'取下这个,所以我认为它应该可以工作,这个版本运行,现在我如何使密文显示为正常数字?这就是我得到的:明文Is:123 11 81 99 232 5密文是:ƻ?K?????6?解密文本是:123 11 81 99 232 5您输入的是什么?输入密钥。1234 5678输入明文。9876 5432明文是:9876 5432密文是:?T:?A?的解密文本是:9876 5432