Examples ======== This page contains complete examples demonstrating common uses of DES PurePy. Encrypting a Single Block ------------------------- The following example encrypts a single 64-bit block using a hexadecimal key. .. code-block:: python from des_PurePy import DES des = DES("0x133457799bbcdff1") ciphertext = des.encrypt("0x0123456789abcdef") print(ciphertext) Output: .. code-block:: text 0x85e813540f0ab405fdf2e174492922f8 Decrypting Data --------------- Decrypt the ciphertext using the same key. .. code-block:: python from des_PurePy import DES des = DES("0x133457799bbcdff1") cleartext = des.decrypt("0x85e813540f0ab405fdf2e174492922f8") print(cleartext) Output: .. code-block:: text 0x0123456789abcdef Encrypting Text --------------- The library accepts ``bytes`` objects, making it easy to encrypt text. .. code-block:: python from des_PurePy import DES des = DES(b"password") ciphertext = des.encrypt(b"secret message") print(ciphertext) Decrypting Text --------------- After decrypting, convert the returned hexadecimal string back into bytes and decode it. .. code-block:: python from des_PurePy import DES des = DES(b"password") ciphertext = des.encrypt(b"secret message") cleartext = des.decrypt(ciphertext) message = bytes.fromhex(cleartext[2:]).decode("utf-8") print(message) Output: .. code-block:: text secret message Working with Binary Files ------------------------- DES PurePy accepts ``bytes`` objects, so files can be encrypted by reading them as binary data. .. code-block:: python from des_PurePy import DES des = DES(b"password") with open("input.bin", "rb") as infile: plaintext = infile.read() ciphertext = des.encrypt(plaintext) # Save the ciphertext with open("ciphertext.txt", "w", encoding="utf-8") as outfile: outfile.write(ciphertext) # Recover the original bytes recovered = bytes.fromhex(des.decrypt(ciphertext)[2:]) with open("output.bin", "wb") as outfile: outfile.write(recovered)