This project demonstrates a simple symmetric XOR encryption and decryption scheme that leverages PyTorch for potential GPU acceleration. It allows users to encrypt and decrypt text messages using a shared secret key, with the computationally intensive key stream generation benefiting from GPU capabilities (Apple Silicon's MPS or NVIDIA's CUDA) or falling back to CPU.
- Symmetric Encryption/Decryption: Uses a single shared key for both encryption and decryption.
- Key Derivation: The encryption key is securely hashed using SHA256 to seed a pseudo-random number generator (PRNG).
- Pseudo-random Key Stream: A unique key stream is generated for each encryption/decryption operation based on the seed and number of iterations.
- GPU Acceleration: Utilizes
torch.backends.mps(for Apple Silicon) ortorch.cuda(for NVIDIA GPUs) for accelerated computations, if available. Falls back to CPU if no compatible GPU is found. - High Iteration Count: The key stream generation involves a large number of iterations (10^6) to enhance randomness and computational complexity.
- Python 3.8 or newer.
pip(Python package installer).- For GPU Acceleration:
- Apple Silicon (MPS): macOS 12.3+ (Monterey) with a supported Apple M-series chip. No additional drivers are usually needed beyond system updates.
- NVIDIA CUDA: Compatible NVIDIA GPU and NVIDIA drivers, along with CUDA Toolkit. Refer to the PyTorch Get Started page for detailed instructions on installing CUDA for PyTorch.
-
Clone the repository (if applicable) or download the files:
git clone https://github.com/CodeAstronomer/Python-GPU-Encryption.git cd Python-GPU-Encryption -
Create a Python Virtual Environment: It's recommended to use a virtual environment to manage project dependencies.
python3 -m venv venv
-
Activate the Virtual Environment:
- On macOS/Linux:
source venv/bin/activate
- On macOS/Linux:
-
Install Dependencies: This project primarily depends on PyTorch. The installation command varies based on your desired backend (CPU, CUDA, or MPS).
- For CPU-only:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
- For Apple Silicon (MPS):
pip install torch torchvision torchaudio # PyTorch will automatically detect and use MPS on supported hardware - For NVIDIA CUDA (example with CUDA 12.1):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 # Adjust 'cu121' to your CUDA version as per PyTorch documentation.
- For CPU-only:
Run the encrypt.py script, provide your secret key, and the text you wish to encrypt. The script will output the encrypted text in hexadecimal format.
python3 encrypt.pyExample:
Set your encryption key: mysecretkey123
Enter text to encrypt: Hello, this is a secret message!
--- Encrypted Output ---
1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b
(Note: The actual output will be a much longer hexadecimal string)
To decrypt, run the decrypt.py script. You must provide the exact same secret key used for encryption and the hexadecimal encrypted text.
python3 decrypt.pyExample:
Enter your encryption key: mysecretkey123
Enter encrypted text (hex): 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b
--- Decrypted Output ---
Hello, this is a secret message!
- Device Detection: The
get_device()function checks for MPS, then CUDA, and finally defaults to CPU. - Key to Seed: Your provided encryption key is hashed using SHA256. The first 8 bytes of this hash are converted into an integer to serve as the seed for PyTorch's random number generator.
- Key Stream Generation: A
torch.Generatoris initialized with the seed. This generator is then used to produce a sequence of pseudo-random bytes (the key stream) with the same length as the input text, iterated10^6times to mix the state. - XOR Operation: The input text (converted to a
torch.tensorof bytes) is XORed with the generated key stream. Due to the properties of XOR, applying the same key stream twice (once for encryption, once for decryption) with the same seed and iterations will revert the text to its original form. - Output: Encrypted text is converted to a hexadecimal string for easy handling. Decrypted text is converted back from hexadecimal and then decoded into a UTF-8 string.
This project is for demonstration purposes only and should NOT be used for securing sensitive or critical data in a production environment. Custom encryption schemes are often prone to vulnerabilities unless thoroughly vetted by cryptography experts. Always use well-established and peer-reviewed cryptographic libraries for real-world security needs.