| Class | PDF::Writer::ARC4 |
| In: |
lib/pdf/writer/arc4.rb
|
| Parent: | Object |
# File lib/pdf/writer/arc4.rb, line 48
48: def encrypt(text)
49: len = text.size
50: a = b = 0
51: c = @arc4.dup
52: out = ""
53:
54: text.each_byte do |x|
55: a = (a + 1) % 256
56: b = (b + c[a].to_i) % 256
57: c[a], c[b] = c[b], c[a]
58: k = (c[(c[a].to_i + c[b].to_i) % 256]).to_i
59: out << ("%c" % (x.to_i ^ k))
60: end
61: out
62: end
Initialize the ARC4 encryption.
# File lib/pdf/writer/arc4.rb, line 28
28: def init(key)
29: @arc4 = ""
30:
31: # Setup the control array
32: return if key.empty?
33:
34: a = []
35: (0..255).each { |ii| a[ii] = "%c" % ii }
36:
37: k = (key * 256)[0..255].split(//)
38:
39: jj = 0
40: @arc4.each_with_index do |el, ii|
41: jj = ((jj + el.to_i) + k[ii].to_i) % 256
42: a[ii], a[jj] = a[jj], a[ii]
43: end
44: @arc4 = a.join
45: end