Ciphering, Deciphering, Hashing and HMAC’ing In Python
Entering Python
3 min readMar 5, 2023
Configuration
hash_algorithm = "sha256"
timestamp_epoch_time_start = 0
timestamp_epoch_time_end = 10*365*24*60*60
key_length_RSA = 2048
message = b"Hello, world!"
Ciphering, Deciphering, Hashing And HMAC’ing Of Messages (Symmetric)
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives import hashes, hmackey = os.urandom(32) # Generate a 256-bit encryption key
nonce = os.urandom(12) # Generate a random 96-bit nonceplaintext = message # message to encrypt# Encrypt the message using AES-GCM
object_aesgcm = AESGCM(key)
ciphertext = object_aesgcm.encrypt(nonce, plaintext, None)"""
key = b"\x98\xe5\xe6\x5a\x1e\xf6\x13\x6e\xdc\xef\xbc\x9d\x6e\x3a\xfd\x5c\xc6\xf8\x32\x41\x84\xab\x84\x10\x1a\x5d\xfd\x07\x2d\xf8\x96\x15"
nonce = b"\xc3\x23\x35\x12\x4a\xb4\x7e\x4b\x00\x7c\x9e\xa2"
"""plaintext_recovered = object_aesgcm.decrypt(nonce, ciphertext, None)print(plaintext_recovered)
print(plaintext_recovered.decode("utf-8"))# Compute the SHA-256 hash of the ciphertext
digest = hashes.Hash(hashes.SHA256())
digest.update(ciphertext)
hash_value = digest.finalize()print("Key:", key.hex())
print("Nonce:"…