- Home
- Overview
- Double Strength Encryption
- Python Overview and Installation
- Reverse Cipher
- Caesar Cipher
- ROT13 Algorithm
- Transposition Cipher
- Encryption of Transposition Cipher
- Decryption of Transposition Cipher
- Encryption of files
- Decryption of files
- Base64 Encoding & Decoding
- XOR Process
- Multiplicative Cipher
- Affine Ciphers
- Hacking Monoalphabetic Cipher
- Simple Substitution Cipher
- Testing of Simple Substitution Cipher
- Decryption of Simple Substitution Cipher
- Python Modules of Cryptography
- Understanding Vignere Cipher
- Implementing Vignere Cipher
- One Time Pad Cipher
- Implementation of One Time Pad Cipher
- Symmetric & Asymmetric Cryptography
- Understanding RSA Algorithm
- Creating RSA Keys
- RSA Cipher Encryption
- RSA Cipher Decryption
- Hacking RSA Cipher
Cryptography with Python - Resources
Testing of Simple Substitution Cipher
In this chapter, we will focus on testing substitution cipher using various methods, which helps to generate random strings as given below −
Example - Testing Substitution Cipher
main.py
import random, string
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def main():
for i in range(1000):
key = getRandomKey()
message = random_string()
print('Test %s: String: "%s.."' % (i + 1, message[:50]))
print("Key: " + key)
encrypted = translateMessage(message, key, 'E')
decrypted = translateMessage(encrypted, key, 'D')
if decrypted != message:
print('ERROR: Decrypted: "%s" Key: %s' % (decrypted, key))
sys.exit()
print('Substutition test passed!')
def random_string(size = 5000, chars = string.ascii_letters + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
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 = list(LETTERS)
random.shuffle(randomList)
return ''.join(randomList)
if __name__ == '__main__':
main()
Output
You can observe the output as randomly generated strings which helps in generating random plain text messages, as shown below −
Test 1: String: "7KdDVIFW16UM8U0lNwn3vcnky4EYCKbFUbxh1hYqQdKcKnq4l9.." Key: VKAYMQGLZHNOUJDCETFPSBWRXI Substutition test passed! Test 2: String: "c78WvP3zLihhCogHNTvhQJ5TsZWaCX71p71xU1UPHSOBmAgLQq.." Key: ROUXJYHEPLGMVDWCQTIAFKZNBS Substutition test passed! ...
After the test is successfully completed, we can observe the output message Substitution test passed!.
... Test 998: String: "P9rpUdfVtHYwH8oCMJssNu2AYl8vVkoJB0BwcXmz1ldXPSNlyy.." Key: SXKWFYBIJAUTVDGELZHCNOPMQR Substutition test passed! Test 999: String: "EWGGt9RSrlZwYo3FUNG4JCtDAytUWF0rihlcJEvQd4nZxo0Qvg.." Key: VCIGEBQTWHNXRSYUMKOFLJADPZ Substutition test passed! Test 1000: String: "GYsSq3xU33VP2f3pnAajgMq1ClUzQPGe72fL9sv9xYV0Bpx04Y.." Key: REMIUPWXVLDYGHCOJFSKQBTAZN Substutition test passed!
Thus, you can hack a substitution cipher in the systematic manner.
Advertisements