0% found this document useful (0 votes)
22 views1 page

1 Simple Substitution Cipher

encryption code and decryption

Uploaded by

ashwinh2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views1 page

1 Simple Substitution Cipher

encryption code and decryption

Uploaded by

ashwinh2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

# **1** Simple Substitution Cipher

def encrypt(plaintext, substitution_key):


ciphertext = ""
for char in plaintext:
if char.islower():
index = ord(char) - ord('a')
ciphertext += substitution_key[index].lower()
elif char.isupper():
index = ord(char) - ord('A')
ciphertext += substitution_key[index].upper()
elif char.isdigit():
ciphertext += str((int(char) + 1) % 10)
else:
ciphertext += char
return ciphertext

def decrypt(ciphertext, substitution_key):


plaintext = ""
reverse_key = {substitution_key[i]: chr(i + ord('a')) for i in range(26)}
for char in ciphertext:
if char.islower():
plaintext += reverse_key[char]
elif char.isupper():
plaintext += reverse_key[char.lower()].upper()
elif char.isdigit():
plaintext += str((int(char) - 1) % 10)
else:
plaintext += char
return plaintext

substitution_key = "qwertyuiopasdfghjklzxcvbnm"
plaintext = "Hello World 123"
ciphertext = encrypt(plaintext, substitution_key)
print("Encrypted:", ciphertext)
print("Decrypted:", decrypt(ciphertext, substitution_key))

You might also like