def test_vector(): key = base64.urlsafe_b64encode( binascii.unhexlify( b"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" )) data = binascii.unhexlify( b"41206369706865722073797374656d206d757374206e6f742062652072657175" b"6972656420746f206265207365637265742c20616e64206974206d7573742062" b"652061626c6520746f2066616c6c20696e746f207468652068616e6473206f66" b"2074686520656e656d7920776974686f757420696e636f6e76656e69656e6365") iv = binascii.unhexlify(b"1af38c2dc2b96ffdd86694092341bc04") associated_data = binascii.unhexlify( b"546865207365636f6e64207072696e6369706c65206f66204175677573746520" b"4b6572636b686f666673") cryptor = AEAD(key) foo = cryptor._encrypt_from_parts(data, associated_data, iv) assert binascii.hexlify(foo) == ( b"1af38c2dc2b96ffdd86694092341bc04c80edfa32ddf39d5ef00c0b468834279" b"a2e46a1b8049f792f76bfe54b903a9c9a94ac9b47ad2655c5f10f9aef71427e2" b"fc6f9b3f399a221489f16362c703233609d45ac69864e3321cf82935ac4096c8" b"6e133314c54019e8ca7980dfa4b9cf1b384c486f3a54c51078158ee5d79de59f" b"bd34d848b3d69550a67646344427ade54b8851ffb598f7f80074b9473c82e2db" b"652c3fa36b0a7c5b3219fab3a30bc1c4")
def test_vector(): key = base64.urlsafe_b64encode(binascii.unhexlify( b"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" )) data = binascii.unhexlify( b"41206369706865722073797374656d206d757374206e6f742062652072657175" b"6972656420746f206265207365637265742c20616e64206974206d7573742062" b"652061626c6520746f2066616c6c20696e746f207468652068616e6473206f66" b"2074686520656e656d7920776974686f757420696e636f6e76656e69656e6365" ) iv = binascii.unhexlify(b"1af38c2dc2b96ffdd86694092341bc04") associated_data = binascii.unhexlify( b"546865207365636f6e64207072696e6369706c65206f66204175677573746520" b"4b6572636b686f666673" ) cryptor = AEAD(key) foo = cryptor._encrypt_from_parts(data, associated_data, iv) assert binascii.hexlify(foo) == ( b"1af38c2dc2b96ffdd86694092341bc04c80edfa32ddf39d5ef00c0b468834279" b"a2e46a1b8049f792f76bfe54b903a9c9a94ac9b47ad2655c5f10f9aef71427e2" b"fc6f9b3f399a221489f16362c703233609d45ac69864e3321cf82935ac4096c8" b"6e133314c54019e8ca7980dfa4b9cf1b384c486f3a54c51078158ee5d79de59f" b"bd34d848b3d69550a67646344427ade54b8851ffb598f7f80074b9473c82e2db" b"652c3fa36b0a7c5b3219fab3a30bc1c4" )
def image_encryption(key): cryptor = AEAD(key) #convert image to memory buffer imageBytesArray = cv2.imencode('.jpg', image)[1] #convert bytesArray to bytesString imageBytes = base64.b64encode(imageBytesArray) #encrypted data ct = cryptor.encrypt(imageBytes, associated_data.encode()) return ct
class Cryptor: def __init__(self): self.__cryptor = AEAD(CRYPTOR_PASSWORD_SECRET_KEY) def encrypt(self, data, relative_data): return self.__cryptor.encrypt(data.encode(), relative_data.encode()).decode() def decrypt(self, encrypted_data, relative_data): return self.__cryptor.decrypt(encrypted_data.encode(), relative_data.encode()).decode()
def main(): global image global i j = 0 ## still_image is for the camera module. Uncomment only if u have an camera module #still_image() file_list = os.listdir() for i in range(len(file_list)): if ('.jpg' in file_list[i]): filename = file_list[i] #Load the photo into the face recognition algorithm and obtain face_locations image = face_recognition.load_image_file(filename) face_locations = face_recognition.face_locations(image) #Mask the face with random image image = masking(face_locations) SaveBlurring() #Generate key key = AEAD.generate_key() #Encrypt image and face count faceCountEncrypted = number_encryption(key) imageEncrypted = image_encryption(key) writeBinaryFile(faceCountEncrypted, 'faceCount{}'.format(j)) writeBinaryFile(imageEncrypted, 'image{}'.format(j)) # Assume key and assoicated_data already known at the server side writeBinaryFile(key, 'key{}'.format(j)) writeBinaryFile(associated_data.encode(), 'associated_data{}'.format(j)) j = j + 1
def decrypt(args): # read encrypted data encrypted = read(args.input) # split in header and encrypted body header = encrypted[:54] data = encrypted[54:] cryptor = AEAD(key) # decrypt and verify body and verify header ct = cryptor.decrypt(data, header) # write header and decrypted body to file out = header + ct write(out, args.output)
def encrypt(args): # read plain data plain = read(args.input) # split in header and body header = plain[:54] data = plain[54:] # init AEAD cryptor = AEAD(key) # encrypt body and add signature of header ct = cryptor.encrypt(data, header) # concat header and encrypted body + signature out = header + ct # write blob to file write(out, args.output)
def main(): global associated_dataB global key global i print('Available Cameras') #Ensure the data is from the camera the user chooses for key in Camera: print(key) cameraNumber = input( 'Enter the Camera which you like to obtain data from:') #Check for any files in directory while (scpEg.fileCheck(i) == False): pass #key and associated are assume to be in the server already. key = readFile('key{}'.format(i)) associated_dataB = cameraNumber.encode() #read the files faceCountEncrypted = readFile('faceCount{}'.format(i)) imageEncrypted = readFile('image{}'.format(i)) #create AEAD object cryptor = AEAD(key) #if file name is random, then the one way to identifed which file is for faceCount which is image is through length. #FileName is random so that sniffer cannot associated name of file with the data it holds try: if (len(faceCountEncrypted) < len(imageEncrypted)): number = number_decryption(cryptor, faceCountEncrypted, associated_dataB) image = image_decryption(cryptor, imageEncrypted, associated_dataB) else: number = number_decryption(cryptor, imageEncrypted, associated_dataB) image = image_decryption(cryptor, faceCountEncrypted, associated_dataB) print(number) scpEg.fileRemove(number, i) i = i + 1 # Convert RGB to BGR image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) display(image) except ValueError: print('This Data is not from ' + associated_dataB.decode())
def __init__(self): self.__cryptor = AEAD(CRYPTOR_PASSWORD_SECRET_KEY)
# Practice aead encryption from aead import AEAD import sys file = sys.argv[1] print('Name of the file you want to encrypt is ', file) flag = True cryptor = AEAD(AEAD.generate_key()) while(flag): response = input('Enter 1 to encrypt file, 2 to decrypt file, and 3 to exit program: ') if response == "1": with open('spring.txt', 'r') as myfile: data = myfile.read() myfile.close() bytesData = data.encode() encrypted = cryptor.encrypt(bytesData, b"AuthenticatedData") print('encrypted: ', encrypted) with open('spring.txt', 'w') as the_file: the_file.write(encrypted.decode("utf-8")) the_file.close() elif response == "2": with open('spring.txt', 'r') as myfile: data = myfile.read() myfile.close()
def test_invalid_signature(): aead = AEAD(AEAD.generate_key()) ct = aead.encrypt(b"Hello, World", b"Goodbye, World!") with pytest.raises(ValueError): aead.decrypt(ct, b"foobar")
def test_round_trip_encrypt_decrypt(): aead = AEAD(AEAD.generate_key()) ct = aead.encrypt(b"Hello, World!", b"Goodbye, World!") assert aead.decrypt(ct, b"Goodbye, World!") == b"Hello, World!"
import json import time import logging import base64 from aead import AEAD from guardian.docker import DockerManager from guardian.config import SECRET_KEY, DOCKER_SOCK, CONFIG_FILE_DIR, \ CONFIG_FILE_PATH, LOG_FILE_DIR, SERVICE_FILE_DIR LOG = logging.getLogger("Installer") TOKEN_SERIALIZER = AEAD(base64.urlsafe_b64encode(str(SECRET_KEY).encode()[:32])) class InstallError(Exception): pass class Installer: def __init__(self, install_token): self.install_token = install_token self.docker_manager = DockerManager() def install(self): LOG.info("Start install agent") self._render_config_file() LOG.info("Write config to {}".format(CONFIG_FILE_PATH)) service_id = self._create_agent_service() LOG.info("Create agent finish.") self._check_service_status(service_id)
def generate_key(): #generate key key = AEAD.generate_key() return key
def test_key_length(): key = base64.urlsafe_b64encode(b"foobar") with pytest.raises(ValueError): AEAD(key)
def test_round_trip_encrypt_decrypt(plaintext, associated_data): cryptor = AEAD(AEAD.generate_key()) ct = cryptor.encrypt(plaintext, associated_data) assert plaintext == cryptor.decrypt(ct, associated_data)
def number_encryption(key): cryptor = AEAD(key) #convert int to bytes face_countB = face_count.to_bytes(2, 'little') ct = cryptor.encrypt(face_countB, associated_data.encode()) return ct
# -*- coding: utf-8 -*- from aead import AEAD sifreleyici = AEAD(AEAD.generate_key()) # 100 defa hashleme metedu def getHash(key): hashli_metin = "" for x in range(0, 100): hashli_metin = hash(key) return hashli_metin # girdi.txt dosyasinin ve key.txt dosyalarinin okunmasi ve haslenip cikti.txt icine yazilmasi def girdiyi_sifrele(): with open("girdi.txt", "r") as fileReadInput: with open("key.txt", "r") as fileReadKey: with open("cikti.txt", "a") as fileWriteOutput: key = fileReadKey.readline().strip() for line in fileReadInput.readlines(): fileWriteOutput.write( sifreleyici.encrypt(line.strip(), key) + "||" + str(hash(line.strip())) + "||" + str(getHash(key)) + "\n") with open("cikti.txt", "w") as fileReadd: print "" # cikti.txt nin icindeki veriler okunup ayristirildiktan sonra butunluk testinin yapilmasi def ciktiyi_coz():