Skip to content

Commit

Permalink
FIXED: Caesar decoder possible result
Browse files Browse the repository at this point in the history
  • Loading branch information
OfficialAhmed committed Oct 5, 2023
1 parent efb7907 commit 483b4ba
Showing 1 changed file with 47 additions and 65 deletions.
112 changes: 47 additions & 65 deletions Caesar Cipher Encoder & Decoder.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,67 @@
#PROJECT1
#CAESAR CIPHER DECODER
# PROJECT1
# CAESAR CIPHER ENCODER/DECODER

#Author: InTruder
#Cloned from: https://github.com/InTruder-Sec/caesar-cipher
# Author: InTruder
# Cloned from: https://github.com/InTruder-Sec/caesar-cipher

# Improved by: OfficialAhmed (https://github.com/OfficialAhmed)

def get_int() -> int:
"""
Get integer, otherwise redo
"""

try:
key = int(input("Enter number of characters you want to shift: "))
except:
print("Enter an integer")
key = get_int()

return key

def main():

print("[>] CAESAR CIPHER DECODER!!! \n")
print("[1] Encrypt\n[2] Decrypt")
try:
func = int(input("Choose one of the above(example for encode enter 1): "))
except:
print("\n[>] Invalid input")
exit()

if func == 2:
decode()
else:
if func == 1:
match input("Choose one of the above(example for encode enter 1): "):

case "1":
encode()
else:
print("\n[>] Invalid input")
exit()

case "2":
decode()

case _:
print("\n[>] Invalid input. Choose 1 or 2")
main()


def encode():
text = input("Enter text to encode: ")
key = input("Enter number of characters you want to shift: ")

encoded_cipher = ""
try:
key = int(key)
except:
print("Only intigers between 0 to 25 are allowed. Try again :)")
exit()
if key > 25:
print("Only intigers between 0 to 25 are allowed. Try again :)")
exit()
else:
key = key
text = text.upper()
text = input("Enter text to encode: ")
key = get_int()

for char in text:
ascii = ord(char)
if ascii > 90:
new_ascii = ascii
else:
if ascii < 65:
new_ascii = ascii
else:
new_ascii = ascii + key
if new_ascii > 90:
new_ascii = new_ascii - 26
else:
new_ascii = new_ascii
encoded = chr(new_ascii)
encoded_cipher = encoded_cipher + encoded
print("Encoded text: " + encoded_cipher)

ascii = ord(char) + key
encoded_cipher += chr(ascii)

print(f"Encoded text: {encoded_cipher}")


def decode():

decoded_cipher = ""
cipher = input("\n[>] Enter your cipher text: ")
print("Posiblities of cipher text are: \n")
cipher = cipher.lower()
for i in range(1, 26):
decoded = ""
decoded_cipher = ""
for char in cipher:
ascii = ord(char)
if ascii < 97:
new_ascii = ascii
else:
if ascii > 122:
new_ascii = ascii
else:
new_ascii = ascii - int(i)
if new_ascii < 97:
new_ascii = new_ascii + 26
else:
new_ascii = new_ascii
decoded = chr(new_ascii)
decoded_cipher = decoded_cipher + decoded
print("\n" + decoded_cipher)
key = get_int()

for character in cipher:
ascii = ord(character) - key
decoded_cipher += chr(ascii)

print(decoded_cipher)


if __name__ == '__main__':
Expand Down

0 comments on commit 483b4ba

Please sign in to comment.