Lecture Notes on Basic Cryptography for CPSC 121 (Fall 2011), Study notes of Computer Science

Lecture notes on basic cryptography concepts, including encryption by shifting and transposition (rail fence), as taught in the cpsc 121 (fall 2011) course. The notes include explanations, examples, and exercises.

Typology: Study notes

2012/2013

Uploaded on 09/28/2013

noob
noob 🇮🇳

4.4

(25)

105 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture Notes CPSC 121 (Fall 2011)
Today ...
Quiz 9
Basic cryptography (cont.)
S. Bowers 1 of 7
pf3
pf4
pf5

Partial preview of the text

Download Lecture Notes on Basic Cryptography for CPSC 121 (Fall 2011) and more Study notes Computer Science in PDF only on Docsity!

Today ...

  • Quiz 9
  • Basic cryptography (cont.)

Basic Cryptography Terms

A plaintext message is unencrypted (readable)

A ciphertext message is encrypted (unreadable)

Encryption converts plaintext to ciphertext

Decryption converts ciphertext to plaintext

def shift_encrypt(message, shift): cipher = "" for c in message: num = ord(c) if 32 <= num <= 126: if num + shift > 126: cipher += chr(31 + num + shift - 126) else: cipher += chr(num + shift) else: cipher += c return cipher

  • Note we shift from 31 instead of 32
    • e.g., so that 126 ("~") + 1 gives 32 (" ")

Exercise

Q: Can we use shift encrypt to decrypt the message?

  • No ... unless shift = 79

Write a shift decrypt(message, shift) function

  • Takes a ciphertext message and a shift number, eturns plaintext message

def shift_decrypt(message, shift): text = "" for ch in message: num = ord(ch) if 32 <= num <= 126: if num - shift < 32: text += chr(127 - (32 - (num - shift))) else: text += chr(num - shift) else: text += ch return text

  • You can also cheat ...

def shift_decrypt(message, shift): return shift_encrypt(message, 95 - shift)

  • where shifting by 95 shifts to itself
  • all assume shift ≤ 95 (get around this by shift = shift % 95)

Exercise

Write a railfence encrypt(message) function

  • Hint: Use a range loop over the length of the message
  • You’ll need to also use the subscript operator on message

def railfence_encrypt(message): cipher1 = "" cipher2 = "" for i in range(len(message)): if i % 2 == 0: cipher1 += message[i] else: cipher2 += message[i] return cipher1 + cipher

  • Note that it is easy to use more rails ...