TP03 pbenc_aes_ctr_hmac in python
This commit is contained in:
parent
7202cf26b9
commit
4826f2fe75
4 changed files with 130 additions and 0 deletions
128
TPs/TP03/py/pbenc_aes_ctr_hmac.py
Normal file
128
TPs/TP03/py/pbenc_aes_ctr_hmac.py
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||||
|
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
||||||
|
from cryptography.hazmat.primitives import hashes
|
||||||
|
from cryptography.hazmat.primitives import hmac
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def encrypt(input_file, password):
|
||||||
|
inp = open(input_file,"rb")
|
||||||
|
out = open(f"{input_file}.enc","wb")
|
||||||
|
|
||||||
|
plaintext = inp.read()
|
||||||
|
print(f"plaintext len : {len(plaintext)}")
|
||||||
|
|
||||||
|
|
||||||
|
# Derive the key from the password using PBKDF2
|
||||||
|
salt = os.urandom(16)
|
||||||
|
kdf = PBKDF2HMAC(
|
||||||
|
algorithm=hashes.SHA256(),
|
||||||
|
length=64,
|
||||||
|
salt=salt,
|
||||||
|
iterations=100000
|
||||||
|
)
|
||||||
|
# FIX: key length must be 32 bytes
|
||||||
|
derived_key = kdf.derive(password.encode('utf-8'))
|
||||||
|
|
||||||
|
key = derived_key[:32]
|
||||||
|
hmac_key = derived_key[32:]
|
||||||
|
|
||||||
|
iv = os.urandom(16)
|
||||||
|
|
||||||
|
cipher = Cipher(algorithms.AES(key),modes.CTR(iv))
|
||||||
|
encryptor = cipher.encryptor()
|
||||||
|
ciphertext = encryptor.update(plaintext)
|
||||||
|
|
||||||
|
ciphertext = salt + iv + ciphertext
|
||||||
|
|
||||||
|
print(f"plaintext len : {len(plaintext)}")
|
||||||
|
print(f"ciphertext len : {len(ciphertext)}")
|
||||||
|
print(f"iv len : {len(iv)}")
|
||||||
|
|
||||||
|
h = hmac.HMAC(hmac_key,hashes.SHA256())
|
||||||
|
h.update(ciphertext)
|
||||||
|
tag = h.finalize()
|
||||||
|
ciphertext = ciphertext + tag
|
||||||
|
|
||||||
|
out.write(ciphertext)
|
||||||
|
|
||||||
|
inp.close()
|
||||||
|
out.close()
|
||||||
|
|
||||||
|
def decrypt(input_file,password):
|
||||||
|
inp = open(f"{input_file}","rb")
|
||||||
|
out = open(f"{input_file}.dec","wb")
|
||||||
|
|
||||||
|
input_bytes = inp.read()
|
||||||
|
salt = input_bytes[:16]
|
||||||
|
iv = input_bytes[16:32]
|
||||||
|
ciphertext = input_bytes[32:-32]
|
||||||
|
tag = input_bytes[-32:]
|
||||||
|
|
||||||
|
kdf = PBKDF2HMAC(
|
||||||
|
algorithm=hashes.SHA256(),
|
||||||
|
length=64,
|
||||||
|
salt=salt,
|
||||||
|
iterations=100000
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f"plaintext len : {len(ciphertext)}")
|
||||||
|
print(f"iv len : {len(iv)}")
|
||||||
|
print(f"salt len : {len(salt)}")
|
||||||
|
|
||||||
|
derived_key = kdf.derive(password.encode('utf-8'))
|
||||||
|
hmac_key = derived_key[32:]
|
||||||
|
key = derived_key[:32]
|
||||||
|
|
||||||
|
# FIX: block size for aes must be 16 bytes
|
||||||
|
# plaintext needs padding
|
||||||
|
cipher = Cipher(algorithms.AES(key),modes.CTR(iv))
|
||||||
|
decryptor = cipher.decryptor()
|
||||||
|
plaintext = decryptor.update(ciphertext)
|
||||||
|
|
||||||
|
h = hmac.HMAC(hmac_key,hashes.SHA256())
|
||||||
|
h.update(salt + iv + ciphertext)
|
||||||
|
if (h.finalize() != tag):
|
||||||
|
print("Error: HMAC verification failed")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
out.write(plaintext)
|
||||||
|
|
||||||
|
inp.close()
|
||||||
|
out.close()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Program to perform operations using AES cipher on files",
|
||||||
|
)
|
||||||
|
|
||||||
|
subparsers = parser.add_subparsers(dest="operation", help="Operation to perform")
|
||||||
|
|
||||||
|
# Encrypt subcommand
|
||||||
|
enc_parser = subparsers.add_parser("enc", help="Encrypt a file")
|
||||||
|
enc_parser.add_argument("fich", help="File to be encrypted")
|
||||||
|
enc_parser.add_argument("password", help="Pass-phrase to derive the key")
|
||||||
|
|
||||||
|
# Decrypt subcommand
|
||||||
|
dec_parser = subparsers.add_parser("dec", help="Decrypt a file")
|
||||||
|
dec_parser.add_argument("fich", help="File to be decrypted")
|
||||||
|
dec_parser.add_argument("password", help="Pass-phrase to derive the key")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
match args.operation:
|
||||||
|
case "enc":
|
||||||
|
input_file = args.fich
|
||||||
|
password = args.password
|
||||||
|
encrypt(input_file,password)
|
||||||
|
case "dec":
|
||||||
|
input_file = args.fich
|
||||||
|
password = args.password
|
||||||
|
decrypt(input_file,password)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
1
TPs/TP03/py/plaintext.txt
Normal file
1
TPs/TP03/py/plaintext.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
not very safe
|
BIN
TPs/TP03/py/plaintext.txt.enc
Normal file
BIN
TPs/TP03/py/plaintext.txt.enc
Normal file
Binary file not shown.
1
TPs/TP03/py/plaintext.txt.enc.dec
Normal file
1
TPs/TP03/py/plaintext.txt.enc.dec
Normal file
|
@ -0,0 +1 @@
|
||||||
|
not very safe
|
Loading…
Reference in a new issue