-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from AM-ash-OR-AM-I/main
Added different Ciphers - Cryptography
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |