In Python cryptography, how do I build a CLI?

In Python cryptography, building a Command Line Interface (CLI) involves using libraries such as `argparse` for argument parsing and a cryptography library like `cryptography` to handle encryption and decryption tasks. Below is a simple example of how you can implement a CLI for encrypting and decrypting text using the symmetric algorithm Fernet.

from cryptography.fernet import Fernet import argparse # Generate a key for encryption key = Fernet.generate_key() cipher_suite = Fernet(key) def encrypt(text): """Encrypts the provided text.""" return cipher_suite.encrypt(text.encode()) def decrypt(encrypted_text): """Decrypts the provided encrypted text.""" return cipher_suite.decrypt(encrypted_text).decode() def main(): parser = argparse.ArgumentParser(description='Encrypt or Decrypt text') parser.add_argument('action', choices=['encrypt', 'decrypt'], help='Choose to encrypt or decrypt text.') parser.add_argument('text', help='The text to encrypt or decrypt.') args = parser.parse_args() if args.action == 'encrypt': encrypted = encrypt(args.text) print(f'Encrypted: {encrypted.decode()}') elif args.action == 'decrypt': decrypted = decrypt(args.text.encode()) print(f'Decrypted: {decrypted}') if __name__ == '__main__': main()

Python Cryptography CLI Encryption Decryption Command Line Interface