Class: Omnizip::Formats::Rar::Rar5::Encryption::Aes256Cbc

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/formats/rar/rar5/encryption/aes256_cbc.rb

Overview

AES-256-CBC cipher for RAR5 encryption

Wrapper around the existing Crypto::Aes256::Cipher that provides RAR5-specific encryption/decryption with proper key and IV handling.

RAR5 uses:

  • AES-256 in CBC mode
  • PKCS#7 padding
  • Per-file IV generation
  • PBKDF2-HMAC-SHA256 key derivation

Examples:

Encrypt file data

cipher = Aes256Cbc.new(key, iv)
encrypted = cipher.encrypt(data)

Constant Summary collapse

IV_SIZE =

IV size (16 bytes = AES block size)

16
KEY_SIZE =

Key size (32 bytes for AES-256)

32

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, iv) ⇒ Aes256Cbc

Initialize cipher with key and IV

Parameters:

  • key (String)

    32-byte AES-256 key

  • iv (String)

    16-byte initialization vector

Raises:

  • (ArgumentError)

    If key or IV wrong size



42
43
44
45
46
47
# File 'lib/omnizip/formats/rar/rar5/encryption/aes256_cbc.rb', line 42

def initialize(key, iv)
  validate_key_iv(key, iv)
  @key = key
  @iv = iv
  @cipher = Crypto::Aes256::Cipher.new(key, iv)
end

Instance Attribute Details

#ivString (readonly)

Returns Initialization vector (16 bytes).

Returns:

  • (String)

    Initialization vector (16 bytes)



35
36
37
# File 'lib/omnizip/formats/rar/rar5/encryption/aes256_cbc.rb', line 35

def iv
  @iv
end

#keyString (readonly)

Returns AES-256 key (32 bytes).

Returns:

  • (String)

    AES-256 key (32 bytes)



32
33
34
# File 'lib/omnizip/formats/rar/rar5/encryption/aes256_cbc.rb', line 32

def key
  @key
end

Class Method Details

.generate_ivString

Generate random IV

Returns:

  • (String)

    16-byte random IV



68
69
70
# File 'lib/omnizip/formats/rar/rar5/encryption/aes256_cbc.rb', line 68

def self.generate_iv
  SecureRandom.random_bytes(IV_SIZE)
end

Instance Method Details

#decrypt(ciphertext) ⇒ String

Decrypt data

Parameters:

  • ciphertext (String)

    Encrypted data

Returns:

  • (String)

    Decrypted data (padding removed)



61
62
63
# File 'lib/omnizip/formats/rar/rar5/encryption/aes256_cbc.rb', line 61

def decrypt(ciphertext)
  @cipher.decrypt(ciphertext)
end

#encrypt(plaintext) ⇒ String

Encrypt data

Parameters:

  • plaintext (String)

    Data to encrypt

Returns:

  • (String)

    Encrypted data (with PKCS#7 padding)



53
54
55
# File 'lib/omnizip/formats/rar/rar5/encryption/aes256_cbc.rb', line 53

def encrypt(plaintext)
  @cipher.encrypt(plaintext)
end