WEEK 4: OTP - M199205zn/IAS-CS4 GitHub Wiki

One-Time Pad, Block Cipher, and Stream Cipher

1. One-Time Pad (OTP)

The One-Time Pad is a symmetric key encryption algorithm known for its theoretical perfect security. It operates by combining plaintext with a key that is as long as the message itself. Each bit of the plaintext is XORed (exclusive OR) with a bit from a random key that is used only once.

How It Works:
  • Key Length: The key must be as long as the plaintext.
  • Key Uniqueness: The key should be truly random and used only once.
  • Encryption: The plaintext is XORed with the key bit by bit.
    • Example:
      Plaintext: 101011
      Key:       110100
      Ciphertext: 011111
      
  • Decryption: The ciphertext is XORed with the same key to retrieve the original plaintext.
    • Example:
      Ciphertext: 011111
      Key:        110100
      Plaintext:  101011
      
Strengths:
  • Perfect Security: If the key is random, as long as the message, and never reused, the ciphertext provides no information about the plaintext.
  • Unbreakable: Without the key, it’s impossible to decrypt the ciphertext.
Weaknesses:
  • Key Distribution: Securely distributing and managing the key is a challenge.
  • Key Length: The key must be as long as the message, which can be impractical for large-scale communication.
  • Key Reuse: If the key is reused, security is compromised.

2. Block Cipher

A Block Cipher encrypts data in fixed-size blocks (usually 64 or 128 bits). The plaintext is divided into these blocks, and each block is encrypted separately using the same key. Some of the most well-known block ciphers include AES (Advanced Encryption Standard) and DES (Data Encryption Standard).

How It Works:
  • Block Size: A fixed block of plaintext (e.g., 128 bits) is processed at a time.
  • Encryption: Each block undergoes a series of transformations, such as substitution, permutation, and mixing, depending on the cipher (e.g., AES or DES).
  • Modes of Operation:
    • ECB (Electronic Codebook): Encrypts each block independently. It’s fast but insecure for repetitive patterns.
    • CBC (Cipher Block Chaining): XORs the previous ciphertext with the current plaintext before encryption, adding more security.
    • CTR (Counter Mode): Encrypts a counter value and XORs it with the plaintext block.
Strengths:
  • Efficiency: Block ciphers can handle large amounts of data and are highly efficient.
  • Versatility: Multiple modes of operation allow for different security levels.
  • Wide Use: Block ciphers like AES are widely used in securing data.
Weaknesses:
  • Fixed Block Size: The fixed block size can be inefficient if the data is not an exact multiple of the block size.
  • Vulnerability (e.g., ECB Mode): In certain modes like ECB, identical blocks of plaintext result in identical ciphertext blocks, which can reveal patterns.
Example (AES):

AES operates on 128-bit blocks and can use keys of 128, 192, or 256 bits. AES is often used in securing communications and data storage.


3. Stream Cipher

A Stream Cipher encrypts plaintext one bit or byte at a time, as opposed to a block cipher that encrypts in fixed-size blocks. Stream ciphers combine the plaintext with a keystream, which is typically generated from a key and a nonce (random number).

How It Works:
  • Keystream: The keystream is generated using a secret key and is combined with the plaintext using XOR.
  • Encryption: Each bit or byte of the plaintext is XORed with the keystream bit by bit.
    • Example:
      Plaintext: 101011
      Keystream: 110100
      Ciphertext: 011111
      
  • Decryption: The ciphertext is XORed with the same keystream to recover the plaintext.
    • Example:
      Ciphertext: 011111
      Keystream: 110100
      Plaintext: 101011
      
Strengths:
  • Speed: Stream ciphers tend to be faster and more efficient than block ciphers for continuous data streams.
  • Real-Time Processing: Suitable for environments where real-time encryption is needed (e.g., live communication).
  • Low Memory Usage: Stream ciphers typically require less memory since they process data bit-by-bit.
Weaknesses:
  • Key Management: If the keystream is not random or is reused, the cipher can be easily broken.
  • Synchronization: Decryption requires the same keystream, so both sender and receiver must stay in sync.
Example (RC4):

RC4 is one of the most well-known stream ciphers. However, it’s now considered insecure for modern use due to vulnerabilities in its design.


Comparison Table

Feature One-Time Pad Block Cipher Stream Cipher
Key Length Equal to plaintext length Fixed length (128, 192, 256 bits) Typically shorter than plaintext
Encryption Mode XOR bitwise Substitution + permutation XOR with keystream
Security Theoretical perfect Depends on key size and mode Depends on keystream security
Efficiency Low (key management) High (efficient for large data) High (fast, real-time)
Key Reuse No (key must be unique) Yes (key can be reused) No (keystream must be unique)
Common Use Cases High-security applications (e.g., diplomatic communication) Data encryption (e.g., AES, DES) Real-time communication (e.g., streaming)

Conclusion:

  • The One-Time Pad is the most secure encryption method but impractical for most real-world applications due to its key distribution and management challenges.
  • Block Ciphers offer high efficiency and security but require careful handling of key management and modes of operation.
  • Stream Ciphers are great for real-time data encryption but require secure keystream generation and synchronization.
⚠️ **GitHub.com Fallback** ⚠️