Introduction to Cryptography

August 30, 2022

Introduction to Cryptography

What is Cryptography?

A sender (traditionally called Alice) would like to send a message to a receiver (traditionally, Bob).

However, while the message is being sent, it can be observed by an eavesdropper (Eve), and Bob and Alice would like to hide it’s meaning from Eve.

Alice, Bob, Eve

For example, Alice wants to send the following message:

mysnapchatusernameisramblingscrambler

Instead, she sends the message:

oaupcrejcvwugtpcogkutcodnkpiuetcodngt

Discuss. Eve observes this communication, and figures out their encryption scheme. The next message sent is qnffqi. What is the decrypted message? What is Alice and Bob’s scheme?

What you need What you will learn

  • cleverness
  • algorithms
  • mathematics
  • R

What is it good for?

  • Privacy
  • E-Commerce
  • Digital rights management
  • Military/Espionage
  • Crypto-currencies
  • Authentication
  • Certification
  • Data integrity

Is cryptography different from coding theory?

Coding Theory deals with how we represent information digitally.

  • Error-correcting codes
  • Compression
  • Data transmission
  • Cryptographic coding (i.e., cryptography)

Syllabus (PDF)

Assumptions and Terminology

Kerckhoff’s Principle

Assumption: Eve always knows the method Alice and Bob are using.

  • So we can have universal, open standards.
  • Not security through obscurity.

Terminology

  • Plaintext: The message that Alice would like to send to Bob. (olddog)
  • Ciphertext: The encrypted message that Alice sends (qnffqi)
  • Key: A piece of information, which, combined with the method, allows a ciphertext to be decrypted.

Example: A shift cipher

Example. The method described above is called a shift cipher (or Caesar cipher).

  • Plaintext: olddog
  • Ciphertext: qnffqi
  • Key: 2 (how much to shift)

Question: How do you encrypt the plaintext fuzzy?

The modulo equivalence relation

We write \[a \equiv b \pmod n\] to denote that \(a\) and \(b\) belong to the same equivalence class modulo \(n\); that is, \((a-b)\) is a multiple of \(n\).

The modulo operator

We write \(n \bmod m\) to represent the remainder when \(n\) is divided by \(m\). For example, in R, we compute \(38 \bmod 6\) as follows.

38 %% 6
[1] 2

Since \(38 \bmod 6 = 2\), \(38\) and \(2\) are in the same equivalence class modulo \(6\), which we denote as \[38 \equiv 2 \pmod 6\] or equivalently, as \[2 \equiv 38 \pmod 6\]

Modular arithmetic

Theorem. If \(a \equiv b \pmod n\) and \(c \equiv d \pmod n\), then:

  • \(a+c \equiv b+d \pmod n\)
  • \(ac \equiv bd \pmod n\)

In other words, when you do arithmetic modulo \(n\), it doesn’t matter which equivalence class representatives you use. In terms of operators, you can apply the \(\bmod\) operator before or after adding and multiplying, and you will obtain the same result. (Example.)

Group Exercise: Modular arithmetic

Compute each expression modulo 7, without using technology.

  1. \((32 + 76)(144 - 7000) \bmod 7\)

  2. \(23^{100} \bmod 7\)

(Take a look at this problem in RStudio.)

Modular arithmetic in R

What goes wrong here?

23^100 %% 7 # wrong!
Warning: probable complete loss of accuracy in modulus
[1] 0

Big integer modular arthmetic in R

library(gmp) # load library for multiple precision arithmetic
a <- as.bigz(23)^100
a
Big Integer ('bigz') :
[1] 14886191506363039393791556586559754231987119653801368686576988209222433278539331352152390143277346804233476592179447310859520222529876001
a %% 7
Big Integer ('bigz') :
[1] 2

Or better yet…

Instead of exponentiating and then modding, we can use the gmp::powm function, which is more efficient.

powm(23, 100, 7)
Big Integer ('bigz') :
[1] 2

Vectorization in R

  • Variables in R can hold a single value or a vector of values.
  • Most functions and operators are vectorized.
x <- c(23,45,67) # c() combines elements into a vector
x * 2
[1]  46  90 134
x - c(3, 5, 7)
[1] 20 40 60

Example: utf8ToInt

utf8ToInt("A")
[1] 65
utf8ToInt("APPLE")
[1] 65 80 80 76 69
intToUtf8(utf8ToInt("APPLE"))
[1] "APPLE"

Shift cipher implemented in R

stringToMod26 <- function(x) {utf8ToInt(x)-utf8ToInt("a")}
mod26ToString <- function(x) {intToUtf8(x+utf8ToInt("a"))}

shiftCipher <- function(p,b)
{
  pt <- stringToMod26(p)
  ct <- (pt + b) %% 26      # add b to each letter
  return(mod26ToString(ct))
}

shiftCipher("thisisasecretmessage", 2)
[1] "vjkukucugetgvoguucig"

Decrypting a shift cipher

shiftCipher("cubswin", 10)
[1] "melcgsx"
shiftCipher("melcgsx", -10)
[1] "cubswin"
shiftCipher("melcgsx", 16)
[1] "cubswin"

Attacks

Types of cryptographic attacks

  • ciphertext only
  • known plaintext
  • chosen plaintext
  • chosen ciphertext

Shift cipher: ciphertext only attack

In a ciphertext only attack, Eve has only the ciphertext and wishes to decrypt it and obtain the key.

  • Ciphertext only attacks are typically brute force.
  • For the shift cipher, just try all the shifts.

Loops in R

ciphertext <- "ufekwfixvkkfufkyvrjjzxedvekj"
for(i in 1:26) {
  print(shiftCipher(ciphertext, i))}
[1] "vgflxgjywllgvglzwskkayfewflk"
[1] "whgmyhkzxmmhwhmaxtllbzgfxgml"
[1] "xihnzilaynnixinbyummcahgyhnm"
[1] "yjioajmbzoojyjoczvnndbihzion"
[1] "zkjpbkncappkzkpdawooecjiajpo"
[1] "alkqclodbqqlalqebxppfdkjbkqp"
[1] "bmlrdmpecrrmbmrfcyqqgelkclrq"
[1] "cnmsenqfdssncnsgdzrrhfmldmsr"
[1] "dontforgettodotheassignments"
[1] "epougpshfuupepuifbttjhonfout"
[1] "fqpvhqtigvvqfqvjgcuukipogpvu"
[1] "grqwirujhwwrgrwkhdvvljqphqwv"
[1] "hsrxjsvkixxshsxliewwmkrqirxw"
[1] "itsyktwljyytitymjfxxnlsrjsyx"
[1] "jutzluxmkzzujuznkgyyomtsktzy"
[1] "kvuamvynlaavkvaolhzzpnutluaz"
[1] "lwvbnwzombbwlwbpmiaaqovumvba"
[1] "mxwcoxapnccxmxcqnjbbrpwvnwcb"
[1] "nyxdpybqoddynydrokccsqxwoxdc"
[1] "ozyeqzcrpeezozesplddtryxpyed"
[1] "pazfradsqffapaftqmeeuszyqzfe"
[1] "qbagsbetrggbqbgurnffvtazragf"
[1] "rcbhtcfushhcrchvsoggwubasbhg"
[1] "sdciudgvtiidsdiwtphhxvcbtcih"
[1] "tedjvehwujjetejxuqiiywdcudji"
[1] "ufekwfixvkkfufkyvrjjzxedvekj"

The R way: use sapply

sapply(1:26, function(x) {shiftCipher(ciphertext, x)})
 [1] "vgflxgjywllgvglzwskkayfewflk" "whgmyhkzxmmhwhmaxtllbzgfxgml"
 [3] "xihnzilaynnixinbyummcahgyhnm" "yjioajmbzoojyjoczvnndbihzion"
 [5] "zkjpbkncappkzkpdawooecjiajpo" "alkqclodbqqlalqebxppfdkjbkqp"
 [7] "bmlrdmpecrrmbmrfcyqqgelkclrq" "cnmsenqfdssncnsgdzrrhfmldmsr"
 [9] "dontforgettodotheassignments" "epougpshfuupepuifbttjhonfout"
[11] "fqpvhqtigvvqfqvjgcuukipogpvu" "grqwirujhwwrgrwkhdvvljqphqwv"
[13] "hsrxjsvkixxshsxliewwmkrqirxw" "itsyktwljyytitymjfxxnlsrjsyx"
[15] "jutzluxmkzzujuznkgyyomtsktzy" "kvuamvynlaavkvaolhzzpnutluaz"
[17] "lwvbnwzombbwlwbpmiaaqovumvba" "mxwcoxapnccxmxcqnjbbrpwvnwcb"
[19] "nyxdpybqoddynydrokccsqxwoxdc" "ozyeqzcrpeezozesplddtryxpyed"
[21] "pazfradsqffapaftqmeeuszyqzfe" "qbagsbetrggbqbgurnffvtazragf"
[23] "rcbhtcfushhcrchvsoggwubasbhg" "sdciudgvtiidsdiwtphhxvcbtcih"
[25] "tedjvehwujjetejxuqiiywdcudji" "ufekwfixvkkfufkyvrjjzxedvekj"

Shift cipher: known plaintext attack

In a known plaintext attack, Eve has a plaintext string along with the corresponding ciphertext, and wishes to recover the key.

  • Quite common to have both. e.g., messages begin with “Hello”.
plaintext <- "usecanvas"
ciphertext <- "camkivdia"
(stringToMod26(ciphertext)-stringToMod26(plaintext)) %% 26
[1] 8 8 8 8 8 8 8 8 8

Shift cipher: chosen plaintext attack

In a chosen plaintext attack, Eve temporarily gets access to the “encryption machine”, and encrypts a plaintext message of her choosing, observing the ciphertext.

key <- 7
stringToMod26(shiftCipher("a", key)) %% 26
[1] 7

Shift cipher: chosen ciphertext attack

In a chosen ciphertext attack, Eve temporarily gets access to the “decryption machine”, and decrypts a ciphertext message of her choosing, observing the plaintext.

key <- 7
-stringToMod26(shiftCipher("a", -key)) %% 26
[1] 7

Assignment: due Wednesday, 11:59 pm.

  • Install RStudio on your favorite computer. Install the library gmp and test some of the examples from these notes.
  • Complete the first written assignment. Upload a PDF scan of your work to Canvas.