Module: IrcBlowfish

Defined in:
lib/ircblowfish.rb,
lib/ircblowfish/base64.rb,
lib/ircblowfish/version.rb

Defined Under Namespace

Modules: Base64

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.decrypt(msg, key) ⇒ String

Decrypts a message using the specified key

Parameters:

  • msg (String)

    the message to be decrypted

  • key (String)

    the key used for decryption

Returns:

  • (String)

    the decrypted message



22
23
24
25
# File 'lib/ircblowfish.rb', line 22

def self.decrypt(msg, key)
  return decrypt_ecb(msg, key) if key =~ /^(?:old|ecb):/
  return decrypt_cbc(msg, key)
end

.decrypt_cbc(msg, key) ⇒ Object

Note:

Forces the use of Blowfish-CBC despite the key passed



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ircblowfish.rb', line 114

def self.decrypt_cbc(msg, key)
  return msg if key.nil? or key == ''
  return msg if msg.nil? or msg == ''

  # Ensure the message is a valid Blowfish-CBC message and remove the prefix
  ciphertext = msg.dup
  return msg unless ciphertext.sub! %r{^\+(?:OK|mcps) \*}, ''

  # Remove the cbc: prefix if it's used
  key = key.sub %r{^cbc:}, ''
  return msg if key == ''

  # Decode the text to get the IV + ciphertext
  ciphertext = ::Base64.decode64 ciphertext

  return msg if ciphertext.bytesize < 8

  iv = ciphertext[0,8]           # Extract the IV from the string
  ciphertext = ciphertext[8..-1] # Remove the IV from the string

  return '' if ciphertext == ''

  # Create the Blowfish-CBC cipher
  cipher = OpenSSL::Cipher.new 'bf-cbc'
  cipher.decrypt
  cipher.key_len = key.length
  cipher.key = key
  cipher.padding = 0
  cipher.iv = iv

  # Decrypt the ciphertext and remove the trailing padding
  cipher.update(ciphertext).sub! %r{\x00*$}, ''
end

.decrypt_ecb(msg, key) ⇒ Object

Note:

Forces the use of Blowfish-ECB despite the key passed



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ircblowfish.rb', line 56

def self.decrypt_ecb(msg, key)
  return msg if key.nil? or key == ''
  return msg if msg.nil? or msg == ''

  # Ensure the message is a valid Blowfish-ECB message and remove the prefix
  ciphertext = msg.dup
  return msg unless ciphertext.sub! %r{^\+(?:OK|mcps) }, ''
  return msg if ciphertext[0] == '*' # Dump if this is actually a Blowfish-CBC message
  return '' if ciphertext == '' # I've seen some clients send "+OK " for null messages

  # Remove the ecb:/old: prefix if it's used
  key = key.sub %r{^(?:old|ecb):}, ''
  return msg if key == ''

  # Create the Blowfish-CBC cipher
  cipher = OpenSSL::Cipher.new 'bf-ecb'
  cipher.decrypt
  cipher.key_len = key.length
  cipher.key = key
  cipher.padding = 0

  # Decrypt the ciphertext and remove the trailing padding
  cipher.update(IrcBlowfish::Base64.decode(ciphertext)).sub! %r{\x00*$}, ''
end

.encrypt(msg, key) ⇒ String

Encrypts a message using the specified key

Parameters:

  • msg (String)

    the message to be encrypted

  • key (String)

    the key used for encryption

Returns:

  • (String)

    the encrypted message



13
14
15
16
# File 'lib/ircblowfish.rb', line 13

def self.encrypt(msg, key)
  return encrypt_ecb(msg, key) if key =~ /^(?:old|ecb):/
  return encrypt_cbc(msg, key)
end

.encrypt_cbc(msg, key) ⇒ Object

Note:

Forces the use of Blowfish-CBC despite the key passed



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ircblowfish.rb', line 83

def self.encrypt_cbc(msg, key)
  return msg if key.nil? or key == ''
  return msg if msg.nil? or msg == ''

  # Force encoding to binary in case of non-ascii messages
  plaintext = msg.dup.force_encoding 'BINARY'

  # Remove the cbc: prefix if it's used
  key = key.sub %r{^cbc:}, ''
  return msg if key == ''

  # Generate a random IV of length 8
  iv = random_iv 8

  # Create the Blowfish-CBC cipher
  cipher = OpenSSL::Cipher.new 'bf-cbc'
  cipher.encrypt
  cipher.key_len = key.length
  cipher.key = key
  cipher.padding = 0
  cipher.iv = iv

  # Add null padding to make the length a multiple of 8
  plaintext += "\x00" * (8 - (plaintext.bytesize % 8))

  # Generate the IRC message with prefix
  '+OK *' + ::Base64.encode64(iv + cipher.update(plaintext)).gsub!(/\n/, '')
end

.encrypt_ecb(msg, key) ⇒ Object

Note:

Forces the use of Blowfish-ECB despite the key passed



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ircblowfish.rb', line 29

def self.encrypt_ecb(msg, key)
  return msg if key.nil? or key == ''
  return msg if msg.nil? or msg == ''

  # Force encoding to binary in case of non-ascii messages
  plaintext = msg.dup.force_encoding 'BINARY'

  # Remove the ecb:/old: prefix if it's used
  key = key.sub %r{^(?:old|ecb):}, ''
  return msg if key == ''

  # Create the Blowfish-CBC cipher
  cipher = OpenSSL::Cipher.new 'bf-ecb'
  cipher.encrypt
  cipher.key_len = key.length
  cipher.key = key
  cipher.padding = 0

  # Add null padding to make the length a multiple of 8
  plaintext += "\x00" * (8 - (plaintext.bytesize % 8))

  # Generate the IRC message with prefix
  '+OK ' + IrcBlowfish::Base64.encode(cipher.update(plaintext))
end