Class: AesAlg
- Inherits:
-
Object
- Object
- AesAlg
- Defined in:
- lib/ruby-aes/aes_alg.rb
Constant Summary
Constants included from AesCons
AesCons::RCON, AesCons::Td0, AesCons::Td1, AesCons::Td2, AesCons::Td3, AesCons::Td4, AesCons::Te0, AesCons::Te1, AesCons::Te2, AesCons::Te3, AesCons::Te4
Instance Method Summary collapse
- #decrypt_block(block) ⇒ Object
- #encrypt_block(block) ⇒ Object
- #init(key_length, mode, key, iv = nil) ⇒ Object
-
#initialize(key_length, mode, key, iv = nil) ⇒ AesAlg
constructor
A new instance of AesAlg.
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.
542 543 544 |
# File 'lib/ruby-aes/aes_alg.rb', line 542 def initialize(key_length, mode, key, iv = nil) init(key_length, mode, key, iv) end |
Instance Method Details
#decrypt_block(block) ⇒ Object
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/ruby-aes/aes_alg.rb', line 494 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
480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/ruby-aes/aes_alg.rb', line 480 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
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/ruby-aes/aes_alg.rb', line 512 def init(key_length, mode, key, iv = nil) @nb = 4 @ek = [] @rk = [] @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 |