1 of 27

Cryptography

Day 0 - Digital System Representations

2 of 27

Today’s Schedule

10:30 - 11:00 → Binary, bits, bytes

11:10 - 11:45 → Intro to Crypto

-- Lunch --

1:00-2:00 Intro to Cryptography (cont)

2:00-3:30 One Time Pad (if there is time!)

3 of 27

System

  • Set of components that work together to achieve a certain task

Behavior

OR

Function

Inputs

Outputs

Input

Output

4 of 27

Why Binary?

  • Simplicity

Now we can pretty much represent anything by:

  1. Real-world signals → Binary
  2. Binary → Decimal Numbers?
  3. Binary → Characters?

+

_

+

_

1

0

1

0

5 of 27

Bit & Byte

  • A bit is the smallest unit of information
    • It represents one 2-way decision or a 2-way choice
    • yes / no, true / false, on / off …
  • Typical value: either 0 or 1
  • All information in a computer is stored and processed as bits
  • A byte is 8 bits that are treated as a unit

0

0

1

0

0

1

0

1

Bits

Byte

6 of 27

Bitwise Operators

AND (&)

OR (|)

XOR (^)

NOT (~)

1 & 1 = 1

1 & 0 = 0

0 & 1 = 0

0 & 0 = 0

1 | 1 = 1

1 | 0 = 1

0 | 1 = 1

0 | 0 = 0

1 ^ 1 = 0

1 ^ 0 = 1

0 ^ 1 = 1

0 ^ 0 = 0

~ 1 = 0

~ 0 = 1

7 of 27

Bitwise Operations

AND (&) 0 0 1 1� 1 0 1 0� ------------------------

0 0 1 0

OR (|) 0 0 1 1� 1 0 1 0� ------------------------

1 0 1 1

XOR (^) 0 0 1 1� 1 0 1 0� ------------------------

1 0 0 1

NOT (~) 0 1 � -----------

1 0

8 of 27

What does this byte represent?

0

0

1

0

0

1

0

1

9 of 27

Decimal Numbers Review

  • Deci == 10: Decimal is a Base-10 system
    • Base-10: group of 10

10 of 27

Decimal Numbers Review

  • Deci == 10: Decimal is a Base-10 system
    • Base-10: position represents a power of 10
    • Each number to the right of another, is ~10 bigger

4 4 4, 4 4 4, 4 4 4

* 10 8

* 10 7

* 10 6

* 10 5

* 10 4

* 10 3

* 10 2

* 10 1

* 10 0

11 of 27

Base-10 Decimal System

  • Decimal numbers are sums of powers of 10
  • Formula:
    • (Digit * 10^n) + …. + (Digit * 10^3) + (Digit * 10^2) + (Digit + 10^1) + (Digit + 10^0)
  • 1234?

1 thousands + 2 hundreds + 3 tens + 4 ones

1 * 1000 + 2 * 100 + 3 * 10 + 4 * 1

1 * 103 + 2 * 102 + 3 * 101 + 4 * 100

12 of 27

Base-2 Binary Number System

  • Base-2 → What does this mean?
    • Groups of 2!

1

10

11

111

1111

24 23 22 21 20

13 of 27

Base-2 Binary Number System

  • Base-2 → powers of 2

  • 1 0 1 2 → (1 * 2 2 ) + (0 * 2 1 ) + (1 * 2 0 ) == 510
  • 2210

→ 16 + 4 + 2 → (1 * 16) + (0 * 8) + (1 * 4) + (1 * 2) + (0 * 1)

→ (1 * 24 ) + (0 * 23 ) + (1 * 22 ) + (1 * 21 ) + (0 * 20 )

22101 0 1 1 0 2

14 of 27

Convert from Binary to Decimal

� �1 0 1 1

1 * 23 + 0 * 22 + 1 * 21 + 1 * 20

1 * 8 + 0 * 4 + 1 * 2 + 1 * 1

8 + 0 + 2 + 1

11

15 of 27

Convert from Decimal to Binary

11

__ * 23 + __ * 22 + __* 21 + __ * 20

1 * 23 + 0 * 22 + 1 * 21 + 1 * 20

1 0 1 1

16 of 27

Decimal → Binary

  1. Start with a number (n)
  2. Divide n by 2
  3. Remainders: 0/1 → append to Binary number on the left side
  4. Quotient → new ‘n’
  5. Continue from Step 2 until quotient becomes zero
  6. Final result is your binary number!

17 of 27

Decimal → Binary

  • Convert n = 97 to binary

---------------------------------------------------------------------------------�n ÷ 2 quotient remainder num�---------------------------------------------------------------------------------

97 ÷ 2 48 1 1

48 ÷ 2 24 0 01

24 ÷ 2 12 0 001

12 ÷ 2 6 0 0001

6 ÷ 2 3 0 00001

3 ÷ 2 1 1 100001

1 ÷ 2 0 (Stop) 1 1100001

Rules:

  1. Start with a number (n)
  2. Divide n by 2
  3. Remainders: 0/1 → append to Binary number on the left side
  4. Quotient → new ‘n’
  5. Continue from Step 2 until quotient becomes zero
  6. Final result is your binary number!

18 of 27

Decimal → Binary

  • Convert n = 97 to binary

------------------------------------------------------------------�n ÷ 2 quotient remainder�------------------------------------------------------------------

97 ÷ 2 48 1

48 ÷ 2 24 0

24 ÷ 2 12 0

12 ÷ 2 6 0

6 ÷ 2 3 0

3 ÷ 2 1 1

1 ÷ 2 0 (Stop) 1

Binary number: 1 1 0 0 0 0 1

Most Significant Bit (MSB)

Least Significant Bit (LSB)

19 of 27

Summary

Binary → Language of Computers

Decimal → Language of Humans

  • Base conversions to communicate back and forth with computers
    • Decimal → Binary
    • Binary → Decimal

20 of 27

Breakout Rooms:

Convert 73 into binary

Convert 11011010 into decimal

21 of 27

How do we represent “Codebreakers”?

22 of 27

Characters

  • How could I represent the text “Codebreakers” with in a digital system?

Sequencing with integers [a→ 0, b→ 1, c→ 2, …., z→ 25]

C: 2, o: 14, d: 3, e: 4, �b: 1, r: 17, e: 4, a: 0, k: 10, e: 4, r: 17, s: 18

23 of 27

ASCII

  • American Standard Code for Information Interchange
  • Number representation of a character
  • Arbitrary but agreed upon representation in US and rest of the world

24 of 27

Character → ASCII

Note that UPPER and lower letters are considered to be different characters

What’s the ASCII representation of

H E L L O ?

72 69 76 76 79 63

25 of 27

ASCII → Character

Note that UPPER and lower letters are considered to be different characters

What does this spell?

72 69 76 76 79 33

H E L L O !

26 of 27

Character <=> Binary?

27 of 27

New Functions! ord() and chr()

ord(<character>)

Takes in a single character input and returns its decimal ASCII code

>>> ord(‘Q’)

81

chr(<integer>)

Reverses ord(), gives you a character for a decimal input

>>> chr(81)

‘Q’