



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Today ...
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
Q: Can we use shift encrypt to decrypt the message?
Write a shift decrypt(message, shift) function
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
def shift_decrypt(message, shift): return shift_encrypt(message, 95 - shift)
Write a railfence encrypt(message) function
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