Skip to content

Commit

Permalink
added caesar cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
Arichikurumo committed Sep 15, 2024
1 parent 43c888d commit 2144b8a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
Binary file added __pycache__/caesar.cpython-312.pyc
Binary file not shown.
Binary file added __pycache__/rot13.cpython-312.pyc
Binary file not shown.
39 changes: 39 additions & 0 deletions caesar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def caesarEncrypt(text, shift):
result = []

for char in text:
# Check if the character is an uppercase letter
if 'A' <= char <= 'Z':
# Rotate the character by 13 within the uppercase letters
result.append(chr((ord(char) - ord('A') + shift) % 26 + ord('A')))

# Check if the character is a lowercase letter
elif 'a' <= char <= 'z':
# Rotate the character by 13 within the lowercase letters
result.append(chr((ord(char) - ord('a') + shift) % 26 + ord('a')))

else:
# If it's not a letter, keep it unchanged
result.append(char)

return ''.join(result)

def caesarDecrypt(text, shift):
result = []

for char in text:
# Check if the character is an uppercase letter
if 'A' <= char <= 'Z':
# Rotate the character by 13 within the uppercase letters
result.append(chr((ord(char) - ord('A') - shift) % 26 + ord('A')))

# Check if the character is a lowercase letter
elif 'a' <= char <= 'z':
# Rotate the character by 13 within the lowercase letters
result.append(chr((ord(char) - ord('a') - shift) % 26 + ord('a')))

else:
# If it's not a letter, keep it unchanged
result.append(char)

return ''.join(result)
3 changes: 3 additions & 0 deletions gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/__pycache__
/__pycache__/*
__pycache__/*
19 changes: 14 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
from rot13 import rot13encrypt, rot13decrypt
from rot13 import rot13Encrypt, rot13Decrypt
from caesar import caesarEncrypt, caesarDecrypt

# Create an ArgumentParser object
parser = argparse.ArgumentParser(description="Process user text to encrypt or decrypt it using various algorithms.")
Expand All @@ -8,14 +9,22 @@
parser.add_argument('-a', '--algorithm', type=str, required=True, help='Choose an algorithm (e.g. ROT13)')
parser.add_argument('-d', '--decrypt', action='store_true', help='decrypt a text')
parser.add_argument('-t', '--text', type=str, required=True, help='Text you want to encrypt/decrypt')
parser.add_argument('-s', '--shift', type=int, help='Number of shifts a char should do (e.g. 7 a->h)')

args = parser.parse_args()


if args.algorithm == "rot13":
if args.decrypt == True:
dec_string = rot13decrypt(args.text)
dec_string = rot13Decrypt(args.text)
print(f"encrypted: {args.text} -> decrypted: {dec_string}")
else:
enc_string = rot13Encrypt(args.text)
print(f"cleartext: {args.text} -> encrypted: {enc_string}")

if args.algorithm == "caesar" and args.shift != None:
if args.decrypt == True:
dec_string = caesarDecrypt(args.text, args.shift)
print(f"encrypted: {args.text} -> decrypted: {dec_string}")
else:
enc_string = rot13encrypt(args.text)
print(f"cleartext: {args.text} -> encrypted: {enc_string}")
enc_string = caesarEncrypt(args.text, args.shift)
print(f"cleartext: {args.text} -> encrypted: {enc_string}")
4 changes: 2 additions & 2 deletions rot13.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ROT13 Implementation Script

def rot13encrypt(text):
def rot13Encrypt(text):
result = []

for char in text:
Expand All @@ -20,7 +20,7 @@ def rot13encrypt(text):

return ''.join(result)

def rot13decrypt(text):
def rot13Decrypt(text):
result = []

for char in text:
Expand Down

0 comments on commit 2144b8a

Please sign in to comment.