Created
May 31, 2018 06:54
-
-
Save sigilioso/6b113695571f4c9f520c64b4de6e5a99 to your computer and use it in GitHub Desktop.
Simple XOR encrypt/decrypt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import argparse | |
from binascii import unhexlify, hexlify | |
from itertools import cycle | |
def encrypt(data, key): | |
return hexlify(bytes(x ^ y for x, y in zip(bytes(data, "utf-8"), cycle(unhexlify(key))))) | |
def decrypt(encrypted, key): | |
return bytes(x ^ y for x, y in zip(unhexlify(encrypted), cycle(unhexlify(key)))) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("operation", help="encrypt|decrypt", type=str) | |
parser.add_argument("data", help="Data to encrypt/decrypt", type=str) | |
parser.add_argument("key", help="Hexadecimal key", type=str) | |
args = parser.parse_args() | |
if args.operation == "encrypt": | |
print(encrypt(args.data, args.key).decode("utf-8"), end='') | |
elif args.operation == "decrypt": | |
print(decrypt(args.data, args.key).decode("utf-8"), end='') | |
else: | |
print("Invalid operation {}, use encrypt or decrypt".format(args.operation)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment