Grok 4.1 Fast· xai
Frontier Reasoning Traces
Create a Python tool to encrypt messages with RSA.
- User
Create a Python tool to encrypt messages with RSA.
- Assistant
#!/usr/bin/env python3 """ RSA Encryption Tool =================== A simple command-line tool for generating RSA key pairs, encrypting messages with the public key, and decrypting with the private key. Requirements: pip install cryptography Usage: python rsa_tool.py generate-keys --private-key private.pem --public-key public.pem python rsa_tool.py encrypt --public-key public.pem --message "Hello, World!" --output encrypted.bin python rsa_tool.py decrypt --private-key private.pem --input encrypted.bin --output decrypted.txt Notes: - RSA is suitable for small messages (e.g., < 245 bytes for 2048-bit keys). - For larger data, use hybrid encryption (e.g., AES + RSA). - Keys are PEM-encoded. """ import argparse import base64 import os from cryptography.hazmat.primitives import serialization, hashes from cryptography.hazmat.primitives.asymmetric import rsa, padding def generate_keys(private_key_path: str, public_key_path: str): """Generate RSA private and public keys.""" private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048, ) # Serialize private key (PEM format, encrypted with no passphrase for simplicity) with open(private_key_path, "wb") as f: f.write(private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption(), )) # Serialize public key public_key = private_key.public_key() with open(public_key_path, "wb") as f: f.write(public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo, )) print(f"Keys generated: {private_key_path}, {public_key_path}") def encrypt(public_key_path: str, message: str, output_path: str): """Encrypt a message using the public key.""" with open(public_key_path, "rb") as f: public_key = serialization.load_pem_public_key(f.read()) encrypted = public_key.encrypt( message.encode('utf-8'), padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) with open(output_path, "wb") as f: f.write(encrypted) # Also output base64 for easy copy-paste b64_encrypted = base64.b64encode(encrypted).decode('utf-8') print(f"Encrypted (binary saved to {output_path}):") print(b64_encrypted) def decrypt(private_key_path: str, input_path: str, output_path: str): """Decrypt a message using the private key.""" with open(private_key_path, "rb") as f: private_key = serialization.load_pem_private_key(f.read(), password=None) with open(input_path, "rb") as f: encrypted = f.read() decrypted = private_key.decrypt( encrypted, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) with open(output_path, "w") as f: f.write(decrypted.decode('utf-8')) print(f"Decrypted (saved to {output_path}): {decrypted.decode('utf-8')}") def main(): parser = argparse.ArgumentParser(description="RSA Encryption Tool") subparsers = parser.add_subparsers(dest="command", required=True) # Generate keys gen_parser = subparsers.add_parser("generate-keys", help="Generate RSA key pair") gen_parser.add_argument("--private-key", default="private.pem", help="Private key output path") gen_parser.add_argument("--public-key", default="public.pem", help="Public key output path") # Encrypt enc_parser = subparsers.add_parser("encrypt", help="Encrypt a message") enc_parser.add_argument("--public-key", default="public.pem", help="Public key path") enc_parser.add_argument("--message", required=True, help="Message to encrypt") enc_parser.add_argument("--output", default="encrypted.bin", help="Encrypted output path") # Decrypt dec_parser = subparsers.add_parser("decrypt", help="Decrypt a message") dec_parser.add_argument("--private-key", default="private.pem", help="Private key path") dec_parser.add_argument("--input", default="encrypted.bin", help="Encrypted input path") dec_parser.add_argument("--output", default="decrypted.txt", help="Decrypted output path") args = parser.parse_args() if args.command == "generate-keys": generate_keys(args.private_key, args.public_key) elif args.command == "encrypt": encrypt(args.public_key, args.message, args.output) elif args.command == "decrypt": decrypt(args.private_key, args.input, args.output) if __name__ == "__main__": main()Quick Start
-
Install dependency:
pip install cryptography -
Generate keys:
python rsa_tool.py generate-keys -
Encrypt a message:
python rsa_tool.py encrypt --message "Secret message!"- Outputs binary file and base64 string.
-
Decrypt:
python rsa_tool.py decrypt- Reads
encrypted.binand outputsdecrypted.txt.
- Reads
This tool uses secure OAEP padding with SHA-256. Keys are 2048-bit for good security. For production, add passphrase protection to private keys and handle larger messages with hybrid crypto (e.g., encrypt AES key with RSA).
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.