class RC4
Constants
- STATE
Attributes
key[R]
Public Class Methods
new(str)
click to toggle source
# File lib/rc4_cipher.rb, line 5 def initialize(str) @i, @j = 0 , 0 ini_state(str) end
Public Instance Methods
encrypt!(text)
click to toggle source
# File lib/rc4_cipher.rb, line 12 def encrypt!(text) n = 0 while n < text.length text.setbyte(n,key.next ^ text.getbyte(n)) n = n.next end text end
Also aliased as: decrypt!
Private Instance Methods
ini_state(key)
click to toggle source
# File lib/rc4_cipher.rb, line 29 def ini_state(key) @S = STATE.dup j = 0 for i in 0..255 j = (j + @S[i] + key.getbyte(i % key.length)) % 256 @S[i],@S[j] = @S[j],@S[i] end @key = Enumerator.new do |k| while true @i = (@i + 1) % 256 @j = (@j+ @S[@i]) % 256 @S[@i], @S[@j] = @S[@j], @S[@i] k.yield @S[(@S[@i]+@S[@j])%256] end end end