RC 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

en_US

Samsung Research

SSTF 2022|Hacker’s Playground

Tutorial Guide
RC four
Crypto
Two kinds of encryption

Symmetric Encryption Asymmetric Encryption

One shared key for Mathematically coupled


Key
encryption and decryption public key and private key

Typical Key Size 128~256 bits 1024~3072 bits (for RSA)

Low, because it’s a complex


Performance High
mathematical computation

Main Purpose Data Encryption Digital Signature/Certificate

Representative
DES, AES, RC4 RSA, DSA, ECC
Algorithms

2
Two kinds of encryptions
Shared Key

Hacker’s tM5#c*q3y Hacker’s


Symmetric VUzw^8Xn
Playground Playground
Encryption WfU6AcwL
Encryption Decryption
Plain Text Cipher Text Plain Text

One key used for both encryption and decryption

Key Pair

Asymmetric Hacker’s Se@K8E1$a Hacker’s


Playground kez8cCaMv Playground
Encryption cg1@FCiVq
Encryption Decryption
Plain Text Cipher Text Plain Text

Key Pair consisting of encryption key and decryption key 3


RC4 (a.k.a ARC4)
A representative stream cipher
▪ Stream cipher is a branch of symmetric key cipher.
▪ XOR-based common encryption/decryption processing

Working
Secret Key Plain text Cipher text

Key Key
Keystream Generator XOR XOR
Stream Stream

Key Stream Cipher text Plain text

Step 1. Key stream generation Step 2-1. Encryption Step 2-2. Decryption

4
5
6
Quiz #1

Simple python code


KeyStream_From_RC4 ="<y4)ky&=zuw(8*#3*<q4Quw)o+"
RC4_CipherText ="k6cv36tb1<9ogcplby#qpT"
Can you get the plaintext?
Download the source code HERE.

Try it before you see the solution.

7
Solution for Quiz #1

It’s quite simple. To decrypt the RC4 ciphertext, just XOR it with the key stream.

And you did it!

Bytewise XORing of ciphertext and 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.

Download the source code HERE.

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

Bytewise XORing of ciphertext and key stream

Try XORing for every possible case

We got a meaningful sentence among them.

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

Ciphertext1 Key Stream flag


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!

Bytewise XORing of ct1, msg, and ct2

17

You might also like