Skip to main content

A Python package that provides robust encryption and decryption mechanisms, utilizing JSON Web Encryption (JWE), Hybrid Encryption, AWS KMS, and AWS Secrets Manager. Ensure the confidentiality and integrity of your data, with secure management of encryption keys.

Project description

Cryptorix

Cryptorix is a Python package that provides robust encryption and decryption mechanisms using JSON Web Encryption (JWE), Hybrid Encryption, AWS KMS, and AWS Secrets Manager. It leverages both symmetric (AES) and asymmetric (RSA) encryption techniques to ensure the confidentiality and integrity of your data. The package also integrates with AWS KMS and Secrets Manager to manage encryption keys securely.

Table of Contents

Overview

Cryptorix allows you to encrypt and decrypt data using industry-standard encryption algorithms, focusing on JWE for secure token exchange, Hybrid Encryption for data security, and AWS services (KMS and Secrets Manager) for key management. The package ensures seamless integration with AWS services for encryption at rest and in transit.

Modules

JWE (JSON Web Encryption)

This module facilitates the encryption and decryption of data using the JWE standard, combining RSA encryption for key management and AES-GCM encryption for content.

Functions:

  • encrypt(api_response, secret_name, secret_key, kms_id): Encrypts a dictionary into a JWE token using RSA encryption for the AES key and AES-GCM for the content.
  • decrypt(jwe_payload, secret_name, secret_key, kms_id): Decrypts a JWE token into its original dictionary form using the RSA private key.

Hybrid Encryption

This module provides hybrid encryption functionality using AES for encrypting data and RSA for encrypting the AES session key. The encrypted data is Base64-encoded to ensure secure and safe transmission across communication channels.

Functions:

  • encrypt_data(api_response, secret_name, secret_key, kms_id, rsa_padding): Encrypts the provided data using a hybrid encryption scheme. AES (in either GCM or CBC mode) is used for data encryption, while RSA encrypts the AES session key. The data is then Base64-encoded for secure transmission.
  • decrypt_data(encrypted_data, encrypted_key, secret_name, secret_key, kms_id, rsa_padding): Decrypts the provided Base64-encoded encrypted data using RSA to retrieve the AES session key and AES-GCM/CBC for decrypting the actual data.

KMS (Key Management System)

This module provides AWS KMS-based encryption and decryption of data. It integrates with AWS KMS to securely manage encryption keys.

Functions:

  • decrypt(encrypted_value, lambda_function_name, kms_id): Decrypts a KMS-encrypted, base64-encoded string.
  • encrypt(plaintext, kms_id): Encrypts a plaintext string using AWS KMS and returns the encrypted value as a base64 string.

Secrets Manager

This module interacts with AWS Secrets Manager to retrieve and decrypt secrets, ensuring that sensitive information is handled securely.

Functions:

  • get_rsa_key(secret_name, secret_key, kms_id): Retrieves and decrypts the RSA key from AWS Secrets Manager using KMS.
  • get_secrets(secret_name, secret_key): Retrieves a specific key from a secret stored in AWS Secrets Manager.
  • decrypt_kms_ciphertext(ciphertext, kms_id): Decrypts base64-encoded ciphertext using AWS KMS.

Installation

To install the Cryptorix package, use pip:

pip install Cryptorix

You also need to install dependencies such as boto3, pycryptodome, and jwcrypto. You can install them with:

pip install boto3 pycryptodome jwcrypto

Usage

Here is a basic example of how to use the package:

Encrypting Data (JWE):

This function encrypts a dictionary payload using RSA to encrypt the AES key and AES-GCM for content encryption.

from Cryptorix.jwe import encrypt

# Input data
api_response = {"user": "John Doe", "transaction_id": "123456", "status": "completed"}
secret_name = "your_secret_name"
secret_key = "your_secret_key"
kms_id = "your_kms_key_id"

try:
    # Call to encrypt to create the JWE token
    jwe_token = encrypt(api_response, secret_name, secret_key, kms_id)
    print("Generated JWE Token:", jwe_token)
except Exception as e:
    print(f"Error during encryption: {e}")

Decrypting Data (JWE):

This function decrypts the JWE payload back into its original dictionary form using RSA decryption.

from Cryptorix.jwe import decrypt

# JWE token to decrypt
jwe_token = "your-encrypted-jwe-token"

secret_name = "your-secret-name"  # AWS Secrets Manager secret name
secret_key = "private-key"  # Key name in the secret (private key)
kms_id = "your-kms-key-id"  # AWS KMS key ID

# Decrypt data using JWE
decrypted_payload = decrypt(jwe_token, secret_name, secret_key, kms_id)
print("Decrypted Payload:", decrypted_payload)

Encrypting Data (Hybrid Encryption):

You can use the encrypt_data function to encrypt your sensitive data.

from Cryptorix.hybrid_encryption import encrypt

# Input data to encrypt
api_response = {"username": "admin", "password": "secure_password"}
secret_name = "your_secret_name"
secret_key = "your_secret_key"
kms_id = "your_kms_key_id"
rsa_padding = "your_padding_type"

try:
    # Encrypt the data
    result = encrypt(api_response, secret_name, secret_key, kms_id, rsa_padding)
    print("Encrypted Data:", result["encryptedData"])
    print("Encrypted Key:", result["encryptedKey"])
except Exception as e:
    print(f"Error during encryption: {e}")

Decrypting Data (Hybrid Encryption):

You can use the decrypt_data function to decrypt the previously encrypted data.

from Cryptorix.hybrid_encryption import decrypt

# Input data to decrypt
encrypted_data = "your_base64_encoded_encrypted_data"
encrypted_key = "your_base64_encoded_encrypted_key"
secret_name = "your_secret_name"
secret_key = "your_secret_key"
kms_id = "your_kms_key_id"
rsa_padding = "your_padding_type"

try:
    # Decrypt the data
    decrypted_response = decrypt(encrypted_data, encrypted_key, secret_name, secret_key, kms_id, rsa_padding)
    print("Decrypted Response:", decrypted_response)
except Exception as e:
    print(f"Error during decryption: {e}")

Encrypting Data (KMS):

This function encrypts a plaintext string using AWS KMS and returns the encrypted value encoded as a Base64 string.

from Cryptorix.kms import encrypt

# Input data
plaintext = "your-sensitive-data"
kms_id = "your_kms_key_id"

try:
    # Call to encrypt the plaintext
    encrypted_value = encrypt(plaintext, kms_id)
    print("Encrypted value (base64 encoded):", encrypted_value)
except Exception as e:
    print(f"Error during encryption: {e}")

Decrypting Data (KMS):

This function decrypts a KMS-encrypted base64-encoded string back to its original plaintext form.

from Cryptorix.kms import decrypt

# Input data
encrypted_value = "your_base64_encoded_encrypted_value_here"
lambda_function_name = "your_lambda_function_name"
kms_id = "your_kms_key_id"

try:
    # Call to decrypt the KMS-encrypted value
    decrypted_value = decrypt(encrypted_value, lambda_function_name, kms_id)
    print("Decrypted value:", decrypted_value)
except Exception as e:
    print(f"Error during decryption: {e}")

Retrieve RSA Key:

This function retrieves and decrypts the RSA key from AWS Secrets Manager using AWS KMS.

from Cryptorix.secrets import get_rsa_key

# Input data
secret_name = "your_secret_name"
secret_key = "your_secret_key"
kms_id = "your_kms_key_id"

try:
    # Call to get_rsa_key to retrieve and decrypt the RSA key
    rsa_key = get_rsa_key(secret_name, secret_key, kms_id)
    print("Decrypted RSA key:", rsa_key)
except Exception as e:
    print(f"Error while fetching RSA key: {e}")

Retrieve Secrets:

This function retrieves a specific key from AWS Secrets Manager without decryption.

from Cryptorix.secrets import get_secrets

# Input data
secret_name = "your_secret_name"
secret_key = "your_secret_key"

try:
    # Call to get_secrets to fetch only the specific key value
    secret_data = get_secrets(secret_name, secret_key)
    print("Secret data retrieved:", secret_data)
except Exception as e:
    print(f"Error while retrieving secrets: {e}")

Retrieve Secrets:

This function decrypts a given base64-encoded ciphertext using AWS KMS directly.

from Cryptorix.secrets import decrypt_kms_ciphertext

# Input data
kms_id = "your_kms_key_id"
ciphertext = "your_base64_encoded_ciphertext"

try:
    # Call to decrypt_kms_ciphertext to decrypt the base64 KMS ciphertext
    decrypted_data = decrypt_kms_ciphertext(ciphertext, kms_id)
    print("Decrypted KMS ciphertext:", decrypted_data)
except Exception as e:
    print(f"Error while decrypting KMS ciphertext: {e}")

Exceptions

Cryptorix provides custom exceptions for error handling:

  • HybridEncryptionError: Raised during hybrid encryption/decryption failures.
  • JWEError: Raised during JWE encryption/decryption failures.
  • KMSDecryptionError: Raised if decryption via AWS KMS fails.
  • KMSEncryptionError: Raised if encryption via AWS KMS fails.
  • SecretRetrievalError: Raised if secrets cannot be retrieved or decrypted from AWS Secrets Manager.

This will capture all error-level logs related to encryption and decryption operations.

AWS Permissions

Ensure the following permissions are assigned to your AWS IAM role or user:

  • KMS Permissions:
    • kms:Encrypt
    • kms:Decrypt
  • Secrets Manager Permissions:
    • secretsmanager:GetSecretValue

Dependencies

The package requires the following dependencies:

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Submit issues or pull requests to enhance the package. For major changes, please open a discussion first.

Authors

M Santhosh Kumar Initial work santhoshse7en@gmail.com

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

cryptorix-1.0.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Cryptorix-1.0.2-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file cryptorix-1.0.2.tar.gz.

File metadata

  • Download URL: cryptorix-1.0.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for cryptorix-1.0.2.tar.gz
Algorithm Hash digest
SHA256 d53a8c01188d36cb72d6e54a91ec59ad0ce567fbed2fbe66328624ebe5fbc4da
MD5 c67e7d66b6a9cf5ae0c950ec94e3e183
BLAKE2b-256 94c6ef73855171e17c3183936c285058616a106c9192e195c33e57e90f35b80d

See more details on using hashes here.

File details

Details for the file Cryptorix-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: Cryptorix-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for Cryptorix-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ce850d02080616bafa42fdc86260456c60c250fe0ec66341c3303ced6ddfbfa6
MD5 9d7b4e1b79283ae3814ee522f92f2ad8
BLAKE2b-256 3e1d5fbe6fcb1d76e43b37cee470430326c3ddefa9bde974c5f9bb4a0c829a68

See more details on using hashes here.

Supported by

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