Module: ComicWalker::Cipher

Defined in:
lib/comic_walker/cipher.rb

Constant Summary collapse

BASE_KEY =
[173, 43, 117, 127, 230, 58, 73, 84, 154, 177, 47, 81, 108, 200, 101, 65].pack('C*')

Class Method Summary collapse

Class Method Details

.decrypt_b64(key, b64data) ⇒ String

Decrypt Base64-encoded data

Parameters:

  • key (String)

    key

  • b64data (String)

    Encrypted Base64-encoded data

Returns:

  • (String)

    Decrypted data



27
28
29
30
31
32
# File 'lib/comic_walker/cipher.rb', line 27

def decrypt_b64(key, b64data)
  data = Base64.decode64(b64data)
  md5 = Digest::MD5.hexdigest(key + data.byteslice(8, 8))
  l = md5.scan(/.{2}/).map { |xy| Integer(xy, 16) }
  decrypt_rc4(l, data[16 .. -1].unpack('C*')).pack('C*')
end

.decrypt_license(bid, u1, license_b64) ⇒ String

Decrypt Base64-encoded license object

Parameters:

  • bid (String)

    browser id

  • u1 (String)

    got from cookie

  • license_b64 (String)

    Base64-encoded license object

Returns:

  • (String)


15
16
17
18
19
20
21
# File 'lib/comic_walker/cipher.rb', line 15

def decrypt_license(bid, u1, license_b64)
  h = bid.chars.map(&:ord)
  if u1
    h += u1.chars.map(&:ord)
  end
  decrypt_b64(h.pack('C*') + BASE_KEY, license_b64)
end

.decrypt_rc4(key, data) ⇒ Array<Fixnum>

Decrypt data

Parameters:

  • key (Array<Fixnum>)
  • data (Array<Fixnum>)

Returns:

  • (Array<Fixnum>)

    Decrypt data



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/comic_walker/cipher.rb', line 38

def decrypt_rc4(key, data)
  s = gen_rc4_table(key)
  i = 0
  j = 0
  data.map do |x|
    i = (i + 1) & 0xff
    j = (j + s[i]) & 0xff
    s[i], s[j] = s[j], s[i]
    k = s[(s[i] + s[j]) & 0xff]
    x ^ k
  end
end

.gen_rc4_table(key) ⇒ Array<Fixnum>

Returns RC4 table.

Parameters:

  • key (Array<Fixnum>)

Returns:

  • (Array<Fixnum>)

    RC4 table



53
54
55
56
57
58
59
60
61
# File 'lib/comic_walker/cipher.rb', line 53

def gen_rc4_table(key)
  s = 256.times.to_a
  j = 0
  256.times do |i|
    j = (j + s[i] + key[i % key.size]) & 0xff
    s[i], s[j] = s[j], s[i]
  end
  s
end