|
| 1 | +import argparse |
| 2 | +import sys |
| 3 | +import os |
| 4 | + |
| 5 | +try: |
| 6 | + from cryptography.fernet import Fernet |
| 7 | +except ImportError: |
| 8 | + print("Please install the 'cryptography' library. Run: pip install -r requirements.txt") |
| 9 | + sys.exit(1) |
| 10 | + |
| 11 | +def generate_key(): |
| 12 | + key = Fernet.generate_key() |
| 13 | + print(f"Generated Key: {key.decode('utf-8')}") |
| 14 | + print("Please save this key in a secure location. You will need it to decrypt your data.") |
| 15 | + |
| 16 | +def encrypt_data(data, key): |
| 17 | + f = Fernet(key) |
| 18 | + return f.encrypt(data) |
| 19 | + |
| 20 | +def decrypt_data(data, key): |
| 21 | + f = Fernet(key) |
| 22 | + return f.decrypt(data) |
| 23 | + |
| 24 | +def main(): |
| 25 | + parser = argparse.ArgumentParser(description="Encrypt and decrypt files or text using symmetric encryption.") |
| 26 | + subparsers = parser.add_subparsers(dest="command", help="Available commands") |
| 27 | + |
| 28 | + # Command: keygen |
| 29 | + parser_keygen = subparsers.add_parser("keygen", help="Generate a new encryption key") |
| 30 | + |
| 31 | + # Command: encrypt |
| 32 | + parser_encrypt = subparsers.add_parser("encrypt", help="Encrypt a file or text") |
| 33 | + parser_encrypt.add_argument("-k", "--key", required=True, help="Encryption key (base64 encoded)") |
| 34 | + group_enc = parser_encrypt.add_mutually_exclusive_group(required=True) |
| 35 | + group_enc.add_argument("-t", "--text", help="Text to encrypt") |
| 36 | + group_enc.add_argument("-f", "--file", help="File to encrypt") |
| 37 | + parser_encrypt.add_argument("-o", "--out", help="Output file to save the encrypted data (optional)") |
| 38 | + |
| 39 | + # Command: decrypt |
| 40 | + parser_decrypt = subparsers.add_parser("decrypt", help="Decrypt a file or text") |
| 41 | + parser_decrypt.add_argument("-k", "--key", required=True, help="Decryption key (base64 encoded)") |
| 42 | + group_dec = parser_decrypt.add_mutually_exclusive_group(required=True) |
| 43 | + group_dec.add_argument("-t", "--text", help="Text to decrypt") |
| 44 | + group_dec.add_argument("-f", "--file", help="File to decrypt") |
| 45 | + parser_decrypt.add_argument("-o", "--out", help="Output file to save the decrypted data (optional)") |
| 46 | + |
| 47 | + args = parser.parse_args() |
| 48 | + |
| 49 | + if args.command == "keygen": |
| 50 | + generate_key() |
| 51 | + |
| 52 | + elif args.command == "encrypt": |
| 53 | + try: |
| 54 | + key = args.key.encode('utf-8') |
| 55 | + if args.text: |
| 56 | + encrypted = encrypt_data(args.text.encode('utf-8'), key) |
| 57 | + if args.out: |
| 58 | + with open(args.out, 'wb') as f: |
| 59 | + f.write(encrypted) |
| 60 | + print(f"Encrypted text saved to: {args.out}") |
| 61 | + else: |
| 62 | + print(f"Encrypted text: {encrypted.decode('utf-8')}") |
| 63 | + |
| 64 | + elif args.file: |
| 65 | + if not os.path.isfile(args.file): |
| 66 | + print(f"Error: File '{args.file}' not found.") |
| 67 | + sys.exit(1) |
| 68 | + with open(args.file, 'rb') as f: |
| 69 | + data = f.read() |
| 70 | + encrypted = encrypt_data(data, key) |
| 71 | + |
| 72 | + out_file = args.out if args.out else args.file + ".enc" |
| 73 | + with open(out_file, 'wb') as f: |
| 74 | + f.write(encrypted) |
| 75 | + print(f"Encrypted file saved to: {out_file}") |
| 76 | + |
| 77 | + except Exception as e: |
| 78 | + print(f"Encryption failed: {e}") |
| 79 | + |
| 80 | + elif args.command == "decrypt": |
| 81 | + try: |
| 82 | + key = args.key.encode('utf-8') |
| 83 | + if args.text: |
| 84 | + decrypted = decrypt_data(args.text.encode('utf-8'), key) |
| 85 | + if args.out: |
| 86 | + with open(args.out, 'wb') as f: |
| 87 | + f.write(decrypted) |
| 88 | + print(f"Decrypted text saved to: {args.out}") |
| 89 | + else: |
| 90 | + try: |
| 91 | + print(f"Decrypted text: {decrypted.decode('utf-8')}") |
| 92 | + except UnicodeDecodeError: |
| 93 | + print(f"Decrypted data is not valid UTF-8. Please save to a file using -o/--out.") |
| 94 | + |
| 95 | + elif args.file: |
| 96 | + if not os.path.isfile(args.file): |
| 97 | + print(f"Error: File '{args.file}' not found.") |
| 98 | + sys.exit(1) |
| 99 | + with open(args.file, 'rb') as f: |
| 100 | + data = f.read() |
| 101 | + decrypted = decrypt_data(data, key) |
| 102 | + |
| 103 | + out_file = args.out |
| 104 | + if not out_file: |
| 105 | + if args.file.endswith(".enc"): |
| 106 | + out_file = args.file[:-4] |
| 107 | + else: |
| 108 | + out_file = args.file + ".dec" |
| 109 | + |
| 110 | + with open(out_file, 'wb') as f: |
| 111 | + f.write(decrypted) |
| 112 | + print(f"Decrypted file saved to: {out_file}") |
| 113 | + |
| 114 | + except Exception as e: |
| 115 | + print(f"Decryption failed: {e}") |
| 116 | + print("Make sure you are using the correct key and valid encrypted data.") |
| 117 | + else: |
| 118 | + parser.print_help() |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + main() |
0 commit comments