COMP4337 Lab1 Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

COMP4337 Lab1 Report

z5270429

Code
tempdes.py

from Crypto.Cipher import DES


import sys
import time

iv = bytes.fromhex(sys.argv[1])
key = bytes.fromhex(sys.argv[2])

inputfiledir = sys.argv[3]
outputfiledir = sys.argv[4]

print('=' * 100)
print('Key used: ', [x for x in key])

print("IV used: ",[x for x in iv])


print('=' * 100)

des1 = DES.new(key, DES.MODE_CBC, iv)


des2 = DES.new(key, DES.MODE_CBC, iv)

with open(inputfiledir, 'rb') as infile:


plain_text = infile.read()

# padding to make last block length a multiple of 8


padded_text = plain_text
remainder = len(plain_text) % 8
if remainder > 0:
length = 8 - remainder
padded_text += b'\x00' * length

# encrypt padded text


start_time = time.time()
cipher_text = des1.encrypt(padded_text)
encryption_time = time.time() - start_time

with open(outputfiledir, 'wb') as outfile:


outfile.write(cipher_text)
outfile.close()

print('Plaintext is:', plain_text)


print('Ciphertext is:', cipher_text)
start_time = time.time()
original_msg = des2.decrypt(cipher_text)
decryption_time = time.time() - start_time

print('Original Message:', original_msg.decode('utf-8'))


print('=' * 100)

print('Encryption time:', encryption_time * 1000000)

print('Decryption time:', decryption_time * 1000000)

tempaes.py

from Crypto.Cipher import AES


from Crypto import Random
import sys
import time

iv = Random.get_random_bytes(16)
key = Random.get_random_bytes(16)

inputfiledir = sys.argv[1]
outputfiledir = sys.argv[2]

print('=' * 100)
print('Key used: ', [x for x in key])

print("IV used: ",[x for x in iv])


print('=' * 100)

aes1 = AES.new(key, AES.MODE_CBC, iv)


aes2 = AES.new(key, AES.MODE_CBC, iv)

with open(inputfiledir, 'rb') as infile:


plain_text = infile.read()
# pad to make last block length a multiple of 16
padded_text = plain_text
remainder = len(plain_text) % 16

if remainder > 0:
length = 16 - remainder
padded_text += b'\x00' * length

# encrypt padded text


start_time = time.time()
cipher_text = aes1.encrypt(padded_text)
encryption_time = time.time() - start_time

with open(outputfiledir, 'wb') as outfile:


outfile.write(cipher_text)
outfile.close()

print('Plaintext is:', plain_text)


print('Ciphertext is:', cipher_text)

start_time = time.time()
original_msg = aes2.decrypt(cipher_text)
decryption_time = time.time() - start_time

print('Original Message:', original_msg.decode('utf-8'))


print('=' * 100)

print('Encryption time:', encryption_time * 1000000)


print('Decryption time:', decryption_time * 1000000)

temprsa.py

import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import ast
import sys
import time

inputfiledir = sys.argv[1]
timefiledir = sys.argv[2]

random_generator = Random.new().read
key = RSA.generate(1024, random_generator)

publickey = key.publickey()

print('=' * 100)

input = open(inputfiledir,"r")
plain_text = input.read()

encrypt_start = time.time()
cipher_text = publickey.encrypt(plain_text, 32)
encrypt_end = time.time()

print ('Plaintext encrypted using Public Key is:', cipher_text)

decrypt_start = time.time()
decrypted = key.decrypt(ast.literal_eval(str(cipher_text)))
decrypt_end = time.time()

print ('Ciphertext decrypted with Private key is', decrypted)


print ('=' * 100)
with open(timefiledir, 'a') as timefile:
timefile.write("encrypt time = ")
timefile.write(str((encrypt_end - encrypt_start)*1000000))
timefile.write("\n")
timefile.write("decrypt time = ")
timefile.write(str((decrypt_end - decrypt_start)*1000000))
timefile.write("\n")

Graphs

DES Encryption and Decryption Times

Encryption

Decryption
AES Encryption and Decryption Times

Encryption

Decryption
RSA Encryption and Decryption Times

Encryption

Decryption
SHA-1 Digest Times

Questions
1) DES takes longer times than AES for encrypting and decrypting. From the
times we see that AES is almost 4x faster than DES when encrypting
2047152 bytes. When decrypting AES is almost 7x faster than DES.

2) DES is faster and takes shorter times than RSA. DES is approximately 2x
faster than RSA.

3) SHA-1 is much faster than DES encryption. SHA-1 is approximately 13x


faster than DES when encrypting 4096 bytes. SHA-1 is a hashing
algorithm whereas DES is a symmetric encryption process.

4) ?

5) RSA encryption is faster than decryption. The secret key while encrypting
using RSA is acquired by using an algorithm on the public key.

You might also like