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:
- 0⊕0=0
- 0⊕1=1
- 1⊕0=1
- 1⊕1=0
- Key property: A⊕B⊕B=A (XORing twice with the same value gives the original value)
- Process:
- Generate a pseudorandom keystream from a secret key.
- XOR the keystream with the plaintext to get the ciphertext.
- 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=P⊕K
- Decryption: P=C⊕K=(P⊕K)⊕K=P
- Where:
- P = Plaintext
- C = Ciphertext
- K = 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
- Generate a truly random key the same length as the plaintext (or more).
- Convert plaintext and key to binary.
- XOR each bit of plaintext with the corresponding bit of the key.
- Result is the ciphertext.
- 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
- Convert plaintext to ASCII:
- "My Name is Ahmed" = [77, 121, 32, 78, 97, 109, 101, 32, 105, 115, 32, 65, 104, 109, 101, 100]
- XOR each byte with the corresponding key byte:
- 77⊕4B=92 (M ⊕ key)
- 121⊕7A=43 (y ⊕ key)
- 32⊕9F=AD ( ⊕ key)
- etc.
- 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:
- C1⊕C2=(P1⊕K)⊕(P2⊕K)=P1⊕P2