EncryptionEncoding

class chepy.modules.encryptionencoding.EncryptionEncoding(*data)

This class handles most operations related to various encryption related operations. This class inherits the ChepyCore class, and all the methods are also available from the Chepy class

Examples

>>> from chepy import Chepy
or
>>> from chepy.modules.encryptionencoding import EncryptionEncoding
aes_decrypt(key: str, iv: str = '00000000000000000000000000000000', mode: str = 'CBC', key_format: str = 'hex', iv_format: str = 'hex') EncryptionEncodingT

Decrypt raw state encrypted with AES. CFB mode reflects Cyberchef and not native python behaviour.

Parameters
  • key (str) – Required. The secret key

  • iv (str, optional) – IV for certain modes only. Defaults to ‘00000000000000000000000000000000’.

  • mode (str, optional) – Encryption mode. Defaults to ‘CBC’.

  • key_format (str, optional) – Format of key. Defaults to ‘hex’.

  • iv_format (str, optional) – Format of IV. Defaults to ‘hex’.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> c = Chepy("5fb8c186394fc399849b89d3b6605fa3")
>>> c.hex_to_str()
>>> c.aes_decrypt("7365637265742070617373776f726421")
>>> c.o
b"some data"
aes_encrypt(key: str, iv: str = '00000000000000000000000000000000', mode: str = 'CBC', key_format: str = 'hex', iv_format: str = 'hex') EncryptionEncodingT

Encrypt raw state with AES. CFB mode reflects Cyberchef and not native python behaviour.

Parameters
  • key (str) – Required. The secret key

  • iv (str, optional) – IV for certain modes only. Defaults to ‘00000000000000000000000000000000’.

  • mode (str, optional) – Encryption mode. Defaults to ‘CBC’.

  • key_format (str, optional) – Format of key. Defaults to ‘hex’.

  • iv_format (str, optional) – Format of IV. Defaults to ‘hex’.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("some data").aes_encrypt("secret password!", mode="ECB").o
b"5fb8c186394fc399849b89d3b6605fa3"
affine_decode(a: int = 1, b: int = 1) EncryptionEncodingT

Decode Affine cipher

Parameters
  • a (int, optional) – Multiplier value. Defaults to 1

  • b (int, optional) – Additive value. Defaults to 1

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("TFDSFU").affine_decode().o
"SECRET"
affine_encode(a: int = 1, b: int = 1) EncryptionEncodingT

Encode with Affine cipher

Parameters
  • a (int, optional) – Multiplier value. Defaults to 1

  • b (int, optional) – Additive value. Defaults to 1

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("secret").affine_encode().o
"TFDSFU"
atbash_decode() EncryptionEncodingT

Decode Atbash cipher

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("hvxivg").atbash_decode().o
"SECRET"
atbash_encode() EncryptionEncodingT

Encode with Atbash cipher

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("secret").atbash_encode().o
"HVXIVG"
bifid_decode(key: Union[str, bytes] = '')

Bifid / polybius decode

Parameters

key (Union[str, bytes], optional) – Key. Defaults to “”.

Returns

The Chepy object.

Return type

Chepy

bifid_encode(key: Union[bytes, str] = '') EncryptionEncodingT

Bifid / polybius decode

Parameters

key (Union[str, bytes], optional) – Key. Defaults to “”.

Returns

The Chepy object.

Return type

Chepy

blowfish_decrypt(key: str, iv: str = '0000000000000000', mode: str = 'CBC', key_format: str = 'hex', iv_format: str = 'hex') EncryptionEncodingT

Encrypt raw state with Blowfish

Parameters
  • key (str) – Required. The secret key

  • iv (str, optional) – IV for certain modes only. Defaults to ‘00000000000000000000000000000000’.

  • mode (str, optional) – Encryption mode. Defaults to ‘CBC’.

  • key_format (str, optional) – Format of key. Defaults to ‘hex’.

  • iv_format (str, optional) – Format of IV. Defaults to ‘hex’.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> c = Chepy("d9b0a79853f13960fcee3cae16e27884")
>>> c.hex_to_str()
>>> c.blowfish_decrypt("password", key_format="utf-8")
>>> c.o
b"some data"
blowfish_encrypt(key: str, iv: str = '0000000000000000', mode: str = 'CBC', key_format: str = 'hex', iv_format: str = 'hex') EncryptionEncodingT

Encrypt raw state with Blowfish

Parameters
  • key (str) – Required. The secret key

  • iv (str, optional) – IV for certain modes only. Defaults to ‘0000000000000000’.

  • mode (str, optional) – Encryption mode. Defaults to ‘CBC’.

  • key_format (str, optional) – Format of key. Defaults to ‘hex’.

  • iv_format (str, optional) – Format of IV. Defaults to ‘hex’.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("some data").blowfish_encrypt("password", mode="ECB").o
b"d9b0a79853f139603951bff96c3d0dd5"
cetacean_decode() EncryptionEncodingT

Cetacean decode

Returns

The Chepy object.

Return type

Chepy

cetacean_encode() EncryptionEncodingT

Cetacean encode

Returns

The Chepy object.

Return type

Chepy

chacha_decrypt(key: str, nonce: str = '0000000000000000', key_format: str = 'hex', nonce_format: str = 'hex') EncryptionEncodingT

Decrypt raw state encrypted with ChaCha 20 rounds.

Parameters
  • key (str) – Required. The secret key

  • nonce (str, optional) – nonce for certain modes only. Defaults to ‘0000000000000000’.

  • key_format (str, optional) – Format of key. Defaults to ‘hex’.

  • nonce_format (str, optional) – Format of nonce. Defaults to ‘hex’.

Returns

The Chepy object.

Return type

Chepy

chacha_encrypt(key: str, nonce: str = '0000000000000000', key_format: str = 'hex', nonce_format: str = 'hex') EncryptionEncodingT

Encrypt raw state with ChaCha 20 rounds

Parameters
  • key (str) – Required. The secret key

  • nonce (str, optional) – Nonce. Defaults to ‘0000000000000000’.

  • key_format (str, optional) – Format of key. Defaults to ‘hex’.

  • nonce_format (str, optional) – Format of nonce. Defaults to ‘hex’.

Returns

The Chepy object.

Return type

Chepy

des_decrypt(key: str, iv: str = '0000000000000000', mode: str = 'CBC', key_format: str = 'hex', iv_format: str = 'hex') EncryptionEncodingT

Decrypt raw state encrypted with DES.

Parameters
  • key (str) – Required. The secret key

  • iv (str, optional) – IV for certain modes only. Defaults to ‘0000000000000000’.

  • mode (str, optional) – Encryption mode. Defaults to ‘CBC’.

  • key_format (str, optional) – Format of key. Defaults to ‘hex’.

  • iv_format (str, optional) – Format of IV. Defaults to ‘hex’.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("1ee5cb52954b211d1acd6e79c598baac").hex_to_str().des_decrypt("password").o
b"some data"
des_encrypt(key: str, iv: str = '0000000000000000', mode: str = 'CBC', key_format: str = 'hex', iv_format: str = 'hex') EncryptionEncodingT

Encrypt raw state with DES

Parameters
  • key (str) – Required. The secret key

  • iv (str, optional) – IV for certain modes only. Defaults to ‘0000000000000000’.

  • mode (str, optional) – Encryption mode. Defaults to ‘CBC’.

  • key_format (str, optional) – Format of key. Defaults to ‘hex’.

  • iv_format (str, optional) – Format of IV. Defaults to ‘hex’.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("some data").des_encrypt("70617373776f7264").o
b"1ee5cb52954b211d1acd6e79c598baac"

To encrypt using a different mode

>>> Chepy("some data").des_encrypt("password", mode="CTR").o
b"0b7399049b0267d93d"
fernet_decrypt(key: Union[bytes, str], encode_key: bool = False) EncryptionEncodingT

Fernet decrypt

Parameters
  • key (Union[bytes, str]) – Key to encrypt with. This should be 32 bytes long

  • encode_key (bool, optional) – If key should be base64 encoded. Defaults to False.

Returns

The Chepy object.

Return type

Chepy

fernet_encrypt(key: Union[bytes, str], encode_key: bool = False) EncryptionEncodingT

Fernet encrypt

Parameters
  • key (Union[bytes, str]) – Key to encrypt with. This should be 32 bytes long

  • encode_key (bool, optional) – If key should be base64 encoded. Defaults to False.

Returns

The Chepy object.

Return type

Chepy

from_letter_number_code(delimiter: Union[str, bytes] = ' ', join_by: Union[str, bytes] = '') EncryptionEncodingT

Decode A1Z26

Parameters
  • delimiter (Union[str, bytes], optional) – Split on. Defaults to ‘ ‘.

  • join_by (Union[str, bytes], optional) – Join output by. Defaults to ‘’.

Returns

The Chepy object.

Return type

Chepy

from_morse_code(dot: str = '.', dash: str = '-', letter_delim: str = ' ', word_delim: str = '\n') EncryptionEncodingT

Decode morse code

Parameters
  • dot (str, optional) – The char for dot. Defaults to “.”.

  • dash (str, optional) – The char for dash. Defaults to “-“.

  • letter_delim (str, optional) – Letter delimiter. Defaults to ” “.

  • word_delim (str, optional) – Word delimiter. Defaults to “n”.

Returns

The Chepy object.

Return type

Chepy

huffman_decode(huffman_codes: Dict[str, str]) EncryptionEncodingT

Huffman decode

Parameters

huffman_codes (Dict[str, str]) – Huffman codes as a dict

Returns

The Chepy object.

Return type

Chepy

huffman_encode() EncryptionEncodingT

Huffman encode

Returns

The Chepy object.

Return type

Chepy

jwt_decode() EncryptionEncodingT

Decode a JWT token. Does not verify

Returns

The Chepy object.

Return type

Chepy

jwt_sign(secret: str, algorithms: str = 'HS256') EncryptionEncodingT

Sign a json/dict object in JWT

Parameters
  • secret (str) – Required. Secret to sign with

  • algorithms (str, optional) – Signing algorithm. Defaults to “HS256”.

Returns

The Chepy object.

Return type

Chepy

jwt_token_generate_embedded_jwk(private_key_pem: str, private_key_passphrase: str = None, headers: dict = {}, alg: str = 'RS256') EncryptionEncodingT

Generate a JWT token with an embedded JWK

Parameters
  • private_key_pem (str) – Private key to sign token

  • private_key_passphrase (str, optional) – Private key passphrase. Defaults to None.

  • headers (dict, optional) – Token headers. Defaults to {}.

  • alg (str, optional) – Token algorithm. Defaults to “RS256”.

Returns

The Chepy object.

Return type

Chepy

jwt_token_generate_none_alg(headers: Dict[str, Any] = {}) EncryptionEncodingT

Generate a jwt token with none algorithm

Parameters

headers (Dict[str, Any], optional) – Headers. alg key will be overwritten. Defaults to {}.

Returns

The Chepy object.

Return type

Chepy

jwt_verify(secret: str, algorithm: list = ['HS256']) EncryptionEncodingT

Verify JWT token

Parameters
  • secret (str) – Required. Secret key for token

  • algorithm (list, optional) – Array of valid algorithms. Defaults to [“HS256”]

Returns

The Chepy object.

Return type

Chepy

ls47_decrypt(password: str, padding: int = 10) EncryptionEncodingT

LS47 decrypt

Parameters
  • password (str) – password

  • padding (int, optional) – Padding. Defaults to 10.

Returns

The Chepy object.

Return type

Chepy

ls47_encrypt(password: str, padding: int = 10, signature: str = '') EncryptionEncodingT

LS47 encrypt

Parameters
  • password (str) – password

  • padding (int, optional) – Padding. Defaults to 10.

  • signature (str, optional) – Signature to prepend. Defaults to ‘’.

Returns

The Chepy object.

Return type

Chepy

monoalphabetic_substitution(mapping: Dict[str, str] = {}) EncryptionEncodingT

Monoalphabetic substitution. Re-map characters

Parameters

mapping (Dict[str, str], optional) – Mapping of characters where key is the character to map and value is the new character to replace with. Defaults to {}.

Returns

The Chepy object

Return type

Chepy

rabbit(key: str, iv: Union[None, str] = None) EncryptionEncodingT

Rabbit encryption/decryption

Parameters
  • key (str) – Key

  • iv (Union[None,str], optional) –

    1. Defaults to None.

Returns

The Chepy object.

Return type

Chepy

rc4_decrypt(key: str, key_format: str = 'hex') EncryptionEncodingT

Decrypt raw state with RC4

Parameters
  • key (str) – Required. Secret key

  • key_format (str, optional) – Key format. Defaults to “hex”.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("9e59bf79a2c0b7d253").hex_to_str().rc4_decrypt("secret").o
b"some data"
rc4_encrypt(key: str, key_format: str = 'hex') EncryptionEncodingT

Encrypt raw state with RC4

Parameters
  • key (str) – Required. Secret key

  • key_format (str, optional) – Key format. Defaults to “hex”.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("some data").rc4_encrypt("736563726574").o
b"9e59bf79a2c0b7d253"
rot_13() EncryptionEncodingT

ROT-13 encoding

A simple caesar substitution cipher which rotates alphabet characters by the specified amount (default 13).

Returns

The Chepy object.

Return type

Chepy

rot_47(rotation: int = 47) EncryptionEncodingT

ROT 47 encoding

A slightly more complex variation of a caesar cipher, which includes ASCII characters from 33 ‘!’ to 126 ‘~’. Default rotation: 47.

Parameters

rotation (int, optional) – Amount to rotate by. Defaults to 14.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("some").rot_47().out
b"D@>6"
rot_47_bruteforce() EncryptionEncodingT

ROT 47 bruteforce

Returns

The Chepy object.

Return type

Chepy

rot_8000()

Rot8000

Returns

The Chepy object.

Return type

Chepy

rotate(rotate_by: int) EncryptionEncodingT

Rotate string by provided number

Parameters

rotate_by (int) – Required. Number to rotate by

Returns

The Chepy object.

Return type

Chepy

Examples

In this example, we will rotate by 20

>>> Chepy("some data").rotate(20).out
"migy xunu"
rotate_bruteforce() EncryptionEncodingT

Brute force rotation from 1 to 26. Returned value is a dict where key is the rotation count.

Returns

The Chepy object.

Return type

Chepy

Examples

In this example, we will rotate by 20

>>> Chepy('uryyb').rotate_bruteforce()
{
    '1': 'vszzc',
    '2': 'wtaad',
    ...
    '13': 'hello',
    ...
}
rsa_decrypt(private_key: str, is_file: bool = True, passphrase: str = None, cipher: Literal['OAEP', 'PKCS'] = 'OAEP') EncryptionEncodingT

Decrypt data with RSA Private key in PEM format

Parameters
  • private_key (str) – Path to Private key

  • is_file (bool) – If supplied argument is a PEM file path. Defaults to false

  • passphrase (str) – passphrase. Defaults to None

  • cipher (str) – Cipher type. Defaults to OAEP

Returns

The Chepy object

Return type

Chepy

rsa_encrypt(public_key: str, is_file: bool = True, passphrase: str = None, cipher: Literal['OAEP', 'PKCS'] = 'OAEP') EncryptionEncodingT

Encrypt data with RSA Public key in PEM format

Parameters
  • public_key (str) – Path to Public key

  • is_file (bool) – If supplied argument is a PEM file path. Defaults to false

  • passphrase (str) – passphrase. Defaults to None

  • cipher (str) – Cipher type. Defaults to OAEP

Returns

The Chepy object

Return type

Chepy

rsa_private_pem_to_jwk() EncryptionEncodingT

Convert RSA PEM private key to jwk format

Returns

The Chepy object.

Return type

Chepy

rsa_public_key_from_jwk() EncryptionEncodingT

Generate RSA public key in PEM format from JWK

Raises

AssertionError – If n or e not found

Returns

The Chepy object.

Return type

Chepy

rsa_sign(private_key: str, is_file: bool = True, passphrase: str = None, hash_format: Literal['SHA256', 'SHA512', 'SHA1', 'MD5', 'SHA384'] = 'SHA256') EncryptionEncodingT

Sign data in state with RSA Private key in PEM format

Parameters
  • private_key (str) – Private key

  • is_file (bool) – If supplied argument is a PEM file path. Defaults to false

  • passphrase (str) – passphrase. Defaults to None

  • hash_format (str) – hash type. Defaults to SHA256

Returns

The Chepy object

Return type

Chepy

rsa_verify(signature: bytes, public_key: str, is_file: bool = True, passphrase: str = None, hash_format: Literal['SHA256', 'SHA512', 'SHA1', 'MD5', 'SHA384'] = 'SHA256') EncryptionEncodingT

Verify data in state with RSA Public key in PEM format

Parameters
  • signature (bytes) – The signature as bytes

  • public_key (str) – Path to Private key

  • is_file (bool) – If supplied argument is a PEM file path. Defaults to false

  • passphrase (str) – passphrase. Defaults to None

  • hash_format (str) – Cipher type. Defaults to SHA256

Returns

The Chepy object

Return type

Chepy

to_letter_number_code(join_by: Union[str, bytes] = b' ') EncryptionEncodingT

Encode to A1Z26

Parameters

join_by (Union[str, bytes], optional) – join output by. Defaults to b’ ‘.

Returns

The Chepy object.

Return type

Chepy

to_morse_code(dot: str = '.', dash: str = '-', letter_delim: str = ' ', word_delim: str = '\n') EncryptionEncodingT

Encode string to morse code

Parameters
  • dot (str, optional) – The char for dot. Defaults to “.”.

  • dash (str, optional) – The char for dash. Defaults to “-“.

  • letter_delim (str, optional) – Letter delimiter. Defaults to ” “.

  • word_delim (str, optional) – Word delimiter. Defaults to “n”.

Returns

The Chepy object.

Return type

Chepy

triple_des_decrypt(key: str, iv: str = '0000000000000000', mode: str = 'CBC', key_format: str = 'hex', iv_format: str = 'hex') EncryptionEncodingT

Decrypt raw state encrypted with DES.

Parameters
  • key (str) – Required. The secret key

  • iv (str, optional) – IV for certain modes only. Defaults to ‘0000000000000000’.

  • mode (str, optional) – Encryption mode. Defaults to ‘CBC’.

  • key_format (str, optional) – Format of key. Defaults to ‘hex’.

  • iv_format (str, optional) – Format of IV. Defaults to ‘hex’.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> c = Chepy("f8b27a0d8c837edce87dd13a1ab41f96")
>>> c.hex_to_str()
>>> c.triple_des_decrypt("super secret password !!")
>>> c.o
b"some data"
triple_des_encrypt(key: str, iv: str = '0000000000000000', mode: str = 'CBC', key_format: str = 'hex', iv_format: str = 'hex') EncryptionEncodingT

Encrypt raw state with Triple DES

Parameters
  • key (str) – Required. The secret key

  • iv (str, optional) – IV for certain modes only. Defaults to ‘0000000000000000’.

  • mode (str, optional) – Encryption mode. Defaults to ‘CBC’.

  • key_format (str, optional) – Format of key. Defaults to ‘hex’.

  • iv_format (str, optional) – Format of IV. Defaults to ‘hex’.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("some data").triple_des_encrypt("super secret password !!", mode="ECB").o
b"f8b27a0d8c837edc8fb00ea85f502fb4"
vigenere_decode(key: str) EncryptionEncodingT

Vigenere decode

Parameters

key (str) – Key

Raises
  • ValueError – Key is not alpha

  • ValueError – Key is not provided

Returns

The Chepy object.

Return type

Chepy

vigenere_encode(key: str) EncryptionEncodingT

Vigenere encode

Parameters

key (str) – Key

Raises
  • ValueError – Key is not alpha

  • ValueError – Key is not provided

Returns

The Chepy object.

Return type

Chepy

xor(key: str, key_type: Literal['hex', 'utf', 'base64', 'decimal'] = 'hex') EncryptionEncodingT

XOR state with a key

Parameters
  • key (str) – Required. The key to xor by

  • key_type (str, optional) – The key type. Valid values are hex, utf, decimal and base64. Defaults to “hex”.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> Chepy("secret").xor(key="secret", key_type="utf").to_hex()
000000000000
xor_bruteforce(length: int = 100, crib: Optional[Union[bytes, str]] = None) EncryptionEncodingT

Brute force single byte xor

For multibyte xor bruteforce, use chepy.extras.crypto_extras.xor_bruteforce_multi function

Parameters
  • length (int, optional) – How to bytes to bruteforce. Defaults to 100.

  • crib (Union[bytes, str, None], optional) – Check for crib in xored value. Defaults to None.

Returns

The Chepy object.

Return type

Chepy

Examples

>>> c = Chepy("pf`qfw").xor_bruteforce()
{'00': bytearray(b'pf`qfw'),
'01': bytearray(b'qgapgv'),
'02': bytearray(b'rdbsdu'),
'03': bytearray(b'secret'), # here is our secret xored with the hex key 03
'04': bytearray(b'tbdubs'),
'05': bytearray(b'ucetcr'),
...}
>>> c.get_by_key("03").bytearray_to_str()
secret
>>> c.xor("03").bytearray_to_str()
pf`qfw