Module: Crypt::CBC

Included in:
Blowfish, Gost, IDEA, RC6, Rijndael
Defined in:
lib/crypt/cbc.rb

Constant Summary collapse

ULONG =
0x100000000

Instance Method Summary collapse

Instance Method Details

#carefully_open_file(filename, mode) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/crypt/cbc.rb', line 70

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

#decrypt_file(crypt_filename, plain_filename) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/crypt/cbc.rb', line 91

def decrypt_file(crypt_filename, plain_filename)
  crypt_file = carefully_open_file(crypt_filename, 'rb')
  plain_file = carefully_open_file(plain_filename, 'wb+')
  decrypt_stream(crypt_file, plain_file)
  crypt_file.close unless crypt_file.closed?
  plain_file.close unless plain_file.closed?
end

#decrypt_stream(crypt_stream, plain_stream) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/crypt/cbc.rb', line 52

def decrypt_stream(crypt_stream, plain_stream)
  # Cypher-block-chain mode
  chain = crypt_stream.read(block_size())

  while (block = crypt_stream.read(block_size()))
    decrypted = decrypt_block(block)
    plain_text = decrypted ^ chain
    plain_stream.write(plain_text) unless crypt_stream.eof?
    chain = block
  end

  # write the final block, omitting the padding
  buffer = plain_text.split('')
  remaining_message_bytes = block_size() - buffer.last.unpack('C').first
  remaining_message_bytes.times { plain_stream.write(buffer.shift) }
end

#decrypt_string(crypt_text) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/crypt/cbc.rb', line 109

def decrypt_string(crypt_text)
  crypt_stream = StringIO.new(crypt_text)
  plain_stream = StringIO.new('')
  decrypt_stream(crypt_stream, plain_stream)
  plain_text = plain_stream.string
  return(plain_text)
end

#encrypt_file(plain_filename, crypt_filename) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/crypt/cbc.rb', line 82

def encrypt_file(plain_filename, crypt_filename)
  plain_file = carefully_open_file(plain_filename, 'rb')
  crypt_file = carefully_open_file(crypt_filename, 'wb+')
  encrypt_stream(plain_file, crypt_file)
  plain_file.close unless plain_file.closed?
  crypt_file.close unless crypt_file.closed?
end

#encrypt_stream(plain_stream, crypt_stream, init_vector = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/crypt/cbc.rb', line 24

def encrypt_stream(plain_stream, crypt_stream, init_vector = nil)
  # Cypher-block-chain mode
  init_vector ||= generate_initialization_vector(block_size() / 4)
  chain = encrypt_block(init_vector)
  crypt_stream.write(chain)

  while ((block = plain_stream.read(block_size())) && (block.length == block_size()))
    block = block ^ chain
    encrypted = encrypt_block(block)
    crypt_stream.write(encrypted)
    chain = encrypted
  end

  # write the final block
  # At most block_size()-1 bytes can be part of the message.
  # That means the final byte can be used to store the number of meaningful
  # bytes in the final block
  block = '' if block.nil?
  buffer = block.split('')
  remaining_message_bytes = block_size() - buffer.length
  buffer << remaining_message_bytes.chr * remaining_message_bytes
  block = buffer.join('')
  block = block ^ chain
  encrypted = encrypt_block(block)
  crypt_stream.write(encrypted)
end

#encrypt_string(plain_text, init_vector = nil) ⇒ Object



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

def encrypt_string(plain_text, init_vector = nil)
  plain_stream = StringIO.new(plain_text)
  crypt_stream = StringIO.new('')
  encrypt_stream(plain_stream, crypt_stream, init_vector)
  crypt_text = crypt_stream.string
  return(crypt_text)
end

#generate_initialization_vector(words) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/crypt/cbc.rb', line 15

def generate_initialization_vector(words)
  vector = ""
  words.times {
    vector << [rand(ULONG)].pack('N')
  }
  return(vector)
end