Class: Crypt::Blowfish

Inherits:
Object show all
Includes:
BlowfishTables, CBC
Defined in:
lib/extensions/crypt/crypt/blowfish.rb

Constant Summary collapse

ULONG =
0x100000000

Constants included from BlowfishTables

Crypt::BlowfishTables::INITIALPARRAY, Crypt::BlowfishTables::INITIALSBOXES

Instance Method Summary collapse

Methods included from CBC

#carefully_open_file, #decrypt_file, #decrypt_stream, #decrypt_string, #encrypt_file, #encrypt_stream, #encrypt_string, #generate_initialization_vector

Constructor Details

#initialize(key) ⇒ Blowfish

Returns a new instance of Blowfish.



22
23
24
25
26
27
28
# File 'lib/extensions/crypt/crypt/blowfish.rb', line 22

def initialize(key)
  @key = key
  raise "Bad key length: the key must be 1-56 bytes." unless (key.length.between?(1,56))
  @pArray = []
  @sBoxes = []
  setup_blowfish()
end

Instance Method Details

#block_sizeObject



17
18
19
# File 'lib/extensions/crypt/crypt/blowfish.rb', line 17

def block_size
  return(8)
end

#decrypt_block(block) ⇒ Object



108
109
110
111
112
113
# File 'lib/extensions/crypt/crypt/blowfish.rb', line 108

def decrypt_block(block)
  xl, xr = block.unpack('NN')
  xl, xr = decrypt_pair(xl, xr)
  decrypted = [xl, xr].pack('NN')
  return(decrypted)
end

#decrypt_pair(xl, xr) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/extensions/crypt/crypt/blowfish.rb', line 87

def decrypt_pair(xl, xr)
  17.downto(2) { |i|
      xl = (xl ^ @pArray[i]) % ULONG
      xr = (xr ^ f(xl)) % ULONG
      xl, xr = [xl, xr].reverse
  }
  xl, xr = [xl, xr].reverse
  xr = (xr ^ @pArray[1]) % ULONG
  xl = (xl ^ @pArray[0]) % ULONG
  return([xl, xr])
end

#encrypt1(i, l, r) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/extensions/crypt/crypt/blowfish.rb', line 65

def encrypt1(i,l,r)
    0.step(255, 2) { |j|
      l, r = encrypt_pair(l, r)
      @sBoxes[i][j]   = l
      @sBoxes[i][j+1] = r
    }
    return([l, r])
end

#encrypt_block(block) ⇒ Object



100
101
102
103
104
105
# File 'lib/extensions/crypt/crypt/blowfish.rb', line 100

def encrypt_block(block)
  xl, xr = block.unpack('NN')
  xl, xr = encrypt_pair(xl, xr)
  encrypted = [xl, xr].pack('NN')
  return(encrypted)
end

#encrypt_pair(xl, xr) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/extensions/crypt/crypt/blowfish.rb', line 74

def encrypt_pair(xl, xr)
  0.upto(15) { |i|
      xl = (xl ^ @pArray[i]) % ULONG
      xr = (xr ^ f(xl)) % ULONG
      xl, xr = [xl, xr].reverse
  }
  xl, xr = [xl, xr].reverse
  xr = (xr ^ @pArray[16]) % ULONG
  xl = (xl ^ @pArray[17]) % ULONG
  return([xl, xr])
end

#f(x) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/extensions/crypt/crypt/blowfish.rb', line 31

def f(x)
  a, b, c, d = [x].pack('N').unpack('CCCC')
  y = (@sBoxes[0][a] + @sBoxes[1][b]) % ULONG
  y = (y ^ @sBoxes[2][c]) % ULONG
  y = (y + @sBoxes[3][d]) % ULONG
  return(y)
end

#setup_blowfishObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/extensions/crypt/crypt/blowfish.rb', line 40

def setup_blowfish()
  @sBoxes = Array.new(4) { |i| INITIALSBOXES[i].clone }
  @pArray = INITIALPARRAY.clone
  keypos = 0
  0.upto(17) { |i|
    data = 0
    4.times {
      #data = ((data << 8) | @key[keypos]) % ULONG
data = ((data << 8) | @key[keypos].ord) % ULONG
      keypos = (keypos.next) % @key.length
    }
    @pArray[i] = (@pArray[i] ^ data) % ULONG
  }
  l = 0
  r = 0
  0.step(17, 2) { |i|
    l, r = encrypt_pair(l, r)
    @pArray[i]   = l
    @pArray[i+1] = r
  }
  0.upto(3) { |i|
      l, r = encrypt1(i,l,r)
  }
end