Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Implementation of Substitution Technique: Experiment-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7
At a glance
Powered by AI
The key takeaways are about implementing various substitution cipher techniques like the Caesar cipher, monoalphabetic cipher, and analyzing techniques like brute force and frequency analysis attacks.

The code implements the Caesar cipher for encryption and decryption as well as a monoalphabetic cipher. It also shows a brute force attack on the Caesar cipher.

A brute force attack on the Caesar cipher tries every possible key on the ciphertext until the original plaintext is recovered. It shifts each character by the tested key and checks if it reveals an intelligible message.

EXPERIMENT-1

IMPLEMENTATION OF SUBSTITUTION TECHNIQUE

Date of Performance: 16/07/2021


Date of Submission:

1 a] Ceaser Cipher Encryption and Decryption

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))]

# loop to recover plain text


decrypt_txt = []

for char in cipher_txt:


if char in all_letters:
temp = dict2[char]
decrypt_txt.append(temp)
else:
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

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

2a] Mono alphabetic Cipher:


Aim:

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= []

for char in plain_text:


if char in letters:
new_text += char

for char in key:


if char in letters:
if char not in new_key:
new_key += char

for char in letters:


if char not in new_key:
new_key += 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:

Enter the secret message: Xie is best


Enter the key: crypto
Cipher text is : wdtdnrtnq

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={}

for char in cipher_text:


if char not in stored_letters:
stored_letters[char] = 1
else:
stored_letters[char] += 1
attempt = cipher_text.replace("i","\033[31me\033[0m")
attempt = attempt.replace("f","\033[34mi\033[0m")
attempt = attempt.replace("r","\033[34ms\033[0m")
attempt = attempt.replace("s","\033[34ms\033[0m")
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}

Conclusion: By doing this experiment I get to know about how to code


for practical implementation of substitution ciphering techniques like
ceaser cipher encryption and decryption, monoalphabetic cipher and also
brute force attack on ceaser cipher and guessing attack on
monoalphabetic cipher.

You might also like