Skip to content

Instantly share code, notes, and snippets.

@AdrianVollmer
Created September 12, 2019 15:26
Show Gist options
  • Save AdrianVollmer/3d076f4097c80fb0a08512cc3a2a6844 to your computer and use it in GitHub Desktop.
Save AdrianVollmer/3d076f4097c80fb0a08512cc3a2a6844 to your computer and use it in GitHub Desktop.
test rc4 ruby
# -*- coding: binary -*-
require 'digest'
class RC4
def initialize(str)
begin
raise SyntaxError, "RC4: Key supplied is blank" if str.eql?('')
@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
end
def encrypt!(text)
process text
end
def encrypt(text)
process text.dup
end
alias_method :decrypt, :encrypt
private
def process(text)
text.unpack("C*").map { |c| c ^ round }.pack("C*")
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
key = "abcdefghjikjlmnopqrstuvwxyz"
rc4 = RC4.new(key)
cleartext = ""
i = 1
x = 123
until i > 10000
x = (43*x+23)%256
cleartext += x.chr
i = i + 1
end
puts Digest::SHA2.hexdigest cleartext
ciphertext = rc4.encrypt(cleartext)
puts Digest::SHA2.hexdigest ciphertext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment