Skip to content

Commit

Permalink
Merge pull request #68 from AM-ash-OR-AM-I/main
Browse files Browse the repository at this point in the history
Added different Ciphers -  Cryptography
  • Loading branch information
Ashutosh74554 authored Oct 24, 2022
2 parents 156a142 + 060f4ec commit 353565f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
53 changes: 53 additions & 0 deletions AM-ash-OR-AM-I/Ciphers - Python/cryptography.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Below is my custom implementation for cipher/deciphering messages
"""
import string

all_characters = string.printable[:-6]


class Encryption:
def encrypt(self, plain_text):
encrypted_str = ""
factor = len(plain_text) # Length based key
for letter in plain_text:
for char in all_characters:
if letter == char:
ordinal = ord(letter)
encrypted_str += str(ordinal / factor) + "?"
return encrypted_str

def decrypt(self, encrypted_str):
en_key = ""
result = ""
length = 0
for i in encrypted_str:
if i == "?":
length += 1
for i in encrypted_str:
if i == "?":
ordinal = round(float(en_key) * length)
char = chr(ordinal)
result += char
en_key = ""
else:
en_key += i # if not '?'
return result


encryption = Encryption()

ch = int(input(
"""Enter your choice:
1. Encrypt
2. Decrypt
"""
))
if ch == 1:
text = input("Enter the text to encrypt: ")
encrypted_text = encryption.encrypt(text)
print("Encrypted text =", encrypted_text)
else:
encrypted_text = input("Enter text to decrypt: ")
plain_text = encryption.decrypt(encrypted_text)
print("Decrypted text =",plain_text)
53 changes: 53 additions & 0 deletions AM-ash-OR-AM-I/Ciphers - Python/rot_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
ROT Cipher a.k.a. Caeser Cipher.
Shifts all of alphabets by n-places/shifts
"""

class ROT:
def __init__(self, shift=1) -> None:
self.shift = shift

def decrypt(self, encrypted_text: str):
cipher_text = ""
for x in encrypted_text:
starting_alphabet = 65 if x.isupper() else 97
position = ord(x) - starting_alphabet
cipher = (
chr(starting_alphabet + (position - self.shift) % 26)
if x.isalpha()
else x
)
cipher_text += cipher
return cipher_text

def encrypt(self, plain_text):
cipher_text = ""
for x in plain_text:
starting_alphabet = 65 if x.isupper() else 97
position = ord(x) - starting_alphabet
cipher = (
chr(starting_alphabet + (position + self.shift) % 26)
if x.isalpha()
else x
)
cipher_text += cipher
return cipher_text


n = input(
"""Enter your choice:
1. Cipher
2. Decipher/Plain Text
"""
)

shift = int(input("Enter no. of shifts: "))
caesar_cipher = ROT(shift)
text = input("Text: ")

if n == "2":
plain_text = caesar_cipher.decrypt(text)
print(f"Deciphered text = {plain_text}")
else:
cipher_text = caesar_cipher.encrypt(text)
print(f"Cipher text = {cipher_text}")

0 comments on commit 353565f

Please sign in to comment.