class GostMagma::MagmaEcb

Public Class Methods

new(key) click to toggle source

key = 32-byte string

# File lib/gost_magma/magma_ecb.rb, line 4
def initialize(key)
  @key = key.dup.force_encoding('BINARY')
  @keys = []
  (0...8).each do |i|
    @keys << self.class.uint8ToUint32(@key[i*4...(i+1)*4].reverse)
  end
end

Public Instance Methods

decrypt(encrypted_text) click to toggle source

returns decrypted text string

# File lib/gost_magma/magma_ecb.rb, line 30
def decrypt(encrypted_text)
  len = encrypted_text.length
  if (len == 0) || (len % BlockLengthInBytes > 0) then
    puts "(encrypted_text.length == 0) || (encrypted_text.length % BlockLengthInBytes > 0)"
    return nil
  end
  blocks = encrypted_text.scan(/.{8}/m)
  decrypted_blocks = []
  blocks.each do |block|
    decryptedBlock = self.class.decryptBlock(block, @keys)
    decrypted_blocks << decryptedBlock
  end
  output = decrypted_blocks.join
  return output
end
encrypt(plain_text) click to toggle source

returns encrypted text string

# File lib/gost_magma/magma_ecb.rb, line 13
def encrypt(plain_text)
  len = plain_text.length
  if (len == 0) || (len % BlockLengthInBytes > 0) then
    puts "(plain_text.length == 0) || (plain_text.length % BlockLengthInBytes > 0)"
    return nil
  end
  blocks = plain_text.scan(/.{8}/m)
  encrypted_blocks = []
  blocks.each do |block|
    encryptedBlock = self.class.encryptBlock(block, @keys)
    encrypted_blocks << encryptedBlock
  end
  output = encrypted_blocks.join
  return output
end