0% found this document useful (0 votes)
59 views

3DES Code

This Python code allows a user to encrypt or decrypt a file using Triple DES (TDES) by prompting them to select an operation, input a file path and TDES key. It then encodes the key, initializes the TDES cipher, reads the file bytes, performs the selected operation and writes the new bytes back to the file before printing that the operation is complete.

Uploaded by

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

3DES Code

This Python code allows a user to encrypt or decrypt a file using Triple DES (TDES) by prompting them to select an operation, input a file path and TDES key. It then encodes the key, initializes the TDES cipher, reads the file bytes, performs the selected operation and writes the new bytes back to the file before printing that the operation is complete.

Uploaded by

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

from Crypto.

Cipher import DES3


from hashlib import md5

# For selecting operation from given choice


while True:
print('Choose operation to be done:\n\t1- Encryption\n\t2- Decryption')
operation = input('Your Choice: ')
if operation not in ['1', '2']:
break

# Image / File Path for operation


file_path = input('File path: ')

# Key for performing Triple DES algorithm


key = input('TDES key: ')

# Encode given key to 16 byte ascii key with md5 operation


key_hash = md5(key.encode('ascii')).digest()

# Adjust key parity of generated Hash Key for Final Triple DES Key
tdes_key = DES3.adjust_key_parity(key_hash)

# Cipher with integration of Triple DES key, MODE_EAX for Confidentiality &
Authentication
# and nonce for generating random / pseudo random number which is used for
authentication protocol
cipher = DES3.new(tdes_key, DES3.MODE_EAX, nonce=b'0')

# Open & read file from given path


with open(file_path, 'rb') as input_file:
file_bytes = input_file.read()

if operation == '1':
# Perform Encryption operation
new_file_bytes = cipher.encrypt(file_bytes)
else:
# Perform Decryption operation
new_file_bytes = cipher.decrypt(file_bytes)

# Write updated values in file from given path


with open(file_path, 'wb') as output_file:
output_file.write(new_file_bytes)
print('Operation Done!')
break

You might also like