This is a java program to implement monoalphabetic cypher. In cryptography, a substitution cipher is a method of encoding by which units of plaintext are replaced with ciphertext, according to a regular system; the “units” may be single letters (the most common), pairs of letters, triplets of letters, mixtures of the above, and so forth.
Program: Chat application (using Mono-alphabetic encryption) created using NetBeans UI and decrypted using a Python script
Author: Anirudh Ravi
About:
These programs are written in Java and Python respectively. For more information please refer to this video: https://youtu.be/gWRWtLkb6t0.
Chat Application:
The Chat Application uses Mono-alphabetic cipher encryption. It was built in Java and makes use of simple socket programming where before a message is transmitted it is encrypted using the mono-alphabetic key and on receiving the message on the receiver side the message is decrypted using the same key.
Cryptanalysis of Mono-alphabetic Cipher:
The Cryptanalysis of this code is done using hill climbing algorithm written using Python code. Character–n–grams of a text can be used to analyse and predict the ciphertext to plaintext mapping. This particular approach is used to get the ciphertext based on the plaintext without knowing the key. We make use of Quadgram data (in this case collected from the book “War and Peace” by Leo Tolstoy) in order to predict the key of the cipher text. Quadgrams work better than tri, di and monograms while higher character-n-grams do not contribute much to accuracy of prediction.
Cryptanalysis Algorithm:
The algorithm works as follows:
- Use a random key and set this as the parent key
- Do this for 1000 iterations:
- Store the fitness score (score based on occurrences of Quadgrams) of the deciphered text using key
- Swap 2 characters in the parent key and set this as child. If fitness score calculated using the child is greater than parent then set the parent as child key
- Print current best key and best plaintext if fitness score is higher than from the earlier iterations and move back to step 1
![Encryption Encryption](/uploads/1/2/6/2/126240989/840246559.jpg)
Quadgram data was taken from the website Practical Cryptography. Breaking ciphers less than 100 characters is more troublesome because word frequency and n-gram
Analysis of results:
Ciphertext of varying character length either encrypted using monoalphabetic cipher (plaintext data collected from Quora) or taken from different university websites and the NSA (National Security Agency) website was used to test the Cryptanalysis code. For further details please refer: https://drive.google.com/file/d/0B5oLABv2TB-FTVphTHhjQmoxTnc/view.
monoalphabetic.py
# Simple Substitution Cipher |
# http://inventwithpython.com/hacking (BSD Licensed) |
import sys, random |
LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
defmain(): |
myMessage ='When you do the common things in life in an uncommon way, you will command the attention of the world' |
myKey ='QWERTYUIOPASDFGHJKLZXCVBNM' |
myMode ='encrypt'# set to 'encrypt' or 'decrypt' |
checkValidKey(myKey) |
if myMode 'encrypt': |
translated = encryptMessage(myKey, myMessage) |
elif myMode 'decrypt': |
translated = decryptMessage(myKey, myMessage) |
print('Using key %s'% (myKey)) |
print('The %sed message is:'% (myMode)) |
print(translated) |
defcheckValidKey(key): |
keyList =list(key) |
lettersList =list(LETTERS) |
keyList.sort() |
lettersList.sort() |
if keyList != lettersList: |
sys.exit('There is an error in the key or symbol set.') |
defencryptMessage(key, message): |
return translateMessage(key, message, 'encrypt') |
defdecryptMessage(key, message): |
return translateMessage(key, message, 'decrypt') |
deftranslateMessage(key, message, mode): |
translated ='' |
charsA =LETTERS |
charsB = key |
if mode 'decrypt': |
# For decrypting, we can use the same code as encrypting. We |
# just need to swap where the key and LETTERS strings are used. |
charsA, charsB = charsB, charsA |
# loop through each symbol in the message |
for symbol in message: |
if symbol.upper() in charsA: |
# encrypt/decrypt the symbol |
symIndex = charsA.find(symbol.upper()) |
if symbol.isupper(): |
translated += charsB[symIndex].upper() |
else: |
translated += charsB[symIndex].lower() |
else: |
# symbol is not in LETTERS, just add it |
translated += symbol |
return translated |
defgetRandomKey(): |
key =list(LETTERS) |
random.shuffle(key) |
return''.join(key) |
main() |
![Implement Monoalphabetic Cipher Encryption And Decryption In Python Implement Monoalphabetic Cipher Encryption And Decryption In Python](/uploads/1/2/6/2/126240989/985389305.jpg)
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment