RC 4
RC 4
RC 4
Samsung Research
Tutorial Guide
RC four
Crypto
Two kinds of encryption
Representative
DES, AES, RC4 RSA, DSA, ECC
Algorithms
2
Two kinds of encryptions
Shared Key
Key Pair
Working
Secret Key Plain text Cipher text
Key Key
Keystream Generator XOR XOR
Stream Stream
Step 1. Key stream generation Step 2-1. Encryption Step 2-2. Decryption
4
5
6
Quiz #1
7
Solution for Quiz #1
It’s quite simple. To decrypt the RC4 ciphertext, just XOR it with the key stream.
8
9
Quiz #2
from Crypto.Cipher import ARC4 ARC4 module generates key
from binascii import hexlify
from secret import key, flag stream and XORs with the input.
def encrypt(data): RC4 ciphertext is given,
return ARC4.new(key).encrypt(data)
but key is not known.
ct = b""
for ch in flag:
ct += encrypt(ch)
Can you get the plaintext?
print("Ciphertext = ", hexlify(ct).decode()) Try it before you see the solution.
'''
$ python3 challenge.py
HINT: You may need a little bit
Ciphertext = 6f47474c06086f47085c47085c404d08464d505c085b5c494f4d09
'''
brute-forcing.
10
Solution for Quiz #2
from Crypto.Cipher import ARC4
According to the source code…
from binascii import hexlify
The flag is not encrypted at once.
from secret import key, flag
It’s split for each byte, encrypted,
def encrypt(data):
return ARC4.new(key).encrypt(data) and put back together.
ct = b""
for ch in flag: Each letter of the flag is XORed with
ct += encrypt(ch)
the first byte of the key stream.
print("Ciphertext = ", hexlify(ct).decode())
Only one byte of the key stream is used.
'''
$ python3 challenge.py The entire flag data can be recovered
Ciphertext = 6f47474c06086f47085c47085c404d08464d505c085b5c494f4d09
''' by finding the value of the one byte.
11
Solution for Quiz #2
Try every possible case.
1 byte is group of 8 bits, so there can be 28 = 256 cases
12
Let’s practice
13
Challenge Definition
There are
▪ one key,
unknown
▪ two plaintexts
flag: unknown
msg: known
▪ and two ciphertexts
both of them are known
Ciphertext1
Ciphertext2
14
Let’s see
key✓unknown
Keystream Generator
msg
✓known flag✓unknown
Key Stream
✓unknown
XOR XOR
Ciphertext1
✓known Ciphertext2
✓known
Can you see? We can find the flag, even without key!
▪ Because when a b = c, a c = b.
15
It’s an easy logic! XOR
msg Ciphertext2
✓ ✓
Step 1. We can recover the Key Stream from the known plaintext and ciphertext pair.
Step 2. We can recover the Ciphertext2 because now we know the Key Stream.
Step 3. Now we got the flag!! :)
16
Does it really work?
Yes, it does!
Give it a shot!
17