def main(argv): plaintext = None password = None topic = "topic" ciphertext = None userTriedAction = False # helper : http://www.diveintopython.net/scripts_and_streams/command_line_arguments.html # try: # opts, args = getopt.getopt(argv, "hg:d", ["help", "p=", "c=", "k="]) # except getopt.GetoptError: # usage() # sys.exit(2) # for opt, arg in opts: # if opt in ("-h", "--help"): # usage() # sys.exit() # elif opt in ("-p", "--plaintext"): # plaintext = arg # userTriedAction = True # elif opt in ("-c", "--ciphertext"): # ciphertext = arg # userTriedAction = True # elif opt in ("-k", "--key"): # password = arg # userTriedAction = True # print("args: ", plaintext, key, ciphertext) try: plaintext = argv[0] password = argv[1] except: if len(argv) == 0: usage() print("\nNo argument provided. Entering development mode.") dev(argv) else: print("Invalide arguments.") usage() sys.exit(2) try: topic = argv[3] except: pass if plaintext != None and password != None: db = Database.TweetDatabase() cipher = Cipher() spam,key = cipher.encode(plaintext, password, "topic", db) print("Here is your spam:") print(spam) print("Checking decoding process: here is the message hidden in the spam:") print(cipher.decode(spam,key)) else: usage()
def dev(argv): db = Database.TweetDatabase() cipher = Cipher() print() print() spam,key = cipher.encode("Your majesty the quee", "awesome password", "topic", db) print("Here is your spam:") print(spam) print(cipher.decode(spam,key))
def main(argv): # helper : http://www.diveintopython.net/scripts_and_streams/command_line_arguments.html usage() db = Database.TweetDatabase() cipher = Cipher() print() print() spam = cipher.encode("your plaintext", "awesome password", "topic", db) print("Here is your spam:") print(spam) print(cipher.decode(spam,"awesome password"))
def encrypt(self, password): # Base64 of the encrypted image data. c = Cipher(password) self.b64encrypted = c.encode(base64.b64encode(self.bin_image)) logging.debug('Encrypted b64: ' + self.b64encrypted) logging.info('Length of b64encoded: %d.' % len(self.b64encrypted)) to_hexify = self.b64encrypted # ECC encode to hex_string. if FLAGS.ecc: logging.info('ECCoder encoding input length: %d.' % \ len(self.b64encrypted)) coder = ECCoder(FLAGS.ecc_n, FLAGS.ecc_k, FLAGS.threads) encoded = coder.encode(self.b64encrypted) logging.info('ECCoder encoding output length: %d.' % len(encoded)) to_hexify = encoded # Hexified data for encoding in the uploaded image. hex_data = binascii.hexlify(to_hexify) self.enc_orig_hex_data = hex_data num_data = len(hex_data) logging.debug('Original encrypted hex Data: ' + hex_data) if FLAGS.extra_logs: with open('hex_data.log', 'w') as _: for i in range(0, len(hex_data), 80): _.write(hex_data[i:min(i + 80, len(hex_data))] + '\n') logging.info('Length of hex_data: %d' % num_data) width, length = self.image.size width_power_2 = int(math.ceil(math.log(width, 2))) TARGET_WIDTH = 2**(width_power_2 + 1) logging.info('Width: %d.' % TARGET_WIDTH) width = int(TARGET_WIDTH / (self.block_size * 2.)) height = int(math.ceil(num_data / (1. * width))) logging.info('Encrypted image (w x h): (%d x %d).' % (width, height)) logging.info('Expected image (w x h): (%d x %d).' % \ (TARGET_WIDTH, height*self.block_size)) # Create the base for the encrypted image. rgb_image_width = width * self.block_size * 2 rgb_image_height = height * self.block_size self.rgb_image = Image.new('RGB', (rgb_image_width, rgb_image_height)) colors = [(255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255)] draw = ImageDraw.Draw(self.rgb_image) for i, hex_datum in enumerate(hex_data): #logging.info('hex_datum (%d): %s.' % (i, hex_datum)) hex_val = int(hex_datum, 16) base4_1 = int(hex_val / 4.0) # Implicit floor. base4_0 = int(hex_val - (base4_1 * 4)) y_coord = int(i / (1. * width)) x_coord = int(i - (y_coord * width)) # base4_0 base4_0_x = int(x_coord * self.block_size * 2) base4_0_y = int(y_coord * self.block_size) base4_0_rectangle = \ [(base4_0_x, base4_0_y), (base4_0_x + self.block_size, base4_0_y + self.block_size)] draw.rectangle(base4_0_rectangle, fill=colors[base4_0]) # base4_1 base4_1_x = int((x_coord * self.block_size * 2) + self.block_size) base4_1_y = int(y_coord * self.block_size) base4_1_rectangle = \ [(base4_1_x, base4_1_y), (base4_1_x + self.block_size, base4_1_y + self.block_size)] draw.rectangle(base4_1_rectangle, fill=colors[base4_1]) filename = 'rgb.jpg' logging.info('Saving %s (quality: %d).' % \ (filename, FLAGS.encrypted_image_quality)) self.rgb_image.save(filename, quality=FLAGS.encrypted_image_quality) return filename
def encrypt(self, password): # Base64 of the encrypted image data. c = Cipher(password) self.b64encrypted = c.encode(base64.b64encode(self.bin_image)) logging.debug('Encrypted b64: ' + self.b64encrypted) logging.info('Length of b64encoded: %d.' % len(self.b64encrypted)) to_hexify = self.b64encrypted # ECC encode to hex_string. if FLAGS.ecc: logging.info('ECCoder encoding input length: %d.' % \ len(self.b64encrypted)) coder = ECCoder(FLAGS.ecc_n, FLAGS.ecc_k, FLAGS.threads) encoded = coder.encode(self.b64encrypted) logging.info('ECCoder encoding output length: %d.' % len(encoded)) to_hexify = encoded # Hexified data for encoding in the uploaded image. hex_data = binascii.hexlify(to_hexify) self.enc_orig_hex_data = hex_data num_data = len(hex_data) logging.debug('Original encrypted hex Data: ' + hex_data) if FLAGS.extra_logs: with open('hex_data.log', 'w') as _: for i in range(0, len(hex_data), 80): _.write(hex_data[i:min(i+80, len(hex_data))] + '\n') logging.info('Length of hex_data: %d' % num_data) width, length = self.image.size width_power_2 = int(math.ceil(math.log(width, 2))) TARGET_WIDTH = 2 ** (width_power_2 + 1) logging.info('Width: %d.' % TARGET_WIDTH) width = int(TARGET_WIDTH / (self.block_size * 2.)) height = int(math.ceil(num_data / (1. * width))) logging.info('Encrypted image (w x h): (%d x %d).' % (width, height)) logging.info('Expected image (w x h): (%d x %d).' % \ (TARGET_WIDTH, height*self.block_size)) # Create the base for the encrypted image. rgb_image_width = width * self.block_size * 2 rgb_image_height = height * self.block_size self.rgb_image = Image.new('RGB', (rgb_image_width, rgb_image_height)) colors = [(255,255,255), (255,0,0), (0,255,0), (0,0,255)] draw = ImageDraw.Draw(self.rgb_image) for i, hex_datum in enumerate(hex_data): #logging.info('hex_datum (%d): %s.' % (i, hex_datum)) hex_val = int(hex_datum, 16) base4_1 = int(hex_val / 4.0) # Implicit floor. base4_0 = int(hex_val - (base4_1 * 4)) y_coord = int(i / (1. * width)) x_coord = int(i - (y_coord * width)) # base4_0 base4_0_x = int(x_coord * self.block_size * 2) base4_0_y = int(y_coord * self.block_size) base4_0_rectangle = \ [(base4_0_x, base4_0_y), (base4_0_x + self.block_size, base4_0_y + self.block_size)] draw.rectangle(base4_0_rectangle, fill=colors[base4_0]) # base4_1 base4_1_x = int((x_coord * self.block_size * 2) + self.block_size) base4_1_y = int(y_coord * self.block_size) base4_1_rectangle = \ [(base4_1_x, base4_1_y), (base4_1_x + self.block_size, base4_1_y + self.block_size)] draw.rectangle(base4_1_rectangle, fill=colors[base4_1]) filename = 'rgb.jpg' logging.info('Saving %s (quality: %d).' % \ (filename, FLAGS.encrypted_image_quality)) self.rgb_image.save(filename, quality=FLAGS.encrypted_image_quality) return filename