Python Cipher
Ciphers with Python
Ciphers with Python
What is a Cipher?
What is a Cipher?
A secret or disguised way of writing
A secret or disguised way of writing
Goal:
Goal:
The goal of this project is to create a program that can encode a message. It should also be able to take an encoded message and convert it back to plain English.
The goal of this project is to create a program that can encode a message. It should also be able to take an encoded message and convert it back to plain English.
Pseudo-code for Cipher
Pseudo-code for Cipher
1. Get a message from the user that will be encoded.
1. Get a message from the user that will be encoded.
2. Set a key value which will be used to shift the letters.
2. Set a key value which will be used to shift the letters.
3. For each letter in the message
3. For each letter in the message
Convert the letter to ASCII
Convert the letter to ASCII
Add the key value
Add the key value
Convert back to letter
Convert back to letter
Print letter
Print letter
Programming a Cipher
Programming a Cipher
key = 13
message = input("What is your message?")
for i in message:
ascii_value = ord(i)
ascii_value = ascii_value + key
new_letter = chr(ascii_value)
print(new_letter, end = "")
Pseudo-code for Decoder
Pseudo-code for Decoder
1. Get a message from the user that will be encoded.
1. Get a message from the user that will be encoded.
2. Set a key value which will be used to shift the letters.
2. Set a key value which will be used to shift the letters.
3. For each letter in the message
3. For each letter in the message
Convert the letter to ASCII
Convert the letter to ASCII
Subtract the key value
Subtract the key value
Convert back to letter
Convert back to letter
Print letter
Print letter
Programming a Decoder
Programming a Decoder
decode = ""
for j in range(1,27):
for i in decode:
ascii_value = ord(i)
ascii_value = ascii_value - j
new_letter = chr(ascii_value)
print(new_letter, end = "")
print()
Some bugs to fix
Some bugs to fix
Final Product
Final Product
##Cipher
from random import randint
key = randint(1,27)
message = input("What is your message?")
for i in message:
ascii_value = ord(i)
if (ascii_value >= 65 and ascii_value <= 90) or (ascii_value >= 97 and ascii_value <= 122):
if ascii_value + key > 90 and ascii_value <= 90:
ascii_value = (ascii_value + key - 90) + 64
elif ascii_value + key > 122:
ascii_value = (ascii_value + key - 122) + 96
else:
ascii_value = ascii_value + key
new_letter = chr(ascii_value)
print(new_letter, end = "")
else:
print(i , end = "")
def decode(message):
for j in range(1, 27):
key = j
for i in message:
ascii_value = ord(i)
if (ascii_value >= 65 and ascii_value <= 90) or (ascii_value >= 97 and ascii_value <= 122):
if ascii_value - key < 65:
ascii_value = 91 - (65 - (ascii_value - key))
elif ascii_value - key < 97 and ascii_value >= 97:
ascii_value = 123 - (97 - (ascii_value - key))
else:
ascii_value = ascii_value - key
new_letter = chr(ascii_value)
print(new_letter, end = "")
else:
print(i , end = "")
print()