Implementation of Substitution Technique: Experiment-1
Implementation of Substitution Technique: Experiment-1
Example:
Algorithm:
Step-1:Start
Step-2: taking all input letters as it is and taking input of number of letters
to be shifted from its original place.
Step-3: using for loop we are making dictionary where we are storing all
letters but their index is shifted by that count taken.
1
Step-4: then for generating cipher text checking one by one all input
letters as if it is present in our created shifted dictionary and if present
then appending that letter to temp list and then joining all letters.
Step-5: for decryption doing the same thing as of encryption except that
instead of positive shifting we are making negative shifting by that count
taken.
Step-6:End.
Code:
#ENCRYPTION
import string
all_letters= string.ascii_letters #A list containing all characters
dict1 = {}
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))]
2
temp = char
decrypt_txt.append(temp)
decrypt_txt = "".join(decrypt_txt)
print("Recovered plain text :", decrypt_txt)
Output:
Cipher Text is: aLH LV EHVW
Recovered plain text : XIE IS BEST
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:
Step-1: Start.
Step-2: taking cipher text as input and letters as string.
Step-3: using for loop and checking for each value in letters string ad
taking initial output empty string.
Step-4: again taking inner for loop and for each symbol in cipher text we
are finding if it is present in letter string then we are subtracting that
cipher text letter from letters string.
Step-5: if subtraction value is less than 0 than we adding it again and
concatenating with output string and this process continues 26 times as
we get final result.
Step-6: End.
Code:
cipher_text= “aLH LV EHVW”
letters = "abcdefghijklmnopqrstuvwxyz"
for key in range(len(letters)):
output = ''
for symbol in cipher_text:
num = letters.find(symbol)
num = num - key
3
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
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
4
number of occurrence in that plaintext, ‘A’ will always get encrypted to
‘D’.
Algorithm:
Step-1: start.
Step-2: we are plain text as input and all letters as one string and taking
key as input.
Step-3: using for loop checking for letters which are present in key
keeping them as it is and those are not present appending them to new
list.
Step-4: merging all letters of list and for encryption corresponding key is
replaced for that letter in plain text.
Step-5: End.
Code:
#Encryption
plain_text = input("Enter the secret message: ").lower()
letters = "abcdefghijklmnopqrstuvwxyz"
key = input("Enter the key: ").lower()
new_key = ""
new_text = ""
cipher_text= []
5
for char in plain_text:
if char in letters:
new_text += char
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:
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.
6
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.
Algorithm:
Step-1: Start.
Step-2: in this we are taking ciher text as input and initially empty
dictionary.
Step-3: using for loop for each character in cipher text we are checking if
that letter is not present in dictionary then setting its value as 1 else
incrementing its value.
Step-4: using attempts to make original text this guessing attack is done.
Step-5: End.
Code:
cipher_text = "xfifruirs"
stored_letters={}
7
attempt = attempt.replace("u","\033[34mb\033[0m")
print(attempt)
print(stored_letters)
Output:
xieisbess
{'x': 1, 'f': 2, 'i': 2, 'r': 2, 'u': 1, 's': 1}