1 of 21

Cryptography!

Encryption, Decryption, better methods

2 of 21

XOR

XOR = exclusive OR

Building block for encryption - bit by bit operator

0 ⊕ 0 = 0

1 ⊕ 0 = 1

1 ⊕ 1 = 0

3 of 21

XOR

4 of 21

What is a One-Time Pad (OTP)?

  • An encryption scheme that solves some of the problems that we had with a Caesar Cipher
  • An unbreakable and simple way to encrypt a message
  • Very easy for a computer to compute
  • One of the uses for XOR

5 of 21

One Time Pad - Encryption

Key (secret, random): 0011100100010000101110010010110110100000111100110010101101100100

ASCII encoding: 0100001001000101010001010101000001000010010011110100111101010000

Message: BEEPBOOP

XOR

Ciphertext: 0111101101010101111111000111110111100010101111000110010000110100

0⊕0=0

0⊕1=1

1⊕0=1

1⊕1=0

Decoded ciphertext: {Uü}â¼d4

Without knowledge of the key, it is impossible for an adversary to extract the message!

6 of 21

One Time Pad - Decryption

Key (secret, random): 0011100100010000101110010010110110100000111100110010101101100100

ASCII encoding: 0111101101010101111111000111110111100010101111000110010000110100

Ciphertext: {Uü}â¼d4

XOR

Message: 0100001001000101010001010101000001000010010011110100111101010000

0⊕0=0

0⊕1=1

1⊕0=1

1⊕1=0

Decoded message: BEEPBOOP

Do the same operation to the ciphertext to get the message back.

7 of 21

Why can’t our message and key be the same?

8 of 21

Why can’t our message and key be the same?

Recap:

  • If message is the same as key, ciphertext will be all 0’s!
  • Eve will be able to know our message!

9 of 21

One Time Pad Restrictions

  • The key is as long as the message
  • The key is pseudorandom*
  • Only use the key ONCE
  • OTP is very easy for a computer to compute using XOR

* Randomness will be covered tomorrow

10 of 21

One Time Pad

Notice that without the key it is actually impossible for the adversary to directly read the message! If you have the ciphertext below, two different keys will get you two different messages:

EVWSYW OFZ PQWR TP CVIQ EVWSYW OFZ PQWR TP CVIQ

$”#2:< ;.? 88;> 5$ ‘7>? $”#2:< ;.? 20%< 5$ -9&?

attack the hill at dawn attack the barn at noon

key1

key2

11 of 21

One Time Pad

Notice that without the key it is actually impossible for the adversary to directly read the message! If you have the ciphertext below, two different keys will get you two different messages:

EVWSYW OFZ PQWR TP CVIQ EVWSYW OFZ PQWR TP CVIQ

ECDSWM VYV IILG TW ZVMD ECDSWM VYV OQFE TW PHUD

attack the hill at dawn attack the barn at noon

Shifts never fall into repetitive pattern!

https://www.youtube.com/watch?v=FlIG3TvQCBQ

12 of 21

About OTP

  • Easy to implement and compute (encryption and decryption are the same operation!)
  • Computer can easily compute XOR
  • Unbreakable if the key is truly random and not known by the attacker
  • The only cipher that has perfect confidentiality

Any problems with OTP?

13 of 21

Problems with the OTP

Key is as long as the message - impractical when you have a lot of messages or a very long message.

Key can only be used once

Bits of the message correspond directly to bits of the ciphertext

Don’t know if a message is legitimate before decoding it

In order to share a secret message, you must first share a secret key. How do you share this key in the first place?

14 of 21

Problems with the OTP

Key is as long as the message - impractical when you have a lot of messages or a very long message.

Key can only be used once

Bits of the message correspond directly to bits of the ciphertext

Don’t know if a message is legitimate before decoding it

In order to share a secret message, you must first share a secret key. How do you share this key in the first place?

15 of 21

Simple message gets very long!

16 of 21

Can we break this the same way we did Caesar?

  • Brute force?
  • Frequency analysis?
  • What else?

17 of 21

Integrity of One Time Pad

18 of 21

Integrity of One Time Pad

  • It is highly vulnerable to tampering
  • Alice is going to send Bob either 1 or 0 using an OTP
    • The adversary can just flip the bit in the ciphertext
  • Alice wants to withdraw $1000 from the bank using an OTP
    • The adversary can just flip the bit again so Alice will never gets the right amount

19 of 21

Problems with the OTP

In order to share a secret message, you must first share a secret key. How do you share this key in the first place?

Key must be random.

Don’t know if a message is legitimate before decoding it.

Key can only be used once.

20 of 21

Try it!

Write the functions in OTP_Exercise.py so that you can do the following:

>>> key = get_key(16)�>>> message = ‘sixteen-byte msg’�>>> ciphertext = encrypt(message, key)�>>> ciphertext�b'\x1f\xdeE4\x12&6\x9aSmNZ\xaf\x97\xb1\xc2' #your results will differ�>>> decrypt(ciphertext, key)�‘sixteen-byte msg’

21 of 21

Tomorrow....

  • We’ll learn how to exploit a reused OTP