Class: NmDatafile::BF

Inherits:
Struct
  • Object
show all
Defined in:
lib/nm_datafile/b_f.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject

Returns the value of attribute key



7
8
9
# File 'lib/nm_datafile/b_f.rb', line 7

def key
  @key
end

#pad_with_spacesObject

Returns the value of attribute pad_with_spaces



7
8
9
# File 'lib/nm_datafile/b_f.rb', line 7

def pad_with_spaces
  @pad_with_spaces
end

Instance Method Details

#decrypt(hex_encoded) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/nm_datafile/b_f.rb', line 19

def decrypt(hex_encoded)
  cipher = OpenSSL::Cipher.new('bf-ecb').decrypt
  cipher.padding = 0 if pad_with_spaces
  cipher.key = key
  binary_data = [hex_encoded].pack('H*')
  str = cipher.update(binary_data) << cipher.final
  str.force_encoding(Encoding::UTF_8)
  str
end

#encrypt(str) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/nm_datafile/b_f.rb', line 8

def encrypt(str)
  cipher = OpenSSL::Cipher.new('bf-ecb').encrypt
  if pad_with_spaces
    str += " " until str.bytesize % 8 == 0
    cipher.padding = 0
  end
  cipher.key = key
  binary_data = cipher.update(str) << cipher.final
  hex_encoded = binary_data.unpack('H*').first
end