A microservice that generates random keys for one-time pad encryption. This service receives file names via ZeroMQ, generates appropriate-length random keys using ASCII printable characters (32-126), and saves them to files.
- Python 3.7 or higher
- PyZMQ library
Install the required dependencies:
pip install pyzmq
- First, check your Python version:
python --version
- (Optional but recommended) Create a virtual environment:
python3 -m venv venv
source venv/bin/activate
- Install the required package:
pip install -r requirements.txt
If you encounter permission errors:
sudo pip install pyzmq
# Run Python and try:
import zmq
print(zmq.pyzmq_version())
- Clone this repository or download the source files
- Ensure all files are in your working directory
- Start the key generator service:
python key_generator_service.py
The microservice accepts requests via ZeroMQ on port 5555. Send a string containing the input filename for which you need a key generated.
Example request code:
import zmq
# Setup ZeroMQ context and socket
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
# Send the filename
input_filename = "message.txt"
socket.send_string(input_filename)
The service responds with two parts:
- Status: "success" or "error"
- Response: Either the key filename or error message
Example receiving code:
# Receive the response
status, response = socket.recv_multipart()
status = status.decode('utf-8')
response = response.decode('utf-8')
if status == "success":
key_filename = response
print(f"Key saved to: {key_filename}")
else:
error_message = response
print(f"Error: {error_message}")
sequenceDiagram
participant C as Client Program
participant K as Key Generator Service
participant FS as File System
C->>+K: Send filename (ZMQ REQ)
K->>+FS: Check if file exists
FS-->>-K: File status
alt File exists
K->>+FS: Read file length
FS-->>-K: File length
K->>K: Generate random key
K->>+FS: Save key to file
FS-->>-K: Save confirmation
K-->>-C: Send success + key filename
else File not found
K-->>C: Send error + error message
end
The service handles several types of errors:
- File not found
- Permission errors when reading/writing files
- Invalid file formats
- Network communication errors
Error responses will include descriptive messages to help diagnose issues.
- Keys are generated using Python's random.randint()
- Characters are in the ASCII printable range (32-126)
- Key length matches input file length exactly
- Keys are saved in the same directory as the program
- Key filenames are generated as: "key_[original_filename].txt"
- All generated key files are stored in the same directory as the program
- Files are automatically named based on the input filename
- Existing files with the same name will be overwritten
- UTF-8 encoding is used for all file operations
- Start the service:
python key_generator_service.py
- In your client code:
import zmq
def request_key(filename):
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
socket.send_string(filename)
status, response = socket.recv_multipart()
status = status.decode('utf-8')
response = response.decode('utf-8')
return status, response
# Example usage
status, response = request_key("message.txt")
if status == "success":
print(f"Key generated and saved to: {response}")
else:
print(f"Error: {response}")
Common issues and solutions:
-
"Address already in use" error:
- Another instance of the service is running
- Wait a few seconds and try again, or use a different port
-
"Connection refused" error:
- Service is not running
- Check if service is started on the correct port
-
"File not found" error:
- Verify the input file exists
- Check file path is correct
- Ensure proper file permissions
Run the included test program to verify the service is working:
python test_key_generator.py
The test program will:
- Create a test input file
- Request a key
- Display the results