Cross-platform cryptographic library providing TLS sockets, asymmetric/symmetric encryption, and key operations using OS-native crypto libraries
—
Symmetric encryption and decryption operations using AES, DES, 3DES, RC2, and RC4 algorithms. All operations use OS-native crypto libraries for optimal performance and security updates through the operating system.
from typing import TupleAdvanced Encryption Standard (AES) operations with CBC mode and various padding schemes.
def aes_cbc_pkcs7_encrypt(key: bytes, data: bytes, iv: bytes = None) -> Tuple[bytes, bytes]:
"""
Encrypt data using AES-CBC with PKCS#7 padding.
Parameters:
- key: bytes - AES key (16, 24, or 32 bytes for AES-128/192/256)
- data: bytes - Data to encrypt
- iv: bytes - 16-byte initialization vector (None to generate random)
Returns:
Tuple of (iv, ciphertext) bytes
"""
def aes_cbc_pkcs7_decrypt(key: bytes, data: bytes, iv: bytes) -> bytes:
"""
Decrypt AES-CBC data with PKCS#7 padding.
Parameters:
- key: bytes - AES key (16, 24, or 32 bytes)
- data: bytes - Encrypted ciphertext
- iv: bytes - 16-byte initialization vector
Returns:
Decrypted plaintext data
"""
def aes_cbc_no_padding_encrypt(key: bytes, data: bytes, iv: bytes = None) -> Tuple[bytes, bytes]:
"""
Encrypt data using AES-CBC without padding.
Parameters:
- key: bytes - AES key (16, 24, or 32 bytes)
- data: bytes - Data to encrypt (must be multiple of 16 bytes)
- iv: bytes - 16-byte initialization vector (None to generate random)
Returns:
Tuple of (iv, ciphertext) bytes
"""
def aes_cbc_no_padding_decrypt(key: bytes, data: bytes, iv: bytes) -> bytes:
"""
Decrypt AES-CBC data without padding.
Parameters:
- key: bytes - AES key (16, 24, or 32 bytes)
- data: bytes - Ciphertext to decrypt
- iv: bytes - 16-byte initialization vector
Returns:
Decrypted plaintext data
"""Data Encryption Standard (DES) operations with CBC mode and PKCS#5 padding.
def des_cbc_pkcs5_encrypt(key: bytes, data: bytes, iv: bytes = None) -> Tuple[bytes, bytes]:
"""
Encrypt data using DES-CBC with PKCS#5 padding.
Parameters:
- key: bytes - DES key (8 bytes)
- data: bytes - Data to encrypt
- iv: bytes - 8-byte initialization vector (None to generate random)
Returns:
Tuple of (iv, ciphertext) bytes
"""
def des_cbc_pkcs5_decrypt(key: bytes, data: bytes, iv: bytes) -> bytes:
"""
Decrypt DES-CBC data with PKCS#5 padding.
Parameters:
- key: bytes - DES key (8 bytes)
- data: bytes - Ciphertext to decrypt
- iv: bytes - 8-byte initialization vector
Returns:
Decrypted plaintext data
"""Triple DES (3DES) operations with CBC mode and PKCS#5 padding.
def tripledes_cbc_pkcs5_encrypt(key: bytes, data: bytes, iv: bytes = None) -> Tuple[bytes, bytes]:
"""
Encrypt data using 3DES-CBC with PKCS#5 padding.
Parameters:
- key: bytes - 3DES key (16 or 24 bytes)
- data: bytes - Data to encrypt
- iv: bytes - 8-byte initialization vector (None to generate random)
Returns:
Tuple of (iv, ciphertext) bytes
"""
def tripledes_cbc_pkcs5_decrypt(key: bytes, data: bytes, iv: bytes) -> bytes:
"""
Decrypt 3DES-CBC data with PKCS#5 padding.
Parameters:
- key: bytes - 3DES key (16 or 24 bytes)
- data: bytes - Ciphertext to decrypt
- iv: bytes - 8-byte initialization vector
Returns:
Decrypted plaintext data
"""RC2 cipher operations with CBC mode and PKCS#5 padding.
def rc2_cbc_pkcs5_encrypt(key: bytes, data: bytes, iv: bytes = None) -> Tuple[bytes, bytes]:
"""
Encrypt data using RC2-CBC with PKCS#5 padding.
Parameters:
- key: bytes - RC2 key (5-16 bytes for 40-128 bit keys)
- data: bytes - Data to encrypt
- iv: bytes - 8-byte initialization vector (None to generate random)
Returns:
Tuple of (iv, ciphertext) bytes
"""
def rc2_cbc_pkcs5_decrypt(key: bytes, data: bytes, iv: bytes) -> bytes:
"""
Decrypt RC2-CBC data with PKCS#5 padding.
Parameters:
- key: bytes - RC2 key (5-16 bytes for 40-128 bit keys)
- data: bytes - Ciphertext to decrypt
- iv: bytes - 8-byte initialization vector
Returns:
Decrypted plaintext data
"""RC4 stream cipher operations (encryption and decryption use the same function).
def rc4_encrypt(key: bytes, data: bytes) -> bytes:
"""
Encrypt data using RC4 stream cipher.
Parameters:
- key: bytes - RC4 key (5-16 bytes for 40-128 bit keys)
- data: bytes - Data to encrypt
Returns:
Encrypted ciphertext
"""
def rc4_decrypt(key: bytes, data: bytes) -> bytes:
"""
Decrypt RC4 data (same operation as encryption).
Parameters:
- key: bytes - RC4 key (5-16 bytes for 40-128 bit keys)
- data: bytes - Ciphertext to decrypt
Returns:
Decrypted plaintext data
"""from oscrypto.symmetric import aes_cbc_pkcs7_encrypt, aes_cbc_pkcs7_decrypt
from oscrypto.util import rand_bytes
# Generate a 256-bit (32-byte) AES key
key = rand_bytes(32)
# Encrypt data
plaintext = b"This is a secret message that needs to be encrypted"
iv, ciphertext = aes_cbc_pkcs7_encrypt(key, plaintext, None)
# Decrypt data
decrypted = aes_cbc_pkcs7_decrypt(key, ciphertext, iv)
assert decrypted == plaintext
print(f"Original: {plaintext}")
print(f"Encrypted length: {len(ciphertext)} bytes")
print(f"Decrypted: {decrypted}")from oscrypto.symmetric import (
aes_cbc_pkcs7_encrypt, aes_cbc_pkcs7_decrypt,
des_cbc_pkcs5_encrypt, des_cbc_pkcs5_decrypt,
rc4_encrypt, rc4_decrypt
)
from oscrypto.util import rand_bytes
message = b"Secret data"
# AES-256 encryption
aes_key = rand_bytes(32)
aes_iv, aes_ciphertext = aes_cbc_pkcs7_encrypt(aes_key, message, None)
aes_decrypted = aes_cbc_pkcs7_decrypt(aes_key, aes_ciphertext, aes_iv)
# DES encryption
des_key = rand_bytes(8)
des_iv, des_ciphertext = des_cbc_pkcs5_encrypt(des_key, message, None)
des_decrypted = des_cbc_pkcs5_decrypt(des_key, des_ciphertext, des_iv)
# RC4 encryption
rc4_key = rand_bytes(16)
rc4_encrypted = rc4_encrypt(rc4_key, message)
rc4_decrypted = rc4_decrypt(rc4_key, rc4_encrypted)
# All should decrypt to original message
assert aes_decrypted == des_decrypted == rc4_decrypted == messagefrom oscrypto.symmetric import aes_cbc_pkcs7_encrypt, aes_cbc_pkcs7_decrypt
from oscrypto.util import rand_bytes
key = rand_bytes(16) # AES-128 key
iv = rand_bytes(16) # AES block size IV
plaintext = b"Message with custom IV"
# Encrypt with custom IV
returned_iv, ciphertext = aes_cbc_pkcs7_encrypt(key, plaintext, iv)
# Decrypt with same IV
decrypted = aes_cbc_pkcs7_decrypt(key, ciphertext, iv)
assert decrypted == plaintextInstall with Tessl CLI
npx tessl i tessl/pypi-oscrypto