def get_key(orig_key): key = bittools.hex_to_bits(orig_key) if len(key) == 56: for i in [7, 15, 23, 31, 39, 47, 55, 63]: key.insert(i, 0) if len(key) != 64: raise ValueError("Key " + orig_key + " Is not 64 chars when expanded") return key
op.error("Not enough arguments") elif len(args) > 2: op.error("Too many arguments") keys = get_keys(args[1]) if (options.decrypt): keys.reverse() # text is plaintext if encrypting or ciphertext if decrypting if options.file: text = bytes_from_file(options.file) else: if options.ascii and not options.decrypt: text = bittools.ascii_to_bits(args[0]) else: try: text = bittools.hex_to_bits(args[0]) except ValueError: op.error( "ciphertext couldn't be converted from [%s]. Perhaps you want --ascii or --file mode?" % args[0]) if len(text) != 64: if options.decrypt: op.error("ciphertext must be 16 hex digits") else: op.error( "plaintext must be 16 hex digits (or 8 ascii letters if using -a/--ascii)" ) text = [text] print_logs = options.verbose
if __name__ == "__main__": op = OptionParser( usage="%prog <plaintext> <ciphertext> <num_chunk_bits>", description="Sets up the input for the keysearch by creating 'input.h'. " "plaintext and ciphertext must be 64 bits of hex (without the 0x " "prefix). num_chunk_bits specifies the number of bits a single call to " "check_keys will search. It must be between 6 and 56 inclusive.") (options, args) = op.parse_args() if len(args) < 3: op.error("Not enough arguments") elif len(args) > 3: op.error("Too many arguments") plaintext = bittools.hex_to_bits(args[0]) ciphertext = bittools.hex_to_bits(args[1]) try: num_chunk_bits = int(args[2]) except ValueError: op.error( "num_chunk_bits must be an integer between 6 and 56 inclusive") if len(plaintext) != 64: op.error("plaintext must be 16 hex digits") if len(ciphertext) != 64: op.error("ciphertext must be 16 hex digits") if num_chunk_bits < 6 or num_chunk_bits > 56: op.error( "num_chunk_bits must be an integer between 6 and 56 inclusive")
op.add_option( "-a", "--ascii", dest="ascii", action="store_true", default=False, help= "Convert input plaintext from ascii if encrypting, or convert resulting plaintext to ascii if decrypting." ) (options, args) = op.parse_args() if len(args) < 2: op.error("Not enough arguments") elif len(args) > 2: op.error("Too many arguments") key = bittools.hex_to_bits(args[1]) # text is plaintext if encrypting or ciphertext if decrypting if options.ascii and not options.decrypt: text = bittools.ascii_to_bits(args[0]) else: text = bittools.hex_to_bits(args[0]) if len(text) != 64: if options.decrypt: op.error("ciphertext must be 16 hex digits") else: op.error( "plaintext must be 16 hex digits (or 8 ascii letters if using -a/--ascii)" ) if len(key) != 64: print key, len(key)
import sys import os.path # Add lib/ to sys.path lib_directory = os.path.realpath(os.path.join(__file__, "../../lib/")) sys.path.append(lib_directory) import bittools if __name__ == "__main__": key = bittools.hex_to_bits(sys.argv[1]) #for i in [63, 55, 47, 39, 31, 23, 15, 7]: for i in [7, 15, 23, 31, 39, 47, 55, 63]: key.insert(i, 0) print bittools.bits_to_hex(key)
if __name__ == "__main__": op = OptionParser( usage="%prog <plaintext> <ciphertext> <num_chunk_bits>", description="Sets up the input for the keysearch by creating 'input.h'. " "plaintext and ciphertext must be 64 bits of hex (without the 0x " "prefix). num_chunk_bits specifies the number of bits a single call to " "check_keys will search. It must be between 6 and 56 inclusive.") (options, args) = op.parse_args() if len(args) < 3: op.error("Not enough arguments") elif len(args) > 3: op.error("Too many arguments") plaintext = bittools.hex_to_bits(args[0]) ciphertext = bittools.hex_to_bits(args[1]) try: num_chunk_bits = int(args[2]) except ValueError: op.error("num_chunk_bits must be an integer between 6 and 56 inclusive") if len(plaintext) != 64: op.error("plaintext must be 16 hex digits") if len(ciphertext) != 64: op.error("ciphertext must be 16 hex digits") if num_chunk_bits < 6 or num_chunk_bits > 56: op.error("num_chunk_bits must be an integer between 6 and 56 inclusive") f = open("input.h", 'w')
description = "Encrypt (default) or decrypt using DES. plaintext, ciphertext and key must be 64 bits in hex.") op.add_option("-d", "--decrypt", dest="decrypt", action="store_true", default=False, help="Interpret the first argument as ciphertext and decrypt it.") op.add_option("-c", "--encrypt", dest="decrypt", action="store_false", default=False, help="Interpret the first argument as plaintext and encrypt it. (default)") op.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Print details and intermediate steps of the DSA algorithm.") op.add_option("-a", "--ascii", dest="ascii", action="store_true", default=False, help="Convert input plaintext from ascii if encrypting, or convert resulting plaintext to ascii if decrypting.") (options, args) = op.parse_args() if len(args) < 2: op.error("Not enough arguments") elif len(args) > 2: op.error("Too many arguments") key = bittools.hex_to_bits(args[1]) # text is plaintext if encrypting or ciphertext if decrypting if options.ascii and not options.decrypt: text = bittools.ascii_to_bits(args[0]) else: text = bittools.hex_to_bits(args[0]) if len(text) != 64: if options.decrypt: op.error("ciphertext must be 16 hex digits") else: op.error("plaintext must be 16 hex digits (or 8 ascii letters if using -a/--ascii)") if len(key) != 64: print key, len(key) op.error("key must be 16 hex digits")