Cryptography Notes

Cryptography

Stream Ciphers

Symmetric Key Cryptography
  • Involves using the same key for both encryption and decryption.
  • Requires both parties to share a secret key.
  • Offers fast and efficient encryption for large data.
Types of Symmetric Ciphers
  • Block Ciphers:
    • Encrypt fixed-length blocks of data (typically 64 or 128 bits).
    • Examples: AES, DES, 3DES.
    • Process data in chunks.
    • More complex operation modes.
    • Generally more resistant to attacks.
  • Stream Ciphers:
    • Encrypt one bit or byte at a time.
    • Generate a keystream and XOR with plaintext.
    • Examples: RC4, A5/1, ChaCha20.
    • Process data bit by bit.
    • Simpler implementation.
    • Often faster and require fewer resources.
    • Well-suited for real-time communications.
Stream Cipher Operation: XOR
  • The core operation of stream ciphers is the XOR (exclusive OR) operation.
  • Truth table:
    • 00=00 \oplus 0 = 0
    • 01=10 \oplus 1 = 1
    • 10=11 \oplus 0 = 1
    • 11=01 \oplus 1 = 0
  • Key property: ABB=AA \oplus B \oplus B = A (XORing twice with the same value gives the original value)
  • Process:
    1. Generate a pseudorandom keystream from a secret key.
    2. XOR the keystream with the plaintext to get the ciphertext.
    3. Use the same keystream and XOR with the ciphertext to recover the plaintext.
  • Security depends on the quality of the keystream generator.
Stream Cipher Operations
  • Encryption: C=PKC = P \oplus K
  • Decryption: P=CK=(PK)K=PP = C \oplus K = (P \oplus K) \oplus K = P
  • Where:
    • PP = Plaintext
    • CC = Ciphertext
    • KK = Keystream
One-Time Pad
  • The simplest and only provably secure cipher.
  • Uses a truly random key that is:
    • As long as the plaintext.
    • Used only once.
    • Kept completely secret.
  • Provides perfect secrecy - unbreakable even with infinite computing power.
One-Time Pad Algorithm
  1. Generate a truly random key the same length as the plaintext (or more).
  2. Convert plaintext and key to binary.
  3. XOR each bit of plaintext with the corresponding bit of the key.
  4. Result is the ciphertext.
  5. Decryption: XOR the ciphertext with the same key.
One-Time Pad Example
  • Encrypt the message "My Name is Ahmed" using One-Time Pad.
  • Given:
    • Plaintext: "My Name is Ahmed"
    • Key (in hex): 4B 7A 9F 2E 5D 8C 1A 3B 7C 9D 0E 4F 6A 2C
One-Time Pad Example: Solution
  1. Convert plaintext to ASCII:
    • "My Name is Ahmed" = [77, 121, 32, 78, 97, 109, 101, 32, 105, 115, 32, 65, 104, 109, 101, 100]
  2. XOR each byte with the corresponding key byte:
    • 774B=9277 \oplus 4B = 92 (M \oplus key)
    • 1217A=43121 \oplus 7A = 43 (y \oplus key)
    • 329F=AD32 \oplus 9F = AD ( \oplus key)
    • etc.
  3. Resulting ciphertext (in hex): 5C 2B AD 56 CA 85 6B 09 79 28 3D 2A 0E 85 6B 28
  • To decrypt, simply XOR the ciphertext with the same key.
Limitations of One-Time Pad
  • Requires a key as long as the message.
  • Key distribution problem.
  • Key must never be reused; if the same key is used twice, security is completely compromised:
    • C1C2=(P1K)(P2K)=P1P2C₁ \oplus C₂ = (P₁ \oplus K) \oplus (P₂ \oplus K) = P₁ \oplus P₂