import pyDes
def encrypt_file(filename, key, iv):
with open(filename, 'rb') as f:
plaintext = f.read()
k = pyDes.des(key, pyDes.CBC, iv, pad=None, padmode=pyDes.PAD_PKCS5)
ciphertext = k.encrypt(plaintext)
with open(filename + ".enc", 'wb') as f:
f.write(ciphertext)
def decrypt_file(filename, key, iv):
ciphertext = f.read()
plaintext = k.decrypt(ciphertext)
with open(filename[:-4], 'wb') as f:
f.write(plaintext)
key = b"DESKEY--"
iv = b"\0\0\0\0\0\0\0\0"
encrypt_file("test.txt", key, iv)
decrypt_file("test.txt.enc", key, iv)
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
import pyDes
def encrypt_file(filename, key, iv):
with open(filename, 'rb') as f:
plaintext = f.read()
k = pyDes.des(key, pyDes.CBC, iv, pad=None, padmode=pyDes.PAD_PKCS5)
ciphertext = k.encrypt(plaintext)
with open(filename + ".enc", 'wb') as f:
f.write(ciphertext)
def decrypt_file(filename, key, iv):
with open(filename, 'rb') as f:
ciphertext = f.read()
k = pyDes.des(key, pyDes.CBC, iv, pad=None, padmode=pyDes.PAD_PKCS5)
plaintext = k.decrypt(ciphertext)
with open(filename[:-4], 'wb') as f:
f.write(plaintext)
key = b"DESKEY--"
iv = b"\0\0\0\0\0\0\0\0"
encrypt_file("test.txt", key, iv)
decrypt_file("test.txt.enc", key, iv)