class HexaPDF::Encryption::RubyAES
Implementation of the general encryption algorithm AES
.
Since this algorithm is implemented in pure Ruby, it is not very fast. Therefore the FastAES
class based on OpenSSL should be used when possible.
For reference: This implementation is about 5000 times slower when decrypting and about 1800 times slower when encrypting than the FastAES
version.
This implementation is using AES
in Cipher Block Chaining (CBC) mode.
See: PDF1.7 s7.6.2
Public Class Methods
new(key, iv, mode)
click to toggle source
Creates a new AES
object using the given encryption key and initialization vector.
The mode must either be :encrypt or :decrypt.
# File lib/hexapdf/encryption/ruby_aes.rb, line 61 def initialize(key, iv, mode) @key = key @expanded_key_blocks = expand_key(@key) @prev_block = iv.bytes @mode = mode end
Public Instance Methods
process(data)
click to toggle source
Encrypts or decrypts the given data whose length must be a multiple of BLOCK_SIZE.
# File lib/hexapdf/encryption/ruby_aes.rb, line 69 def process(data) data = data.bytes (data.size / BLOCK_SIZE).times do |i| block = data[i * BLOCK_SIZE, BLOCK_SIZE] if @mode == :encrypt xor_blocks(block, @prev_block) # CBC: XOR plain text block with previous cipher block send(@mode, block) @prev_block = block else prev = block.dup send(@mode, block) xor_blocks(block, @prev_block) # CBC: XOR plain text block with previous cipher block @prev_block = prev end data[i * BLOCK_SIZE, BLOCK_SIZE] = block end data.pack('C*') end