encryptlib package#

Submodules#

encryptlib.core module#

ENcrypt: A Encryption Library

This module implements a custom encryption algorithm based on matrix operations and periodic decimal expansions. It’s designed for educational purposes as part of the TUBITAK 2204-a project.

The encryption process uses a key derived from numbers ending in 9 to generate a 2x2 matrix for encryption/decryption operations.

Example

>>> encryptor = ENcrypt(59)
>>> encrypted = encryptor.encrypt("Hello World")
>>> decrypted = encryptor.decrypt(encrypted)
>>> assert "hello world" == decrypted.lower()
class encryptlib.core.ENcrypt(value, alphabet=None)[source]#

Bases: object

A class implementing matrix-based encryption and decryption operations.

alphabet#

The character set used for encryption/decryption.

Type:

str

key#

The encryption key derived from the input value.

Type:

int

value#

The original input value.

Type:

int

approx#

High-precision approximation of 1/value.

Type:

mpf

decrypt(mat)[source]#

Decrypt an encrypted matrix back to text.

Parameters:

mat (Union[ndarray[tuple[int, ...], dtype[int64]], ndarray[tuple[int, ...], dtype[float64]]]) – The encrypted matrix to decrypt.

Return type:

str

Returns:

The decrypted message.

Raises:

EncryptionError – If decryption fails.

encrypt(message)[source]#

Encrypt a message using matrix multiplication.

Parameters:

message (str) – The message to encrypt.

Return type:

ndarray[tuple[int, ...], dtype[int64]]

Returns:

An encrypted matrix.

Raises:

MessageFormatError – If message contains invalid characters.

property key_mat: ndarray[tuple[int, ...], dtype[int64]]#

Generate the encryption key matrix from the decimal expansion.

Returns:

A 2x2 numpy array representing the key matrix.

Raises:

EncryptionError – If unable to extract required digits.

property period: int#

Calculate the basic period in the decimal expansion.

property true_period: int#

Calculate the true period used in encryption.

exception encryptlib.core.EncryptionError[source]#

Bases: Exception

Base exception class for encryption-related errors.

exception encryptlib.core.InvalidKeyError[source]#

Bases: EncryptionError

Raised when the encryption key is invalid.

exception encryptlib.core.MessageFormatError[source]#

Bases: EncryptionError

Raised when the message format is invalid.

encryptlib.core.main()[source]#

Demo function showcasing basic usage.

encryptlib.exceptions module#

Exception classes for the encryptlib package.

exception encryptlib.exceptions.EncryptionError[source]#

Bases: Exception

Base exception class for encryption-related errors.

exception encryptlib.exceptions.InvalidKeyError[source]#

Bases: EncryptionError

Raised when the encryption key is invalid.

exception encryptlib.exceptions.MessageFormatError[source]#

Bases: EncryptionError

Raised when the message format is invalid.

encryptlib.utils module#

Utility functions for the encryptlib package.

encryptlib.utils.matrix_to_numbers(matrix, alphabet)[source]#

Convert a matrix of characters to a matrix of numbers based on the alphabet.

Parameters:
Return type:

ndarray[tuple[int, ...], dtype[int64]]

Returns:

A matrix of corresponding numeric values.

Raises:

ValueError – If a character is not found in the alphabet.

encryptlib.utils.validate_alphabet(alphabet)[source]#

Validate the provided alphabet.

Parameters:

alphabet (str) – The alphabet to validate.

Raises:

ValueError – If the alphabet contains duplicate characters.

Return type:

None

Module contents#

ENcrypt: A Matrix-based Encryption Library

A secure and efficient encryption library using matrix operations and periodic decimal expansions, developed as part of the TUBITAK 2204-a project.

class encryptlib.ENcrypt(value, alphabet=None)[source]#

Bases: object

A class implementing matrix-based encryption and decryption operations.

alphabet#

The character set used for encryption/decryption.

Type:

str

key#

The encryption key derived from the input value.

Type:

int

value#

The original input value.

Type:

int

approx#

High-precision approximation of 1/value.

Type:

mpf

decrypt(mat)[source]#

Decrypt an encrypted matrix back to text.

Parameters:

mat (Union[ndarray[tuple[int, ...], dtype[int64]], ndarray[tuple[int, ...], dtype[float64]]]) – The encrypted matrix to decrypt.

Return type:

str

Returns:

The decrypted message.

Raises:

EncryptionError – If decryption fails.

encrypt(message)[source]#

Encrypt a message using matrix multiplication.

Parameters:

message (str) – The message to encrypt.

Return type:

ndarray[tuple[int, ...], dtype[int64]]

Returns:

An encrypted matrix.

Raises:

MessageFormatError – If message contains invalid characters.

property key_mat: ndarray[tuple[int, ...], dtype[int64]]#

Generate the encryption key matrix from the decimal expansion.

Returns:

A 2x2 numpy array representing the key matrix.

Raises:

EncryptionError – If unable to extract required digits.

property period: int#

Calculate the basic period in the decimal expansion.

property true_period: int#

Calculate the true period used in encryption.

exception encryptlib.EncryptionError[source]#

Bases: Exception

Base exception class for encryption-related errors.

exception encryptlib.InvalidKeyError[source]#

Bases: EncryptionError

Raised when the encryption key is invalid.

exception encryptlib.MessageFormatError[source]#

Bases: EncryptionError

Raised when the message format is invalid.