Encrypted Content Encoding for HTTP

1.2.1 · active · verified Sat Apr 11

http-ece is a Python library that implements Encrypted Content Encoding for HTTP, primarily used in contexts like Web Push to secure payload data. It provides functions to encrypt and decrypt arbitrary byte strings using AES-GCM with a derived keying material. The current version is 1.2.1, and the library has an infrequent release cadence, with the most recent update in August 2024, indicating active maintenance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to encrypt and decrypt a simple byte string using `http-ece` for content encoding. It uses randomly generated keys and salts, which must be securely managed and shared in a production environment. The example focuses on `aes128gcm` version for content encryption without Diffie-Hellman key agreement.

import os
from http_ece import encrypt, decrypt

# --- Basic Content Encryption/Decryption ---
# This example demonstrates content encryption without Diffie-Hellman key agreement.
# In a real Web Push scenario, 'auth_secret' and 'salt' are often derived
# or exchanged as part of the Web Push protocol.

# Generate a random content encryption key (CEK) and salt
# In a real application, securely manage and transport these values.
cek = os.urandom(16)
salt = os.urandom(16)
auth_secret = os.urandom(16) # A secret known to both sender and receiver

plaintext_data = b"This is a secret message to be encrypted."

# Encrypt the plaintext
encrypted_payload, record_size = encrypt(
    plaintext_data,
    private_key=None, # Not used for simple content encryption
    dh=None,          # Not used for simple content encryption
    auth_secret=auth_secret,
    salt=salt,
    keyid=b'',
    key=cek,
    version='aes128gcm' # Recommended version
)

print(f"Original plaintext: {plaintext_data.decode()}")
print(f"Encrypted payload (hex): {encrypted_payload.hex()}")
print(f"Record size used for encryption: {record_size}")

# Decrypt the payload
decrypted_data = decrypt(
    encrypted_payload,
    private_key=None, # Not used for simple content encryption
    dh=None,          # Not used for simple content encryption
    auth_secret=auth_secret,
    salt=salt,
    keyid=b'',
    key=cek,
    rs=record_size,   # Must be the same record size used for encryption
    version='aes128gcm'
)

print(f"Decrypted plaintext: {decrypted_data.decode()}")

assert plaintext_data == decrypted_data
print("Encryption and decryption successful!")

view raw JSON →