Created
October 21, 2022 20:46
-
-
Save xl00t/d24c32a2bfa09d6cc1222f902064fdc5 to your computer and use it in GitHub Desktop.
Xor little tool to play with XOR encryption
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 | |
import base64 | |
import argparse | |
import string | |
class XorBreak: | |
def __init__(self, clear, ciphertext, key, cribs): | |
self.clear = clear | |
self.ciphertext = ciphertext | |
self.key = key | |
self.cribs = cribs | |
self.key_padd = "" | |
def pad_key(self, text): | |
return self.key * (len(text)//len(self.key)+1) | |
def cipher(self): | |
self.ciphertext = "" | |
self.key_padd = self.pad_key(self.clear) | |
for i in range(len(self.clear)): | |
self.ciphertext += chr(ord(self.clear[i]) ^ ord(self.key_padd[i])) | |
return self.ciphertext | |
def uncipher(self): | |
self.key_padd = self.pad_key(self.ciphertext) | |
self.uncipher = "" | |
for i in range(len(self.ciphertext)): | |
self.uncipher += chr(self.ciphertext[i] ^ ord(self.key_padd[i])) | |
return self.uncipher | |
def get_key(self): | |
self.possibles_keys = [] | |
for i in range(len(self.ciphertext)-len(self.cribs)+1): | |
self.possible_key = "" | |
for j in range(len(self.cribs)): | |
self.possible_key += chr(self.ciphertext[i+j] ^ ord(self.cribs[j])) | |
self.possibles_keys.append(self.possible_key) | |
return self.possibles_keys | |
def main(): | |
parser = argparse.ArgumentParser(description='Simple tool for use and break xor encryption ') | |
parser.add_argument('-p', '--plaintext', help='Plaintext input file', type=argparse.FileType('r')) | |
parser.add_argument('-c', '--ciphertext', help='Ciphertext input file', type=argparse.FileType('rb')) | |
parser.add_argument('-k', '--key', help='Input Key : Required for encryption or decryption') | |
parser.add_argument('--cribs', help='Known part of plaintext : should be longer than the key length') | |
args = parser.parse_args() | |
if args.plaintext != None and args.key != None and args.ciphertext == None and args.cribs == None: | |
crypt = XorBreak(args.plaintext.read(), "", args.key, "") | |
print(crypt.cipher()) | |
elif args.plaintext == None and args.key != None and args.ciphertext != None and args.cribs == None: | |
decrypt = XorBreak("", args.ciphertext.read(),args.key,"") | |
print(decrypt.uncipher()) | |
elif args.plaintext == None and args.key == None and args.ciphertext != None and args.cribs != None: | |
crack = XorBreak("", args.ciphertext.read(), "", args.cribs) | |
for keys in crack.get_key(): | |
printable = True | |
for c in keys: | |
if c not in string.ascii_letters + string.digits + string.punctuation + ' ': | |
printable = False | |
if printable: | |
print(keys) | |
else: | |
parser.print_help() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment