-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRC4_progress.rb
More file actions
70 lines (57 loc) · 1.19 KB
/
Copy pathRC4_progress.rb
File metadata and controls
70 lines (57 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Clase de RC4
class RC4
def initialize(key)
# Variables 'i' y 'j' para la salida (PRGA). Las de KSA son locales
@i = 0
@j = 0
# KSA ---------------------------------------: START
@key = key.bytes
@S = Array.new(256)
@S.each_index do |index|
@S[index] = index
end
key_len = @key.count
j = 0
256.times do |i|
j = (j + @S[i] + @key[i % key_len]) % 256
swap(i,j)
end
# KSA ---------------------------------------: END
end
# SWAP
def swap(i,j)
tmp = @S[i]
@S[i] = @S[j]
@S[j] = tmp
end
# PRGA
def more
@i = (@i + 1) % 256
@j = (@j + @S[@i]) % 256
swap(@i, @j)
k = @S[ ( @S[@i] + @S[@j] ) % 256 ]
return k
end
attr_accessor :percent
# File cipher
def run(file)
@percent = 0.0
# Check file extension
if (file[-4,4] == ".rc4")
destination = File.new(file[0,file.length-4],"wb")
elsif
destination = File.new(file+".rc4", "wb")
end
# Read and cipher
source = File.open(file, "rb")
source_size = Float(source.size)
current_size = 0.0
source.each_byte do |byte|
current_size += 1
destination.print (byte ^ more).chr
@percent = current_size/source_size
end
destination.close
source.close
end
end