Skip to content

Commit

Permalink
refactoring caesar and rsa
Browse files Browse the repository at this point in the history
  • Loading branch information
koshkar committed Oct 25, 2023
1 parent d91babd commit 2f1cef4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
28 changes: 14 additions & 14 deletions src/lab2/caesar.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
def encrypt_caesar(plaintext: str, shift: int = 3) -> str:
"""
Encrypts plaintext using a Caesar cipher.
>>> encrypt_caesar("PYTHON")
'SBWKRQ'
>>> encrypt_caesar("python")
'sbwkrq'
>>> encrypt_caesar("Python3.6")
'Sbwkrq3.6'
>>> encrypt_caesar("ILOVEBEER")
'LORYHEHHU'
>>> encrypt_caesar("bruuuuuuh")
'euxxxxxxk'
>>> encrypt_caesar("9mm")
'9pp'
>>> encrypt_caesar("")
''
"""
ciphertext = ""
for char in plaintext:
if char.isalpha():
if char.isupper():
ciphertext += chr((ord(char) - 65 + shift) % 26 + 65)
ciphertext += chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
else:
ciphertext += chr((ord(char) - 97 + shift) % 26 + 97)
ciphertext += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
else:
ciphertext += char
return ciphertext
Expand All @@ -25,12 +25,12 @@ def encrypt_caesar(plaintext: str, shift: int = 3) -> str:
def decrypt_caesar(ciphertext: str, shift: int = 3) -> str:
"""
Decrypts a ciphertext using a Caesar cipher.
>>> decrypt_caesar("SBWKRQ")
'PYTHON'
>>> decrypt_caesar("sbwkrq")
'python'
>>> decrypt_caesar("Sbwkrq3.6")
'Python3.6'
>>> decrypt_caesar("LORYHEHHU")
'ILOVEBEER'
>>> decrypt_caesar("euxxxxxxk")
'bruuuuuuh'
>>> decrypt_caesar("9pp")
'9mm'
>>> decrypt_caesar("")
''
"""
Expand Down
6 changes: 2 additions & 4 deletions src/lab2/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def is_prime(n: int) -> bool:
if n % i == 0:
return False
return True
pass


def gcd(a: int, b: int) -> int:
Expand All @@ -32,7 +31,6 @@ def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
pass


def multiplicative_inverse(e: int, phi: int) -> int:
Expand All @@ -57,7 +55,6 @@ def multiplicative_inverse(e: int, phi: int) -> int:
d = x1 % phi
return d

pass


def generate_keypair(p: int, q: int) -> tp.Tuple[tp.Tuple[int, int], tp.Tuple[int, int]]:
Expand Down Expand Up @@ -121,4 +118,5 @@ def decrypt(pk: tp.Tuple[int, int], ciphertext: tp.List[int]) -> str:
print("".join(map(lambda x: str(x), encrypted_msg)))
print("Decrypting message with public key ", public, " . . .")
print("Your message is:")
print(decrypt(public, encrypted_msg))
print(decrypt(public, encrypted_msg))

0 comments on commit 2f1cef4

Please sign in to comment.