Challenge 1:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
message = input("Enter your message: ").lower()
digit_in_msg = True
def has_digit(a):
for i in a:
if i.isdigit():
return True
digit_in_msg = has_digit(message)
while digit_in_msg == True:
print("Input should only contain alphabets")
message = input("Enter your message again: ").lower()
digit_in_msg = has_digit(message)
def encrypt_alpha(msg):
new_code = ""
for i in msg:
if i == " ":
new_code += " "
else:
code = alphabet.index(i) - 1
new_code += alphabet[code]
print(new_code)
encrypt_alpha(message)
Challenge 2:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
message = input("Enter your message: ").lower()
def check_input(a):
for i in a:
if i == " ":
is_valid = True
elif i.isalnum():
is_valid = True
else:
is_valid = False
return is_valid
is_valid_input = check_input(message)
while is_valid_input == False:
print("Input should be only alphabets and numbers")
message = input("Enter your message again: ").lower()
is_valid_input = check_input(message)
def encrypt_alpha(msg):
new_code = ""
for i in msg:
if i == " ":
new_code += " "
elif i.isdigit():
num_index = numbers.index(i) - 1
new_code += numbers[num_index]
else:
alpha_code = alphabet.index(i) - 1
new_code += alphabet[alpha_code]
print(new_code)
encrypt_alpha(message)
Challenge 3:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
special_char = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "|", "\\", "{", "[", "]", "}", ";", ":", "<", ">", ",", ".", "?", "/"]
message = input("Enter your message: ").lower()
def encrypt_message(msg):
new_code = ""
for i in msg:
if i == " ":
new_code += " "
elif i.isdigit():
num_index = numbers.index(i) - 1
new_code += numbers[num_index]
elif i in special_char:
char_index = special_char.index(i) - 1
new_code += special_char[char_index]
else:
alpha_code = alphabet.index(i) - 1
new_code += alphabet[alpha_code]
return new_code
print("Your encrypted message is:",encrypt_message(message))
Challenge 4:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
special_char = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "|", "\\", "{", "[", "]", "}", ";", ":", "<", ">", ",", ".", "?", "/"]
message = input("Enter your message: ").lower()
shift = 0
new_word = ''
my_list = []
new_string=''
empty_str = ''
for i in message:
if i == ' ':
my_list.append(empty_str)
empty_str = ''
else:
empty_str += i
if empty_str:
my_list.append(empty_str)
print("Your input was:",message)
for words in my_list:
shift += 1
for w in words:
if w.isalpha():
new_word += alphabet[int(alphabet.index(w) - shift)]
elif w.isdigit():
new_word += numbers[int(numbers.index(w) - shift)]
elif w in special_char:
new_word += special_char[int(special_char.index(w) - shift)]
new_string += new_word+ ' '
new_word = ''
print("Your encrypted message is:",new_string)
Challenge 5:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
special_char = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "|", "\\", "{", "[", "]", "}", ";", ":", "<", ">", ",", ".", "?", "/"]
message = input("Enter your message to decrypt: ").lower()
shift = 1
def decrypt_message(msg):
new_code = ""
for i in msg:
if i == " ":
new_code += " "
elif i.isdigit():
num_index = numbers.index(i)
if num_index >= 9:
num_index = 0
else:
num_index = num_index + shift
new_code += numbers[num_index]
elif i in special_char:
spl_index = special_char.index(i)
if spl_index >= 27:
spl_index = 0
else:
spl_index = spl_index + shift
new_code += special_char[spl_index]
else:
alpha_index = alphabet.index(i)
if alpha_index >=25:
alpha_index = 0
else:
alpha_index = alpha_index + shift
new_code += alphabet[alpha_index]
return new_code
print("Your input was:",message,"\nYour decrypted message is:",decrypt_message(message))
Challenge 6:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
special_char = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "|", "\\", "{", "[", "]", "}", ";", ":", "<", ">", ",", ".", "?", "/"]
mode_type = int(input("Please enter 1 to encrypt or 2 to decrypt "))
while (mode_type != 1 and mode_type != 2):
print("Input should be 1 or 2")
mode_type = int(input("Try again. Enter 1 to encrypt or 2 to decrypt: "))
message = input("Enter your message: ").lower()
if mode_type == 1:
shift = -1
elif mode_type == 2:
shift = 1
def process_message(msg):
new_code = ""
for i in msg:
if i == " ":
new_code += " "
elif i.isdigit():
num_index = numbers.index(i)
if num_index >= 9:
num_index = 0
else:
num_index = num_index + shift
new_code += numbers[num_index]
elif i in special_char:
spl_index = special_char.index(i)
if spl_index >= 27:
spl_index = 0
else:
spl_index = spl_index + shift
new_code += special_char[spl_index]
else:
alpha_index = alphabet.index(i)
if alpha_index >=25:
alpha_index = 0
else:
alpha_index = alpha_index + shift
new_code += alphabet[alpha_index]
return new_code
new_string = process_message(message)
if mode_type ==1:
print("Your message was: ", message,"\nYour encrypted message is:", new_string)
elif mode_type ==2:
print("Your message was: ", message,"\nYour decrypted message is:", new_string)