Skip to main content

A friendly cryptographic primitives library.

Project description

xycrypto

Installation

Require Python 3.6+.

pip install -U xycrypto

Ciphers

The xycrypto provides simple and elegant interfaces for ciphers.

The cryptography components for ciphers we support:

  • Available stream cipher: ChaCha20, RC4.
  • Available block cipher: AES, Blowfish, Camellia, CAST5, DES, IDEA, SEED, TripleDES.
  • Available mode: ECB, CBC, CFB, OFB, 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.

Files for xycrypto, version 0.9
Filename, size File type Python version Upload date Hashes
Filename, size xycrypto-0.9-py3-none-any.whl (12.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size xycrypto-0.9.tar.gz (10.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page