t_crypt.py


Overview

t_crypt.py is a utility script designed to perform RSA-based encryption of input text strings. It reads a public RSA key from a PEM file, encodes the input string in base64, encrypts the base64-encoded string using the RSA public key with PKCS#1 v1.5 padding, and returns the encrypted data as a base64-encoded string. The script also supports command-line usage, enabling users to encrypt a password or any text string passed as an argument and print both the encrypted and decrypted values.

This file is part of a larger system that handles cryptographic operations, most likely within the InfiniFlow project, and relies on helper modules such as api.utils for decryption and file management utilities.


Detailed Description of Components

Imports


Function: crypt(line)

def crypt(line):

Purpose:

Encrypts a given string line using RSA public key encryption with PKCS#1 v1.5 padding.

Parameters:

Returns:

Description:

  1. Key Loading:

    • Constructs the file path to the RSA public key located at <project_base_directory>/conf/public.pem.

    • Reads and imports the RSA public key using a password "Welcome". (Note: Using a hardcoded password is generally discouraged for security reasons.)

  2. Base64 Encoding:

    • Encodes the input plaintext string in UTF-8 and then base64-encodes it. This double encoding step ensures the input is in a suitable format for encryption.

  3. Encryption:

    • Uses the RSA public key to create a cipher object with PKCS#1 v1.5 padding.

    • Encrypts the base64-encoded password.

  4. Output Encoding:

    • The encrypted binary data is then base64-encoded again to produce a string safe for transport or storage.

Usage Example:

encrypted = crypt("my_secret_password")
print(encrypted)  # Prints the base64-encoded encrypted password

Command-Line Interface

if __name__ == "__main__":
    passwd = crypt(sys.argv[1])
    print(passwd)
    print(decrypt(passwd))

Important Implementation Details


Interaction with Other System Components


Visual Diagram

Below is a class/function flowchart illustrating the main function crypt and its interaction with the external utilities for key retrieval and decryption.

flowchart TD
    A[Start: Input string] --> B[crypt(line)]
    B --> C{Get project base directory}
    C --> D[Locate public.pem in conf/]
    D --> E[Import RSA public key with password]
    E --> F[Base64 encode input string]
    F --> G[Encrypt base64 string with RSA PKCS1_v1_5]
    G --> H[Base64 encode encrypted data]
    H --> I[Return encrypted string]

    subgraph External Utilities
        C
        J[decrypt(encrypted_string)]
    end

    I --> J
    J --> K[Output decrypted string]

    classDef extUtil fill:#f9f,stroke:#333,stroke-width:1px,color:#000
    class C,J extUtil

Summary


Recommendations


This documentation should provide sufficient context for developers and maintainers working with t_crypt.py within the InfiniFlow system.