Class: Omnizip::Password::ZipCryptoStrategy

Inherits:
EncryptionStrategy show all
Defined in:
lib/omnizip/password/zip_crypto_strategy.rb

Overview

Traditional ZIP encryption (PKWARE) WARNING: This is a weak encryption method and should not be used for sensitive data. Use WinZipAesStrategy instead.

Constant Summary collapse

COMPRESSION_METHOD =

ZIP compression method for traditional encryption

0

Instance Attribute Summary

Attributes inherited from EncryptionStrategy

#password

Instance Method Summary collapse

Methods inherited from EncryptionStrategy

#extra_field_data, #method_name, #supports?

Constructor Details

#initialize(password, warn_weak: true) ⇒ ZipCryptoStrategy

Initialize ZIP crypto encryption

Parameters:

  • password (String)

    Password to use

  • warn_weak (Boolean) (defaults to: true)

    Show security warning (default: true)



15
16
17
18
19
20
21
22
# File 'lib/omnizip/password/zip_crypto_strategy.rb', line 15

def initialize(password, warn_weak: true)
  super(password)

  if warn_weak
    warn "WARNING: Traditional ZIP encryption is weak and easily cracked."
    warn "Consider using WinZip AES encryption instead for better security."
  end
end

Instance Method Details

#compression_methodInteger

Get compression method for ZIP header

Returns:

  • (Integer)

    Compression method ID



70
71
72
# File 'lib/omnizip/password/zip_crypto_strategy.rb', line 70

def compression_method
  COMPRESSION_METHOD
end

#decrypt(data) ⇒ String

Decrypt data using traditional ZIP encryption

Parameters:

  • data (String)

    Data to decrypt

Returns:

  • (String)

    Decrypted data



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/omnizip/password/zip_crypto_strategy.rb', line 46

def decrypt(data)
  bytes = data.bytes
  keys = initialize_keys

  # Skip encryption header (12 bytes)
  header = bytes[0...12]
  encrypted_data = bytes[12..]

  # Verify header
  header.each { |byte| update_keys(keys, decrypt_byte(keys) ^ byte) }

  # Decrypt data
  decrypted = encrypted_data.map do |byte|
    temp = decrypt_byte(keys)
    plaintext_byte = byte ^ temp
    update_keys(keys, plaintext_byte)
    plaintext_byte
  end

  decrypted.pack("C*")
end

#encrypt(data) ⇒ String

Encrypt data using traditional ZIP encryption

Parameters:

  • data (String)

    Data to encrypt

Returns:

  • (String)

    Encrypted data



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/omnizip/password/zip_crypto_strategy.rb', line 27

def encrypt(data)
  keys = initialize_keys

  # Generate encryption header
  header = generate_encryption_header(keys)

  # Encrypt data with same keys (header generation updated them)
  encrypted = data.bytes.map do |byte|
    temp = decrypt_byte(keys)
    update_keys(keys, byte)
    byte ^ temp
  end

  (header + encrypted).pack("C*")
end

#encryption_flagsInteger

Get encryption flags

Returns:

  • (Integer)

    Encryption flags (bit 0 set)



76
77
78
# File 'lib/omnizip/password/zip_crypto_strategy.rb', line 76

def encryption_flags
  0x0001 # Traditional encryption
end