Class: Omnizip::Crypto::Aes256::Cipher

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/crypto/aes256/cipher.rb

Overview

AES-256-CBC cipher operations

Wraps OpenSSL's AES-256-CBC implementation with proper padding and error handling for 7-Zip compatibility.

Constant Summary

Constants included from Constants

Omnizip::Crypto::Aes256::Constants::BLOCK_SIZE, Omnizip::Crypto::Aes256::Constants::DEFAULT_CYCLES_POWER, Omnizip::Crypto::Aes256::Constants::IV_SIZE, Omnizip::Crypto::Aes256::Constants::KEY_SIZE, Omnizip::Crypto::Aes256::Constants::MAX_CYCLES_POWER, Omnizip::Crypto::Aes256::Constants::MIN_CYCLES_POWER, Omnizip::Crypto::Aes256::Constants::SALT_SIZE, Omnizip::Crypto::Aes256::Constants::SALT_SIZE_MAX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, iv) ⇒ Cipher

Initialize cipher with key and IV

Parameters:

  • key (String)

    32-byte AES-256 key

  • iv (String)

    16-byte initialization vector



23
24
25
26
27
# File 'lib/omnizip/crypto/aes256/cipher.rb', line 23

def initialize(key, iv)
  validate_key_iv(key, iv)
  @key = key
  @iv = iv
end

Instance Attribute Details

#ivObject (readonly)

Returns the value of attribute iv.



17
18
19
# File 'lib/omnizip/crypto/aes256/cipher.rb', line 17

def iv
  @iv
end

#keyObject (readonly)

Returns the value of attribute key.



17
18
19
# File 'lib/omnizip/crypto/aes256/cipher.rb', line 17

def key
  @key
end

Instance Method Details

#decrypt(ciphertext) ⇒ String

Decrypt data using AES-256-CBC

Parameters:

  • ciphertext (String)

    Encrypted data

Returns:

  • (String)

    Decrypted data



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/omnizip/crypto/aes256/cipher.rb', line 51

def decrypt(ciphertext)
  cipher = create_cipher(:decrypt)

  # Handle empty or very small ciphertext
  if ciphertext.empty?
    return ""
  end

  result = cipher.update(ciphertext)
  result << cipher.final

  result
end

#encrypt(plaintext) ⇒ String

Encrypt data using AES-256-CBC

Parameters:

  • plaintext (String)

    Data to encrypt

Returns:

  • (String)

    Encrypted data



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/omnizip/crypto/aes256/cipher.rb', line 33

def encrypt(plaintext)
  cipher = create_cipher(:encrypt)

  # Handle empty data - just call final() for padding
  if plaintext.empty?
    return cipher.final
  end

  result = cipher.update(plaintext)
  result << cipher.final

  result
end