Class: AesAlg

Inherits:
Object
  • Object
show all
Includes:
AesCons, AesShared
Defined in:
lib/ruby-aes/aes_alg.rb

Constant Summary

Constants included from AesCons

AesCons::RCON, AesCons::S0, AesCons::S1, AesCons::S2, AesCons::S3, AesCons::Si0, AesCons::Si1, AesCons::Si2, AesCons::Si3, AesCons::Td0, AesCons::Td1, AesCons::Td2, AesCons::Td3, AesCons::Te0, AesCons::Te1, AesCons::Te2, AesCons::Te3

Instance Method Summary collapse

Methods included from AesShared

#decrypt_blocks, #decrypt_buffer, #encrypt_blocks, #encrypt_buffer

Constructor Details

#initialize(key_length, mode, key, iv = nil) ⇒ AesAlg

Returns a new instance of AesAlg.



312
313
314
# File 'lib/ruby-aes/aes_alg.rb', line 312

def initialize(key_length, mode, key, iv = nil)
    init(key_length, mode, key, iv)
end

Instance Method Details

#decrypt_block(block) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/ruby-aes/aes_alg.rb', line 264

def decrypt_block(block)
    case @mode
    when 'ECB'
        _decrypt_block(block)
    when 'CBC'
        o = xor(_decrypt_block(block), @iv)
        @iv = block
        o
    when 'OFB'
        @iv = _encrypt_block(@iv)
        xor(@iv, block)
    when 'CFB'
        o = xor(_encrypt_block(@iv), block)
        @iv = block
        o
    end
end

#encrypt_block(block) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/ruby-aes/aes_alg.rb', line 250

def encrypt_block(block)
    case @mode
    when 'ECB'
        _encrypt_block(block)
    when 'CBC'
        @iv = _encrypt_block(xor(block, @iv))
    when 'OFB'
        @iv = _encrypt_block(@iv)
        xor(@iv, block)
    when 'CFB'
        @iv = xor(_encrypt_block(@iv), block)
    end
end

#init(key_length, mode, key, iv = nil) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/ruby-aes/aes_alg.rb', line 282

def init(key_length, mode, key, iv = nil)
    @nb = 4
    @ek = []
    @dk = []
    @state = nil
    @iv = "\000" * 16
    @iv = iv if iv
    case key_length
    when 128
        @nk = 4
        @nr = 10
    when 192
        @nk = 6
        @nr = 12
    when 256
        @nk = 8
        @nr = 14
    else
        raise 'Bad Key length'
    end
    @kl = key_length
    case mode
    when 'ECB', 'CBC', 'OFB', 'CFB'
        @mode = mode
    else
        raise 'Bad AES mode'
    end
    decryption_key_schedule(key)
end