def decrypted_qr(master_pass, aes_key, img): key = makeqr.gen_aes_key(master_pass, aes_key) scanner = zbar.ImageScanner() # obtain image data pil = Image.open(img).convert('L') width, height = pil.size raw = pil.tostring() # wrap image data image = zbar.Image(width, height, 'Y800', raw) # scan the image for barcodes scanner.scan(image) # extract results for symbol in image: # do something useful with results if str(symbol.type) != 'QRCODE': continue b64 = symbol.data #~ print b64 data = base64.b64decode(b64) iv = data[:16] ciphertext = data[16:] return unpad(decrypt(key, iv, ciphertext)) return None
def test_encryption_decryption(self): master_pass = "".join(random.sample(string.letters, 10)) aes_key = "".join(random.sample(string.letters, 16)) plaintext = "".join(random.sample(string.letters, 50)) key = makeqr.gen_aes_key(master_pass, aes_key) iv, ciphertext = makeqr.encrypt(key, plaintext) self.assertNotEqual(plaintext, ciphertext) self.assertEqual(plaintext, readqr.decrypt(key, iv, ciphertext))
def test_keygen(self): master_pass = "".join(random.sample(string.letters, 10)) aes_key = "".join(random.sample(string.letters, 16)) # No IV dependency. key = makeqr.gen_aes_key(master_pass, aes_key) key2 = makeqr.gen_aes_key(master_pass, aes_key) self.assertEqual(key,key2) # Wrong password should make different key. wrong_password_key = makeqr.gen_aes_key("test122", aes_key) self.assertNotEqual(key, wrong_password_key) # Wrong aes pub-key should make different key wrong_aes_key = makeqr.gen_aes_key(master_pass, "testtesttesttess") self.assertNotEqual(key, wrong_aes_key) # Different number of rounds should make different key orig_value = makeqr.NUMBER_OF_AES_ROUNDS while makeqr.NUMBER_OF_AES_ROUNDS == orig_value: makeqr.NUMBER_OF_AES_ROUNDS = random.randint(10, 1337) wrong_rounds_key = makeqr.gen_aes_key(master_pass, aes_key) self.assertNotEqual(key, wrong_rounds_key) # Reset and sanity check... makeqr.NUMBER_OF_AES_ROUNDS = orig_value key2 = makeqr.gen_aes_key(master_pass, aes_key) self.assertEqual(key,key2)