def caesar_encode(text, shift):
result = ''
for char in text:
if char.isalpha():
#assign values to plaintext using ascii
start = ord('a') if char.islower() else ord('A')
#determine ascii value of encrypted text
shifted_char = chr((ord(char) - start + shift) % 26 + start)
else:
shifted_char = char
#append to result
result += shifted_char
return result
def caesar_decode(text, shift):
result = ''
for char in text:
if char.isalpha():
start = ord('a') if char.islower() else ord('A')
shifted_char = chr((ord(char) - start - shift) % 26 + start)
else:
shifted_char = char
result += shifted_char
return result
def vignere_encode(text, key):
result = ''
key = key.upper()
key_index = 0
#loop iterates through each character in text
for i, char in enumerate(text):
if char.isalpha():
start = ord('a') if char.islower() else ord('A')
shift = ord(key[key_index % len(key)]) - ord('A')
shifted_char = chr((ord(char) - start + shift) % 26 + start)
key_index += 1
else:
shifted_char = char
result += shifted_char
return result
def vignere_decode(text, key):
result = ''
key = key.upper()
key_index = 0
for i, char in enumerate(text):
if char.isalpha():
start = ord('a') if char.islower() else ord('A')
shift = ord(key[key_index % len(key)]) - ord('A')
shifted_char = chr((ord(char) - start - shift) % 26 + start)
key_index += 1
else:
shifted_char = char
result += shifted_char
return result
mode2= ""
mode = input("Choose mode (caesar/vignere): ")
mode3 = input("encode/decode:")
text = input("Enter text: ")
if mode3 == "decode":
if mode == 'caesar' or mode == 'vignere':
if mode == 'caesar':
mode2 = input("Known shift?(y/n)")
if mode2 == 'y':
shift = int(input("Enter shift amount: "))
decrypted_text = caesar_decode(text, shift)
print("Decrypted text:", decrypted_text)
elif mode2 == 'n':
decoded_array = []
for x in range(27):
decrypted_text=caesar_decode(text, x)
print("\nPossible decrypted text:",decrypted_text)
decoded_array.append(decrypted_text)
print("\ncondensed:\n", decoded_array)
else:
print("Invalid input. Please enter 'y' or 'n'.")
exit()
elif mode == 'vignere':
key = input("Enter the key: ")
decrypted_text = vignere_decode(text, key)
print("Decrypted text:", decrypted_text)
elif mode3 == "encode":
if mode == 'caesar':
shift = int(input("Enter shift amount: "))
encrypted_text = caesar_encode(text, shift)
print("Encrypted text:", encrypted_text)
elif mode == 'vignere':
key = input("Enter the key: ")
encrypted_text = vignere_encode(text, key)
print("Encrypted text:", encrypted_text)
else:
print("Invalid mode.")
exit()