API reference

API reference for CryptoPyX package.

cryptopyx

CryptoPyX - a python cryptography package written in Rust made for speed.

cryptopyx.ciphers

Ciphers submodule.

cryptopyx.ciphers.aes

AES encryption and decryption. Currently only supports ECB mode. Work in progress.

cryptopyx.ciphers.aes.decrypt_ecb(data: bytes, key: bytes, padding_mode: str) bytes

Decrypt data using AES ECB mode with the inputted padding mode.

Parameters:
  • data (bytes) – The data to be decrypted.

  • key (bytes) – The key to decrypt the data with. Can be 128, 196 and 256 bytes, as per AES standards.

  • padding_mode (str) – The padding to use for decryption. Can be any of the following: pkcs, PKCS, pkcs#7, PKCS#7, for PKCS#7 padding, and ISO, iso, ISO/IEC, iso/iec, for ISO/IEC 9797-1 Padding Method 2.

Returns:

The decrypted and unpadded data.

Raises:

ValueError – If the padding_mode argument is invalid, or if the key size is invalid.

Return type:

bytes

cryptopyx.ciphers.aes.encrypt_ecb(data: bytes, key: bytes, padding_mode: str) bytes

Encrypt data using AES ECB mode with the inputted padding mode.

Parameters:
  • data (bytes) – The data to be encrypted.

  • key (bytes) – The key to encrypt the data with. Can be 128, 196 and 256 bytes, as per AES standards.

  • padding_mode (str) – The padding to use for encryption. Can be any of the following: pkcs, PKCS, pkcs#7, PKCS#7, for PKCS#7 padding, and ISO, iso, ISO/IEC, iso/iec, for ISO/IEC 9797-1 Padding Method 2.

Returns:

The encrypted and padded data.

Raises:

ValueError – If the padding_mode argument is invalid, or if the key size is invalid.

Return type:

bytes

cryptopyx.ciphers.caesar

Caesar shift encryption and decryption. Ignores all non alphabetical characters.

cryptopyx.ciphers.caesar.decrypt(data: str, shift: int) str

Decrypt a string using a caesar shift.

Equivalent to using encrypt with the sign of the shift flipped.

e.g. caesar.encrypt(‘Hello’, 5) is the same as caesar.decrypt(‘Hello’, -5)

Parameters:
  • data (str) – The string to be decrypted.

  • shift (int) – How much to shift each letter. Should be the same as the one used to encrypt the data.

Returns:

The decrypted string.

Raises:

ValueError – If the shift is not in the range -25 to 25 (inclusive).

Return type:

str

cryptopyx.ciphers.caesar.encrypt(data: str, shift: int) str

Encrypt a string using a caesar shift.

Equivalent to using decrypt with the sign of the shift flipped.

e.g. caesar.encrypt(‘Hello’, 5) is the same as caesar.decrypt(‘Hello’, -5)

Parameters:
  • data (str) – The string to be encrypted.

  • shift (int) – How much to shift each letter.

Returns:

The encrypted string.

Raises:

ValueError – If the shift is not in the range -25 to 25 (inclusive).

Return type:

str

cryptopyx.ciphers.vigenere

Vigenere shift encryption and decryption.

cryptopyx.ciphers.vigenere.decrypt(data: str, key: str, skip_non_alpha: bool = True) str

Decrypts string using the provided key.

Parameters:
  • data (str) – The string to decrypt.

  • key (str) – The key to decrypt the data.

  • skip_non_alpha (bool) – Whether to skip non-alphabetic characters (optional, default is True).

Returns:

The decrypted string.

Raises:

ValueError – If the key is invalid, i.e. it’s empty or contains non-alphabetic characters, though lowercase and uppercase characters are both supported. or, if data is non-ascii.

Return type:

str

cryptopyx.ciphers.vigenere.encrypt(data: str, key: str, skip_non_alpha: bool = True) str

Encrypts string using the provided key.

Parameters:
  • data (str) – The string to encrypt.

  • key (str) – The key to encrypt the data.

  • skip_non_alpha (bool) – Whether to skip non-alphabetic characters (optional, default is True).

Returns:

The encrypted string.

Raises:

ValueError – If the key is invalid, i.e. it’s empty or contains non-alphabetic characters, though lowercase and uppercase characters are both supported. or, if data is non-ascii.

Return type:

str

cryptopyx.ciphers.rot13

Rot13 shift encryption and decryption. Ignores all non alphabetical characters.

cryptopyx.ciphers.rot13.decrypt(data: str) str

“Decrypt” a string using rot13.

Not really decryption as rot13 decryption is the exact same as rot13 encryption

Parameters:

data (str) – The string to be decrypted.

Returns:

The “decrypted” string

Return type:

str

cryptopyx.ciphers.rot13.encrypt(data: str) str

“Encrypt” a string using rot13.

Not really encryption as rot13 encryption is the exact same as rot13 decryption

Parameters:

data (str) – The string to be encrypted.

Returns:

The “encrypted” string

Return type:

str

cryptopyx.ciphers.rot13.rotate(data: str) str

Rotates a string using rot13.

Same as decrypt() and encrypt().

Parameters:

data (str) – The string to be rotated.

Returns:

The rotated string

Return type:

str

cryptopyx.ciphers.substitution

Simple custom substitution ciphers.

cryptopyx.ciphers.substitution.substitute(data: str, mapping: dict[str, str]) str

Substitutes characters in the input string according to the provided mapping.

Parameters:
  • data (str) – The string to perform substitution on.

  • mapping (dict[str, str]) – A dictionary defining character substitutions, where keys are characters to be replaced and values are their replacements.

Returns:

The string after applying the substitutions.

Return type:

str

cryptopyx.ciphers.substitution.substitute_bytes(data: bytes, mapping: dict[int, int]) str

Substitutes characters in the input bytes according to the provided mapping.

Parameters:
  • data (bytes) – The bytes to perform substitution on.

  • mapping (dict[int, int]) – A dictionary defining character substitutions, where keys are integers of the charactersto be replaced and values are their replacements.

Returns:

The bytes after applying the substitutions.

Return type:

str

cryptopyx.ciphers.substitution.substitute_reverse(data: str, mapping: dict[str, str]) str

Substitutes characters in the input string according to the reverse of the provided mapping.

Parameters:
  • data (str) – The string to perform substitution on.

  • mapping (dict[str, str]) – A dictionary defining character substitutions, where keys are the replacements and values are what they are to be replaced with.

Returns:

The string after applying the substitutions.

Return type:

str

cryptopyx.ciphers.substitution.substitute_reverse_bytes(data: bytes, mapping: dict[int, int]) str

Substitutes characters in the input bytes according to the reverse of the provided mapping.

Parameters:
  • data (bytes) – The bytes to perform substitution on.

  • mapping (dict[int, int]) – A dictionary defining character substitutions, where keys are integers of the the replacements and values are what they are to be replaced with.

Returns:

The bytes after applying the substitutions.

Return type:

str

cryptopyx.encodings

Encodings submodule.

cryptopyx.encodings.base32

Base32 encoding and decoding.

Currently only supports the standard base 32 alphabet (RFC 4648).

cryptopyx.encodings.base32.decode(data: str, strict: bool = False) str

Decodes string using base32.

Parameters:
  • data (str) – The string to be decoded.

  • strict (bool) – Ensure string is correct length. Defaults to False. (optional)

Returns:

Decoded string.

Raises:

ValueError – If the string is invalid base32.

Return type:

str

cryptopyx.encodings.base32.decode_bytes(data: bytes) bytes

Decode bytes using base32.

Parameters:

data (bytes) – The data to be decoded.

Returns:

Decoded bytes.

Raises:

ValueError – If the bytes are invalid base32.

Return type:

bytes

cryptopyx.encodings.base32.encode(data: str) str

Encodes string using base32.

Parameters:

data (str) – The string to be encoded.

Returns:

Encoded base32 string.

Return type:

str

cryptopyx.encodings.base32.encode_bytes(data: bytes) bytes

Encode bytes using base32.

Parameters:

data (bytes) – The data to be encoded.

Returns:

Encoded base32 bytes.

Return type:

bytes

cryptopyx.encodings.base64

Base64 encoding and decoding functions.

Currently only supports standard base64 alphabet (RFC 4648).

cryptopyx.encodings.base64.decode(data: str) str

Decodes base64 string.

Parameters:
  • data (str) – The base64 string to be decoded.

  • strict – Ensure string is correct length. Defaults to False. (optional)

Returns:

Decoded base64 string.

Raises:

ValueError – If the string is invalid base64.

Return type:

str

cryptopyx.encodings.base64.decode_bytes(data: bytes) bytes

Decode bytes using base64.

Parameters:

data (bytes) – The data to be decoded.

Returns:

Decoded base64 bytes.

Raises:

ValueError – If the bytes are invalid base32.

Return type:

bytes

cryptopyx.encodings.base64.encode(data: str) str

Encodes string using base64.

Parameters:

data (str) – The string to be encoded.

Returns:

Encoded base64 string.

Return type:

str

cryptopyx.encodings.base64.encode_bytes(data: bytes) bytes

Encode bytes using base64.

Parameters:

data (bytes) – The data to be encoded.

Returns:

Encoded base64 bytes.

Return type:

bytes