simple substitution cipher Algorithm
The simple substitution cipher algorithm is a classical encryption technique that has been widely used throughout history as a means of securing messages and information. It is based on the concept of replacing each letter (or symbol) in the plaintext with another letter (or symbol) according to a predetermined key. The key, in this case, is typically a permutation of the alphabet, and it defines the relationship between the plaintext and the ciphertext. The substitution is done such that the same plaintext letter always maps to the same ciphertext letter, and vice versa. This monoalphabetic substitution ensures that the frequency distribution of letters in the plaintext is preserved in the ciphertext, making it vulnerable to frequency analysis attacks.
To encrypt a message using the simple substitution cipher, one would first choose a key, which is a mapping of the plaintext alphabet to the ciphertext alphabet. For example, a key could be the arrangement of the letters in the alphabet shifted by a certain number of positions (like in the Caesar cipher), or it could be a completely randomized permutation of the alphabet. To encrypt the message, the sender would replace each letter in the plaintext with the corresponding letter in the ciphertext alphabet according to the key. To decrypt the message, the receiver would reverse the process by replacing each letter in the ciphertext with the corresponding letter in the plaintext alphabet according to the same key. The security of the simple substitution cipher relies on the secrecy of the key; if the key is discovered, the message can be easily decrypted. However, due to its susceptibility to frequency analysis attacks, the simple substitution cipher is no longer considered secure for modern applications.
import random
import sys
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def main():
message = input("Enter message: ")
key = "LFWOAYUISVKMNXPBDCRJTQEGHZ"
resp = input("Encrypt/Decrypt [e/d]: ")
checkValidKey(key)
if resp.lower().startswith("e"):
mode = "encrypt"
translated = encryptMessage(key, message)
elif resp.lower().startswith("d"):
mode = "decrypt"
translated = decryptMessage(key, message)
print("\n{}ion: \n{}".format(mode.title(), translated))
def checkValidKey(key):
keyList = list(key)
lettersList = list(LETTERS)
keyList.sort()
lettersList.sort()
if keyList != lettersList:
sys.exit("Error in the key or symbol set.")
def encryptMessage(key, message):
"""
>>> encryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Harshil Darji')
'Ilcrism Olcvs'
"""
return translateMessage(key, message, "encrypt")
def decryptMessage(key, message):
"""
>>> decryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Ilcrism Olcvs')
'Harshil Darji'
"""
return translateMessage(key, message, "decrypt")
def translateMessage(key, message, mode):
translated = ""
charsA = LETTERS
charsB = key
if mode == "decrypt":
charsA, charsB = charsB, charsA
for symbol in message:
if symbol.upper() in charsA:
symIndex = charsA.find(symbol.upper())
if symbol.isupper():
translated += charsB[symIndex].upper()
else:
translated += charsB[symIndex].lower()
else:
translated += symbol
return translated
def getRandomKey():
key = list(LETTERS)
random.shuffle(key)
return "".join(key)
if __name__ == "__main__":
main()