Skip to main content

Lightweight encryption

Project description

NALEnc - Python Encryption Library

NALEnc is a lightweight Python encryption library designed for securely encrypting and decrypting text and binary data. With an intuitive interface and robust functionality, it is ideal for developers seeking a straightforward yet effective encryption solution.

Features

  • Encrypt and decrypt strings or binary data.
  • Supports passwords as strings, bytes, or lists of integers (0-255).
  • Optimized for messages of size 2046n, where n ∈ N.

Installation

To install the library, use pip:

pip install nalenc

Usage

Importing the Library

import nalenc

Creating an Instance of NALEnc

To use the library, create an instance of the NALEnc class with a password. The password can be:

  • A string
  • A byte sequence
  • An iterable of integers (each integer must be in the range 0-255)

Example:

import nalenc
import random

# Generate a password as a list of integers
password = [random.randint(0, 255) for _ in range(512)]
nal = nalenc.NALEnc(password)

Encrypting Data

Use the encrypt method to encrypt a message. The message can be a:

  • String
  • Byte sequence
  • Iterable of integers (each integer must be in the range 0-255)

Example:

# Encrypt a string
encrypted = nal.encrypt("Hello, World!")

# Encrypt binary data
binary_data = b"\x89PNG\r\n\x1a\n"
encrypted_binary = nal.encrypt(binary_data)

Decrypting Data

Use the decrypt method to decrypt an encrypted message.

Example:

# Decrypt the encrypted string
original = nal.decrypt(encrypted)  # Returns a list of integers

# Decrypt binary data
original_binary = nal.decrypt(encrypted_binary)

Working with Binary Files

NALEnc supports encrypting and decrypting binary files. Simply read the file as binary data, encrypt or decrypt it, and then save the result. Note that the encrypted data needs to be cast to bytes before writing to a file.

Example:

# Encrypt a binary file
with open("input.bin", "rb") as f:
    data = f.read()

encrypted_data = nal.encrypt(data)

with open("output.enc", "wb") as f:
    f.write(bytes(encrypted_data))

# Decrypt the binary file
with open("output.enc", "rb") as f:
    encrypted_data = f.read()

decrypted_data = nal.decrypt(encrypted_data)

with open("decrypted.bin", "wb") as f:
    f.write(bytes(decrypted_data))

Optimal Message Size

For best performance, messages should have sizes of 2046n, where n is a positive integer. This helps to maximize efficiency and ensure optimal encryption.

API Reference

Class: NALEnc

Constructor

NALEnc(password: str | bytes | Iterable[int])
  • password: The encryption password. It can be a string, byte sequence, or iterable of integers (each in the range 0-255).

Methods

encrypt(msg: str | bytes | Iterable[int])

Encrypts the given message.

  • msg: The message to encrypt. Can be a string, byte sequence, or iterable of integers (each in the range 0-255).
  • Returns: The encrypted message as a list of integers.
decrypt(msg: str | bytes | Iterable[int])

Decrypts the given encrypted message.

  • msg: The encrypted message. Can be a string, byte sequence, or iterable of integers (each in the range 0-255).
  • Returns: The original message as a list of integers.

Example Code

import nalenc
import random

# Generate a random password
password = [random.randint(0, 255) for _ in range(512)]

# Create an instance of NALEnc
nal = nalenc.NALEnc(password)

# Encrypt a message
message = "Encrypt this message!"
encrypted = nal.encrypt(message)

# Decrypt the message
decrypted = nal.decrypt(encrypted)

print("Original:", message)
print("Encrypted:", bytes(encrypted))  # Cast to bytes for readability
print("Decrypted:", bytes(decrypted))

License

This library is licensed under the LGPL License. See the LICENSE file for more information.


For questions, feedback, or contributions, feel free to open an issue on the GitHub repository.

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

nalenc-1.0.0.tar.gz (29.9 kB view details)

Uploaded Source

Built Distribution

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

nalenc-1.0.0-py3-none-any.whl (34.4 kB view details)

Uploaded Python 3

File details

Details for the file nalenc-1.0.0.tar.gz.

File metadata

  • Download URL: nalenc-1.0.0.tar.gz
  • Upload date:
  • Size: 29.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for nalenc-1.0.0.tar.gz
Algorithm Hash digest
SHA256 28e98ff28c7b4ed08c25314b228bf022caa351bd7c8405a03a069d05bb5c7a0c
MD5 9076e56038afe85da52fde02a32546d4
BLAKE2b-256 250e8edfb81ad380d96d9564ce5fb56a5c7c78fdcbac0d1cda7c1c4c9e8bba15

See more details on using hashes here.

File details

Details for the file nalenc-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: nalenc-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 34.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for nalenc-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c35e53331b01c561a9454e826bdf7b69c92e489840792998f94d80c242798922
MD5 322722826bb372383f4a9606e87a27a2
BLAKE2b-256 7b4853ed509d5c55982d61e14db96e5efc57ffc7c100982245f5707f5f23b27c

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