Implementation of Substitution Technique: Experiment-1
Implementation of Substitution Technique: Experiment-1
Implementation of Substitution Technique: Experiment-1
Aim:
Theory:
To encrypt a message with a Caesar cipher, each letter in the message is
changed
using a simple rule: shift by three. Each letter is replaced by the letter
three letters ahead in the alphabet. A becomes D, B becomes E, and so
on. For the last letters, we can think of alphabet as a circle and "wrap
around". W becomes Z, X becomes A, Y becomes B, and Z becomes C.
To change a message back, each letter is replaced by the one three before
it.
Example:
Algorithm:
Code:
#ENCRYPTION
import string
all_letters= string.ascii_letters #A list containing all characters
dict1 = {}
1
key = 3
for i in range(len(all_letters)):
dict1[all_letters[i]] = all_letters[(i+key)%len(all_letters)]
plain_txt= "XIE IS BEST"
cipher_txt=[]
# loop to generate ciphertext
for char in plain_txt:
if char in all_letters:
temp = dict1[char]
cipher_txt.append(temp)
else:
temp =char
cipher_txt.append(temp)
cipher_txt= "".join(cipher_txt)
print("Cipher Text is: ",cipher_txt)
#DECRYPTION
dict2 = {}
for i in range(len(all_letters)):
dict2[all_letters[i]] = all_letters[(i-key)%(len(all_letters))]
decrypt_txt = "".join(decrypt_txt)
print("Recovered plain text :", decrypt_txt)
Output:
Cipher Text is: aLH LV EHVW
Recovered plain text : XIE IS BEST
2
1 b] Brute force attack on Ceaser Cipher:
Aim:
Theory:
We can hack the Caesar cipher by using a cryptanalytic technique
called brute-force. A brute-force attack tries every possible decryption
key for a cipher. Decrypting the ciphertext with that key, looking at the
output, and then moving on to the next key if they didn’t find the secret
message. Because the brute-force technique is so effective against the
Caesar cipher.
Algorithm:
Code:
cipher_text= “aLH LV EHVW”
for key in range(len(letters)):
output = ''
for symbol in cipher_text:
num = letters.find(symbol)
num = num - key
if num < 0:
num = num + len(letters)
output = output + letters[num]
else:
output = output + symbol
print('The message is #%s: %s' %(key, output))
Output:
The message is #0: azzzzzzzzzz
The message is #1: zyyyyyyyyyy
The message is #2: yxxxxxxxxxx
The message is #3: xwwwwwwwwww
The message is #4: wvvvvvvvvvv
The message is #5: vuuuuuuuuuu
The message is #6: utttttttttt
The message is #7: tssssssssss
The message is #8: srrrrrrrrrr
The message is #9: rqqqqqqqqqq
The message is #10: qpppppppppp
The message is #11: poooooooooo
The message is #12: onnnnnnnnnn
The message is #13: nmmmmmmmmmm
3
The message is #14: mllllllllll
The message is #15: lkkkkkkkkkk
The message is #16: kjjjjjjjjjj
The message is #17: jiiiiiiiiii
The message is #18: ihhhhhhhhhh
The message is #19: hgggggggggg
The message is #20: gffffffffff
The message is #21: feeeeeeeeee
The message is #22: edddddddddd
The message is #23: dcccccccccc
The message is #24: cbbbbbbbbbb
The message is #25: baaaaaaaaaa
Theory:
Mono alphabetic cipher is a substitution cipher in which for a given key,
the cipher alphabet for each plain alphabet is fixed throughout the
encryption process. For example, if ‘A’ is encrypted as ‘D’, for any
number of occurrence in that plaintext, ‘A’ will always get encrypted to
‘D’.
4
Algorithm:
Code:
#Encryption
plain_text = input("Enter the secret message: ").lower()
letters = "abcdefghijklmnopqrstuvwxyz"
key = input("Enter the key: ").lower()
new_key = ""
new_text = ""
cipher_text= []
def encrypt():
index_values=[letters.index(char) for char in new_text]
return "".join(new_key[indexKey] for indexKey in index_values)
print(“Cipher text is :”,encrypt())
Output:
5
2b] Guessing Attack using Frequency Analysis on Mono Alphabetic
Cipher:
Aim:
Theory:
The methodology behind frequency analysis relies on the fact that in any
language, each letter has its own personality. The most obvious trait that
letters have is the frequency with which they appear in a language.
Clearly in English the letter "Z" appears far less frequently than, say, "A".
In times gone by, if you wanted to find out the frequencies of letters
within a language, you had to find a large piece of text and count each
frequency. Now, however, we have computers that can do the hard work
for us. But in fact, we don't even need to do this step, as for most
languages there are databases of the letter frequencies, which have been
calculated by looking at millions of texts, and are thus very highly
accurate.
From these databases we find that "E" is the most common letter in
English, appearing about 12% of the time (that is just over one in ten
letters is an "E"). The next most common letter is "T" at 9%. The full
frequency list is given by the graph below.
6
Algorithm:
Code:
cipher_text = "xfifruirs"
stored_letters={}
Output:
xieisbess
{'x': 1, 'f': 2, 'i': 2, 'r': 2, 'u': 1, 's': 1}