Skip to main content

Componentes adicionais de aplicativos clientes Confy

Project description


Confy Addons

Componentes adicionais de aplicativos clientes Confy.

Test Coverage PyPI - Version GitHub License Visitors


➡️ Click here for English version

A Python package that provides symmetric and asymmetric encryption functions for client applications of the Confy encrypted communication system, as well as prefixes that identify messages and encryption keys sent by applications during the handshake process. The package also includes functions to encode and decode the public RSA key to base64 for sending over the network.

Learn more about the project at github.com/confy-security

Made with dedication by students from Brazil 🇧🇷.

⚡ Using

Install the package

Install the package with the package manager used in your project.

For example, with pip:

pip install confy-addons

Or with Poetry:

poetry add confy-addons

Role of each function

aes_decrypt

The aes_decrypt function is responsible for decrypting data that was encrypted using the AES algorithm. It receives as input the encrypted base64-encoded data and the AES key, and returns the original data.

def aes_decrypt(key: bytes, b64_ciphertext: str):
    data = base64.b64decode(b64_ciphertext)
    iv, ciphertext = data[:16], data[16:]
    cipher = Cipher(algorithms.AES(key), modes.CFB(iv))
    decryptor = cipher.decryptor()
    return (decryptor.update(ciphertext) + decryptor.finalize()).decode()

aes_encrypt

The aes_encrypt function is responsible for encrypting data using the AES algorithm. It takes the original data and the AES key as input and returns the encrypted data.

def aes_encrypt(key: bytes, plaintext: str):
    iv = os.urandom(16)
    cipher = Cipher(algorithms.AES(key), modes.CFB(iv))
    encryptor = cipher.encryptor()
    ciphertext = encryptor.update(plaintext.encode()) + encryptor.finalize()
    return base64.b64encode(iv + ciphertext).decode()

deserialize_public_key

The deserialize_public_key function is responsible for decoding an RSA public key that has been encoded in base64. It receives the public key in base64 format as input and returns the public key object.

def deserialize_public_key(b64_key):
    key_bytes = base64.b64decode(b64_key.encode())
    return serialization.load_pem_public_key(key_bytes)

generate_aes_key

The generate_aes_key function generates a random 32-byte (256-bit) AES key for use in symmetric encryption.

def generate_aes_key():
    return os.urandom(32)

generate_rsa_keypair

The generate_rsa_keypair function generates an RSA key pair (public and private) for use in asymmetric encryption.

def generate_rsa_keypair():
    private_key = rsa.generate_private_key(public_exponent=65537, key_size=4096)
    return private_key, private_key.public_key()

rsa_decrypt

The rsa_decrypt function is responsible for decrypting data that was encrypted using the RSA algorithm. It receives the encrypted data and the RSA private key as input, and returns the original data.

def rsa_decrypt(private_key, encrypted_data: bytes):
    return private_key.decrypt(
        encrypted_data,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None,
        ),
    )

rsa_encrypt

The rsa_encrypt function is responsible for encrypting data using the RSA algorithm. It takes the original data and the RSA public key as input and returns the encrypted data.

def rsa_encrypt(public_key, data: bytes):
    return public_key.encrypt(
        data,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None,
        ),
    )

serialize_public_key

The serialize_public_key function is responsible for encoding an RSA public key in base64 format. It receives the public key object as input and returns the base64-encoded key.

def serialize_public_key(public_key):
    return base64.b64encode(
        public_key.public_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PublicFormat.SubjectPublicKeyInfo,
        )
    ).decode()

Usage example

from confy_addons.encryption import (
    aes_decrypt,
    aes_encrypt,
    deserialize_public_key,
    generate_aes_key,
    generate_rsa_keypair,
    rsa_decrypt,
    rsa_encrypt,
    serialize_public_key,
)

# Generating RSA key pair
pk, pub_key = generate_rsa_keypair()

# Encoding public key to base64
pub_b64 = serialize_public_key(pub_key)

# Decoding public key from base64
decoded_pub_key = deserialize_public_key(pub_b64)

# Generating random AES key
aes_key = generate_aes_key()

# Encrypting AES key with RSA keys
encrypted_aes_key = rsa_encrypt(decoded_pub_key, aes_key)

# Decrypting AES key with RSA private key
rsa_decrypt(pk, encrypted_aes_key)

# Encrypting message with AES key
aes_encrypted_msg = aes_encrypt(aes_key, "Secret message")

# Decrypting message with AES key
decrypted_msg = aes_decrypt(aes_key, aes_encrypted_msg)

print(decrypted_msg) # Output: Secret message

📜 License

Confy Addons is open source software licensed under the GPL-3.0 license.

Pacote Python que fornece as funções de criptografia simétrica e assimétrica para os aplicativos clientes do sistema Confy de comunicação criptografada, assim como os prefixos que identificam as mensagens e chaves de criptografia enviadas pelos aplicativos durante o processo de handshake. O pacote também inclui funções de encode e decode da chave RSA pública para base64, para fins de envio pela rede.

Saiba mais sobre o projeto em github.com/confy-security

Feito com dedicação por estudantes do Brasil 🇧🇷.

⚡ Utilizando

Instale o pacote

Instale o pacote com o gerenciador de pacotes usado no seu projeto.

Por exemplo, com pip:

pip install confy-addons

Ou com Poetry:

poetry add confy-addons

Papel de cada função

aes_decrypt

A função aes_decrypt é responsável por descriptografar dados que foram criptografados usando o algoritmo AES. Ela recebe como entrada os dados criptografados codificados em base64 e a chave AES, e retorna os dados originais.

def aes_decrypt(key: bytes, b64_ciphertext: str):
    data = base64.b64decode(b64_ciphertext)
    iv, ciphertext = data[:16], data[16:]
    cipher = Cipher(algorithms.AES(key), modes.CFB(iv))
    decryptor = cipher.decryptor()
    return (decryptor.update(ciphertext) + decryptor.finalize()).decode()

aes_encrypt

A função aes_encrypt é responsável por criptografar dados usando o algoritmo AES. Ela recebe como entrada os dados originais e a chave AES, e retorna os dados criptografados.

def aes_encrypt(key: bytes, plaintext: str):
    iv = os.urandom(16)
    cipher = Cipher(algorithms.AES(key), modes.CFB(iv))
    encryptor = cipher.encryptor()
    ciphertext = encryptor.update(plaintext.encode()) + encryptor.finalize()
    return base64.b64encode(iv + ciphertext).decode()

deserialize_public_key

A função deserialize_public_key é responsável por decodificar uma chave pública RSA que foi codificada em base64. Ela recebe como entrada a chave pública em formato base64 e retorna o objeto da chave pública.

def deserialize_public_key(b64_key):
    key_bytes = base64.b64decode(b64_key.encode())
    return serialization.load_pem_public_key(key_bytes)

generate_aes_key

A função generate_aes_key é responsável por gerar uma chave AES aleatória de 32 bytes (256 bits) para uso na criptografia simétrica.

def generate_aes_key():
    return os.urandom(32)

generate_rsa_keypair

A função generate_rsa_keypair é responsável por gerar um par de chaves RSA (pública e privada) para uso na criptografia assimétrica.

def generate_rsa_keypair():
    private_key = rsa.generate_private_key(public_exponent=65537, key_size=4096)
    return private_key, private_key.public_key()

rsa_decrypt

A função rsa_decrypt é responsável por descriptografar dados que foram criptografados usando o algoritmo RSA. Ela recebe como entrada os dados criptografados e a chave privada RSA, e retorna os dados originais.

def rsa_decrypt(private_key, encrypted_data: bytes):
    return private_key.decrypt(
        encrypted_data,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None,
        ),
    )

rsa_encrypt

A função rsa_encrypt é responsável por criptografar dados usando o algoritmo RSA. Ela recebe como entrada os dados originais e a chave pública RSA, e retorna os dados criptografados.

def rsa_encrypt(public_key, data: bytes):
    return public_key.encrypt(
        data,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None,
        ),
    )

serialize_public_key

A função serialize_public_key é responsável por codificar uma chave pública RSA em formato base64. Ela recebe como entrada o objeto da chave pública e retorna a chave codificada em base64.

def serialize_public_key(public_key):
    return base64.b64encode(
        public_key.public_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PublicFormat.SubjectPublicKeyInfo,
        )
    ).decode()

Exemplo de uso

from confy_addons.encryption import (
    aes_decrypt,
    aes_encrypt,
    deserialize_public_key,
    generate_aes_key,
    generate_rsa_keypair,
    rsa_decrypt,
    rsa_encrypt,
    serialize_public_key,
)

# Gerando par de chaves RSA
pk, pub_key = generate_rsa_keypair()

# Codificando chave pública para base64
pub_b64 = serialize_public_key(pub_key)

# Decodificando chave pública de base64
decoded_pub_key = deserialize_public_key(pub_b64)

# Gerando chave AES aleatória
aes_key = generate_aes_key()

# Criptografando chave AES com chaves RSA
encrypted_aes_key = rsa_encrypt(decoded_pub_key, aes_key)

# Descriptografando chave AES com chave privada RSA
rsa_decrypt(pk, encrypted_aes_key)

# Criptografando mensagem com chave AES
aes_encrypted_msg = aes_encrypt(aes_key, "Mensagem secreta")

# Descriptografando mensagem com chave AES
decrypted_msg = aes_decrypt(aes_key, aes_encrypted_msg)

print(decrypted_msg)  # Output: Mensagem secreta

📜 Licença

Confy Addons é um software de código aberto licenciado sob a Licença GPL-3.0.

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

confy_addons-0.1.4.tar.gz (33.0 kB view details)

Uploaded Source

Built Distribution

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

confy_addons-0.1.4-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

Details for the file confy_addons-0.1.4.tar.gz.

File metadata

  • Download URL: confy_addons-0.1.4.tar.gz
  • Upload date:
  • Size: 33.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for confy_addons-0.1.4.tar.gz
Algorithm Hash digest
SHA256 8cf2d9f7bfa515ea57c41b9f54081b4975521fcea6b510b04f9bb5d7ba52d1ed
MD5 d5ff824bbbda86fa31746ce42069748d
BLAKE2b-256 b0fe8807a099917a3c94c7974b3fa49be9b60999349143df1fb16270a834d5ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for confy_addons-0.1.4.tar.gz:

Publisher: publish.yml on confy-security/confy-addons

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file confy_addons-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: confy_addons-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 31.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for confy_addons-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5ae45ab91110162ee0ebfd21a70ff35485a33dec4f9d6eced2b218ed83608e95
MD5 129f5f0a215006457b6a2edce4d7d18a
BLAKE2b-256 4ae1508c4edcc35599e595b5e305429be3004e0164179aa4106be05a4bdd0cf6

See more details on using hashes here.

Provenance

The following attestation bundles were made for confy_addons-0.1.4-py3-none-any.whl:

Publisher: publish.yml on confy-security/confy-addons

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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