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()
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?