0% found this document useful (0 votes)
75 views10 pages

Implementation of Advanced Encryption Technique and Public Key Encryption

The document describes an experiment implementing advanced encryption techniques. It includes: 1) A Python program to encrypt and decrypt files using AES (Advanced Encryption Standard) in both ECB (Electronic Code Block) and CBC (Cipher Block Chaining) modes. The code takes a key as input and allows the user to encrypt and decrypt files. 2) An explanation of the RSA (Rivest-Shamir-Adleman) public-key encryption algorithm and a Python program to encrypt and decrypt messages using the RSA algorithm. The program generates public and private keys based on prime numbers and performs encryption and decryption of a sample message. 3) A conclusion that the experiment provided knowledge of how

Uploaded by

Sonam Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views10 pages

Implementation of Advanced Encryption Technique and Public Key Encryption

The document describes an experiment implementing advanced encryption techniques. It includes: 1) A Python program to encrypt and decrypt files using AES (Advanced Encryption Standard) in both ECB (Electronic Code Block) and CBC (Cipher Block Chaining) modes. The code takes a key as input and allows the user to encrypt and decrypt files. 2) An explanation of the RSA (Rivest-Shamir-Adleman) public-key encryption algorithm and a Python program to encrypt and decrypt messages using the RSA algorithm. The program generates public and private keys based on prime numbers and performs encryption and decryption of a sample message. 3) A conclusion that the experiment provided knowledge of how

Uploaded by

Sonam Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

EXPERIMENT-4

IMPLEMENTATION OF ADVANCED ENCRYPTION


TECHNIQUE AND PUBLIC KEY ENCRYPTION

Name: Sonam Gupta (201903015)


Roll no.: 14
Date of Performance: 06/08/2021
Date of Submission: 11/08/2021

1a) Advanced Encryption Algorithm:


Aim: To write a python program for the implementation of AES.

Theory:
AES is an iterative rather than Feistel cipher. It is based on ‘substitution–
permutation network’. It comprises of a series of linked operations, some
of which involve replacing inputs by specific outputs (substitutions) and
others involve shuffling bits around (permutations).
Interestingly, AES performs all its computations on bytes rather than
bits. Hence, AES treats the 128 bits of a plaintext block as 16 bytes.
These 16 bytes are arranged in four columns and four rows for
processing as a matrix −
Unlike DES, the number of rounds in AES is variable and depends on
the length of the key. AES uses 10 rounds for 128-bit keys, 12 rounds for
192-bit keys and 14 rounds for 256-bit keys. Each of these rounds uses a
different 128-bit round key, which is calculated from the original AES
key.

1
Encryption Process:
Each round comprise of four sub-processes. The first round process is
depicted below-

Algorithm:
Step-1: Start
Step-2: Load the n-bit Initialization Vector (IV) in the top register.
Step-3: XOR the n-bit plaintext block with data value in top register.
Step-4: Encrypt the result of XOR operation with underlying block
cipher with key K.
Step-5: Feed ciphertext block into top register and continue the operation
till all plaintext blocks are processed.
Step-6: For decryption, IV data is XORed with first ciphertext block
decrypted. The first ciphertext block is also fed into to register replacing
IV for decrypting next ciphertext block.
Step-7: End.

Code:
from Crypto import Random
from Crypto.Cipher import AES
import os
import os.path
import time

class Encryptor:

2
def __init__(self, key):
self.key = key

def pad(self, s):


return s + b"\0" * (AES.block_size - len(s) % AES.block_size)

def encrypt(self, message, key, key_size=256):


message = self.pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(message)

def encrypt_file(self, file_name):


with open(file_name, 'rb') as fo:
plaintext = fo.read()
enc = self.encrypt(plaintext, self.key)
with open(file_name + ".enc", 'wb') as fo:
fo.write(enc)
os.remove(file_name)

def decrypt(self, ciphertext, key):


iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
return plaintext.rstrip(b"\0")

def decrypt_file(self, file_name):


with open(file_name, 'rb') as fo:
ciphertext = fo.read()
dec = self.decrypt(ciphertext, self.key)
with open(file_name[:-4], 'wb') as fo:
fo.write(dec)
os.remove(file_name)

key =
b'[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\x
df\xcb\xc4\x94\x9d(\x9e'
enc = Encryptor(key)
clear = lambda: os.system('cls')
while True:
choice = int(input("1. Press '1' to encrypt file.\n2. Press '2' to decrypt
file.\n3. Press '3' to exit.\n"))
clear()

3
if choice == 1:
enc.encrypt_file(str(input("Enter name of file to encrypt: ")))
elif choice == 2:
enc.decrypt_file(str(input("Enter name of file to decrypt: ")))
elif choice == 3:
exit()
else:
print("Please select a valid option!")

Output:
1. Press '1' to encrypt file.
2. Press '2' to decrypt file.
3. Press '3' to exit.
1
Enter name of file to encrypt: test.txt
1. Press '1' to encrypt file.
2. Press '2' to decrypt file.
3. Press '3' to exit.
2
Enter name of file to decrypt: test.txt.enc
1. Press '1' to encrypt file.
2. Press '2' to decrypt file.
3. Press '3' to exit.
3

test.txt file

test.txt.enc file:

4
ECB(Electronic Code Block Chaining):
The Electronic Code Book (ECB) mode uses simple substitution, making
it one of the easiest and fastest algorithms to implement. The input
plaintext is broken into a number of blocks, and encrypted individually
using the key. This allows each encrypted block to be decrypted
individually. Encrypting the same block twice will result in the same
ciphertext being returned twice.

Algorithm:
Step-1: Start
Step-2: The user takes the first block of plaintext and encrypts it with the
key to produce the first block of ciphertext.
Step-3: He then takes the second block of plaintext and follows the same
process with same key and so on so forth
Step-4: End

Code:
# ECB(Electronic Code Block)
# AES(Advanced Encryption Standard Algorithm)
from Crypto import Random
from Crypto.Cipher import AES
import os
import os.path
import time

class Encryptor:
def __init__(self, key):

5
self.key = key

def pad(self, s):


return s + b"\0" * (AES.block_size - len(s) % AES.block_size)

def encrypt(self, message, key, key_size=256):


message = self.pad(message)
cipher = AES.new(key, AES.MODE_ECB)
return cipher.encrypt(message)
def encrypt_file(self, file_name):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
enc = self.encrypt(plaintext, self.key)
with open(file_name + ".enc", 'wb') as fo:
fo.write(enc)
os.remove(file_name)

def decrypt(self, ciphertext, key):


cipher = AES.new(key, AES.MODE_ECB)
plaintext = cipher.decrypt(ciphertext)
return plaintext.rstrip(b"\0")

def decrypt_file(self, file_name):


with open(file_name, 'rb') as fo:
ciphertext = fo.read()
dec = self.decrypt(ciphertext, self.key)
with open(file_name[:-4], 'wb') as fo:
fo.write(dec)
os.remove(file_name)

key =
b'[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\x
df\xcb\xc4\x94\x9d(\x9e'
enc = Encryptor(key)
clear = lambda: os.system('cls')
while True:
choice = int(input("1. Press '1' to encrypt file.\n2. Press '2' to decrypt
file.\n3. Press '3' to exit.\n"))
clear()
if choice == 1:
enc.encrypt_file(str(input("Enter name of file to encrypt: ")))
elif choice == 2:
enc.decrypt_file(str(input("Enter name of file to decrypt: ")))

6
elif choice == 3:
exit()
else:
print("Please select a valid option!")

Output:
1. Press '1' to encrypt file.
2. Press '2' to decrypt file.
3. Press '3' to exit.
1
Enter name of file to encrypt: test.txt
1. Press '1' to encrypt file.
2. Press '2' to decrypt file.
3. Press '3' to exit.
2
Enter name of file to decrypt: test.txt.enc
1. Press '1' to encrypt file.
2. Press '2' to decrypt file.
3. Press '3' to exit.
3
test.txt file

test.txt.enc file

7
1b) Public Key Encryption:
Aim: To write a python program for the implementation of RSA
algorithm.

Theory:

Asymmetric encryption involves a mechanism called Public Key and


Private Key.  Everyone in the network can access the public key but the
private key is anonymous. The user generates a private key using a
function.

 To encrypt a message, one can use the public key.


 Send the message over a channel. The private key is generated on
the receiver side.
 The private key is used to decrypt the encrypted message.

The Rivest-Shamir-Adleman(RSA) Algorithm is a public-key crypto


algorithm. It is based on the principle that prime factorization of a large
composite number is tough.  Only the private key of the receiver can
decrypt the cipher message. RSA is a key pair generator.

Algorithm:
1. Choose two different large random prime numbers p and q
2. Calculate n = p q
3. n is the modulus for the public key and the private keys
4. Calculate  ϕ ( n ) = ( p − 1 ) ( q − 1 )
5. Choose an integer k such that 1 < k  < ϕ ( n ) and k is co-prime to ϕ
( n ) : k and ϕ ( n )  share no factors other than 1; gcd (k, ϕ ( n ))= 1.
6. k is released as the public key exponent
7. Compute d  to satisfy the  d k ≡ 1 ( mod ϕ ( n ) )  i.e.: d k = 1 + x ϕ ( n )
for some integer x
8. d is kept as the private key exponent

The public key consists of n and k.

The private key consists of p, q, and the private exponent d.

Code:
import math

message = int(input("Enter the message to be encrypted: "))


p = int(input())

8
q = int(input())
n = p*q
totient=(p-1)*(q-1)
def gcd(message,n):
if n==0:
return n
else:
return gcd(n,message%n)

for e in range(2,totient):
if gcd(e,totient)== 1:
break

def encrypt(me):
en = math.pow(me,e)
c = en % n
print("Encrypted Message is: ", c)
return c

def decrypt(c):
for i in range(e):
x = 1 + i*totient
if x % e == 0:
d = int(x/e)
break
de = math.pow(c,d)
decr = de% n
print("Decrypted Message is ", decr)
return decr

print("Original Message is: ", message)


c = encrypt(message)
decr = decrypt(c)

Output:
Enter the message to be encrypted: 4
3
5
Original Message is: 4
Encrypted Message is: 4.0
Decrypted Message is 4.0

9
Conclusion: By doing this experiment I get to know about how
practically advanced encryption algorithm works for ECB(Electronic
Code Block) and for CBC(Cde Block Chaining).Public key encryption
also learned in this for RSA algorithm.

10

You might also like