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 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 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)
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() bytesData = data.encode() print(bytesData) decrypted = cryptor.decrypt(bytesData, b"AuthenticatedData")
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!"
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