Cryptography with Python Tutorial
Useful Resources
Selected Reading
- Hacking RSA Cipher
- RSA Cipher Decryption
- RSA Cipher Encryption
- Creating RSA Keys
- Understanding RSA Algorithm
- Symmetric & Asymmetric Cryptography
- Implementation of One Time Pad Cipher
- One Time Pad Cipher
- Implementing Vignere Cipher
- Understanding Vignere Cipher
- Python Modules of Cryptography
- Decryption of Simple Substitution Cipher
- Testing of Simple Substitution Cipher
- Simple Substitution Cipher
- Hacking Monoalphabetic Cipher
- Affine Ciphers
- Multiplicative Cipher
- XOR Process
- Base64 Encoding & Decoding
- Decryption of files
- Encryption of files
- Decryption of Transposition Cipher
- Encryption of Transposition Cipher
- Transposition Cipher
- ROT13 Algorithm
- Caesar Cipher
- Reverse Cipher
- Python Overview and Installation
- Double Strength Encryption
- Overview
- Home
Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Simple Substitution Cipher
Simple Substitution Cipher
Simple substitution cipher is the most commonly used cipher and includes an algorithm of substituting every plain text character for every cipher text character. In this process, alphabets are jumbled in comparison with Caesar cipher algorithm.
Example
Keys for a simple substitution cipher usually consists of 26 letters. An example key is −
plain alphabet : abcdefghijklmnopqrstuvwxyz cipher alphabet: phqgiumeaylnofdxjkrcvstzwb
An example encryption using the above key is−
plaintext : defend the east wall of the castle ciphertext: giuifg cei iprc tpnn du cei qprcni
The following code shows a program to implement simple substitution cipher −
import random, sys LETTERS = ABCDEFGHIJKLMNOPQRSTUVWXYZ def main(): message = if len(sys.argv) > 1: with open(sys.argv[1], r ) as f: message = f.read() else: message = raw_input("Enter your message: ") mode = raw_input("E for Encrypt, D for Decrypt: ") key = while checkKey(key) is False: key = raw_input("Enter 26 ALPHA key (leave blank for random key): ") if key == : key = getRandomKey() if checkKey(key) is False: print( There is an error in the key or symbol set. ) translated = translateMessage(message, key, mode) print( Using key: %s % (key)) if len(sys.argv) > 1: fileOut = enc. + sys.argv[1] with open(fileOut, w ) as f: f.write(translated) print( Success! File written to: %s % (fileOut)) else: print( Result: + translated) # Store the key into pst, sort it, convert back, compare to alphabet. def checkKey(key): keyString = .join(sorted(pst(key))) return keyString == LETTERS def translateMessage(message, key, mode): translated = charsA = LETTERS charsB = key # If decrypt mode is detected, swap A and B if mode == D : 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(): randomList = pst(LETTERS) random.shuffle(randomList) return .join(randomList) if __name__ == __main__ : main()
Output
You can observe the following output when you implement the code given above −
Advertisements