Module: Crypt::CBC

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

Constant Summary collapse

ULONG =
0x100000000

Instance Method Summary collapse

Instance Method Details

#carefully_open_file(filename, mode) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/extensions/crypt/crypt/cbc.rb', line 78

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



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

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/extensions/crypt/crypt/cbc.rb', line 60

def decrypt_stream(cryptStream, plainStream)
  # Cypher-block-chain mode
  chain = cryptStream.read(block_size())

  while (block = cryptStream.read(block_size()))
    decrypted = decrypt_block(block)
    plainText = decrypted ^ chain
    plainStream.write(plainText) unless cryptStream.eof?
    chain = block
  end
  
  # write the final block, omitting the padding
  buffer = plainText.split('')
  remainingMessageBytes = buffer.last.unpack('C').first
  remainingMessageBytes.times { plainStream.write(buffer.shift) }
end

#decrypt_string(cryptText) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/extensions/crypt/crypt/cbc.rb', line 117

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



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

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/extensions/crypt/crypt/cbc.rb', line 29

def encrypt_stream(plainStream, cryptStream)
  # Cypher-block-chain mode
  
  initVector = generate_initialization_vector(block_size() / 4)
  chain = encrypt_block(initVector)
  cryptStream.write(chain)

  while ((block = plainStream.read(block_size())) && (block.length == block_size()))
    block = block ^ chain 
    encrypted = encrypt_block(block)
    cryptStream.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('')
  remainingMessageBytes = buffer.length
  # we use 7-bit characters to avoid possible strange behavior on the Mac
  remainingMessageBytes.upto(block_size()-2) { buffer << rand(128).chr }
  buffer << remainingMessageBytes.chr
  block = buffer.join('')
  block = block ^ chain
  encrypted = encrypt_block(block)
  cryptStream.write(encrypted)
end

#encrypt_string(plainText) ⇒ Object



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

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

#generate_initialization_vector(words) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/extensions/crypt/crypt/cbc.rb', line 19

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