Skip to main content

A friendly cryptographic primitives library.

Project description

xycrypto

Ciphers

The xycrypto provides simple and elegant interfaces for ciphers.

The cryptography components for ciphers we support:

  • Available stream cipher: ARC4, ChaCha20.
  • Available block cipher: AES, TripleDES.
  • Available mode: ECB, CBC, OFB, CFB, CTR.
  • Available padding: PKCS7, ANSIX923, ISO10126.

Usage

Firstly, you should import the cipher class from the package xycrypto.ciphers. The cipher class follows the following naming conventions:

  • For stream cipher, <cipher_name>.
  • For block cipher, <cipher_name>_<mode>.
>>> from xycrypto.ciphers import AES_CBC

Secondly, you should create the instance of the cipher class. In this case, you should provide some arguments:

  • key for all ciphers.
  • iv for block cipher in ECB, CBC, OFB, CFB mode.
  • nonce for block cipher in CTR mode.
  • padding for block cipher in ECB, CBC mode.
# The len(key) is 16 bytes for AES-128, you can use 32 bytes key for AES-256.
>>> key = b'0123456789abcdef'
# Note len(iv) should be equal to AES_CBC.block_size.
>>> iv = b'0123456789abcdef'
# We use PKCS7 padding.
>>> cipher = AES_CBC(key, iv=iv, padding='PKCS7')

Finally, call encrypt or decrypt method to encrypt or decrypt data respectively.

>>> plaintext = b'Welcome to xycrypto!'
>>> ciphertext = cipher.encrypt(plaintext) 
>>> ciphertext
b'3\x0f\xad\xa6\x17\xc4}\xb9\t\x17\xf8\xae\xbb\xa2t\xb9o\xf2\xf6\x16\t\x0803\xaci\x0c\x19q\x9d\xa3O'
>>> cipher.decrypt(ciphertext)
b'Welcome to xycrypto!'

Example

>>> from xycrypto.ciphers import AES_CBC

# The len(key) is 16 bytes for AES-128, you can use 32 bytes key for AES-256.
>>> key = b'0123456789abcdef'
# Note len(iv) should be equal to AES_CBC.block_size.
>>> iv = b'0123456789abcdef'
# We use PKCS7 padding.
>>> cipher = AES_CBC(key, iv=iv, padding='PKCS7')

>>> plaintext = b'Welcome to xycrypto!'
>>> ciphertext = cipher.encrypt(plaintext) 
>>> ciphertext
b'3\x0f\xad\xa6\x17\xc4}\xb9\t\x17\xf8\xae\xbb\xa2t\xb9o\xf2\xf6\x16\t\x0803\xaci\x0c\x19q\x9d\xa3O'
>>> cipher.decrypt(ciphertext)
b'Welcome to xycrypto!'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xycrypto-0.4.tar.gz (8.8 kB view hashes)

Uploaded Source

Built Distribution

xycrypto-0.4-py3-none-any.whl (10.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page