Module: Crypt::ECB

Included in:
Blowfish
Defined in:
lib/crypto/ecb_null_padded_blowfish.rb

Instance Method Summary collapse

Instance Method Details

#carefully_open_file(filename, mode) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/crypto/ecb_null_padded_blowfish.rb', line 40

def carefully_open_file(filename, mode)
  begin
    aFile = File.new(filename, mode)
  rescue
    puts "Sorry. There was a problem opening the file <#{filename}>."
    aFile.close() unless aFile.nil?
    raise
  end
  return(aFile)
end

#decrypt_file(cryptFilename, plainFilename) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/crypto/ecb_null_padded_blowfish.rb', line 59

def decrypt_file(cryptFilename, plainFilename)
  cryptFile = carefully_open_file(cryptFilename, 'rb')
  plainFile = carefully_open_file(plainFilename, 'wb+')
  decrypt_stream(cryptFile, plainFile)
  cryptFile.close unless cryptFile.closed?
  plainFile.close unless plainFile.closed?
end

#decrypt_stream(cryptStream, plainStream) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/crypto/ecb_null_padded_blowfish.rb', line 32

def decrypt_stream(cryptStream, plainStream)
  while (block = cryptStream.read(block_size()))
    plainText = decrypt_block(block)
    plainStream.write(plainText) unless cryptStream.eof?
  end
  plainStream.write(unpadded_last_block(plainText))
end

#decrypt_string(cryptText) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/crypto/ecb_null_padded_blowfish.rb', line 75

def decrypt_string(cryptText)
  cryptStream = StringIO.new(cryptText)
  plainStream = StringIO.new('')
  decrypt_stream(cryptStream, plainStream)
  plainText = plainStream.string
  return(plainText)
end

#encrypt_file(plainFilename, cryptFilename) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/crypto/ecb_null_padded_blowfish.rb', line 51

def encrypt_file(plainFilename, cryptFilename)
  plainFile = carefully_open_file(plainFilename, 'rb')
  cryptFile = carefully_open_file(cryptFilename, 'wb+')
  encrypt_stream(plainFile, cryptFile)
  plainFile.close unless plainFile.closed?
  cryptFile.close unless cryptFile.closed?
end

#encrypt_stream(plainStream, cryptStream) ⇒ Object



25
26
27
28
29
30
# File 'lib/crypto/ecb_null_padded_blowfish.rb', line 25

def encrypt_stream(plainStream, cryptStream)
  while ((block = plainStream.read(block_size())) && (block.length == block_size()))
    cryptStream.write(encrypt_block(block))
  end
  cryptStream.write(encrypt_block(padded_last_block(block)))
end

#encrypt_string(plainText) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/crypto/ecb_null_padded_blowfish.rb', line 67

def encrypt_string(plainText)
  plainStream = StringIO.new(plainText)
  cryptStream = StringIO.new('')
  encrypt_stream(plainStream, cryptStream)
  cryptText = cryptStream.string
  return(cryptText)
end

#padded_last_block(last_block) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/crypto/ecb_null_padded_blowfish.rb', line 3

def padded_last_block(last_block)
  block = last_block || ''
  buffer = block.split('')
  remainingMessageBytes = buffer.length
  remainingMessageBytes.upto(block_size()-1) { buffer << 0.chr }
  return buffer.join('')
end

#unpadded_last_block(last_block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/crypto/ecb_null_padded_blowfish.rb', line 11

def unpadded_last_block(last_block)
  block = last_block || ''
  chars = block.split('')
  buffer = []
  chars.each do |c|
    if c != 0
      buffer << c
    else
      break
    end
  end
  return buffer.join('')
end