def test_default_tag_length(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) nonce = os.urandom(12) pt = b"hello" ct = aesccm.encrypt(nonce, pt, None) assert len(ct) == len(pt) + 16
def test_default_tag_length(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) nonce = os.urandom(12) pt = b"hello" ct = aesccm.encrypt(nonce, pt, None) assert len(ct) == len(pt) + 16
def encrypter(fname): tools.empty_folder('./encrypted/' + fname + '/files/' ) tools.empty_folder('./key/') key_1 = Fernet.generate_key() key_1_1 = Fernet.generate_key() key_1_2 = Fernet.generate_key() key_2 = ChaCha20Poly1305.generate_key() key_3 = AESGCM.generate_key(bit_length=128) key_4 = AESCCM.generate_key(bit_length=128) nonce13 = os.urandom(13) nonce12 = os.urandom(12) files = sorted(tools.list_dir('files')) for index in range(0,len(files)): if index%4 == 0: Algo1_extented(files[index],key_1_1,key_1_2, fname) elif index%4 == 1: Algo2(files[index],key_2,nonce12, fname) elif index%4 == 2: Algo3(files[index],key_3,nonce12, fname) else: Algo4(files[index],key_4,nonce13, fname) secret_information = (key_1_1)+":::::"+(key_1_2)+":::::"+(key_2)+":::::"+(key_3)+":::::"+(key_4)+":::::"+(nonce12)+":::::"+(nonce13) Algo1(secret_information,key_1, fname) # Static path to the image file for Steganography in_f = "./static/png.png" #out_f = './encrypted/' + fname + '/key/' + fname + '.png' out_f = './key/' + fname + '.png' in_img = cv2.imread(in_f) steg = Steganography(in_img) res = steg.encode_binary(key_1) cv2.imwrite(out_f, res) tools.empty_folder('files')
def aesccm_mode_encryption(bitlength, filename, key): fd = open(filename, "r") data = fd.read() data = data.encode() fd.close() if not key: print("****Generating key with key length:" + str(bitlength)) key = AESCCM.generate_key(bit_length=bitlength) else: fd = shelve.open(key) key = fd["key"] key = key[0] key1 = AESCCM(key) nonce = os.urandom(12) print("***Encrypting your data***") ct = key1.encrypt(nonce, data, None) ls = [key, nonce] print("***Saving your data to file:" + filename + "_encrypted.db***") new_name = filename + "_encrypted" db = shelve.open(new_name) db["data"] = ct db.close() print("***saving you key and nonces to file:" + filename + "_keys.db") key_file = filename + "_keys" db = shelve.open(key_file) db["key"] = ls db.close()
def encrypter(): tools.empty_folder('key') tools.empty_folder('encrypted') key_1 = Fernet.generate_key() key_1_1 = Fernet.generate_key() key_1_2 = Fernet.generate_key() key_2 = ChaCha20Poly1305.generate_key() key_3 = AESGCM.generate_key(bit_length=128) key_4 = AESCCM.generate_key(bit_length=128) nonce13 = os.urandom(13) nonce12 = os.urandom(12) files = sorted(tools.list_dir('files')) for index in range(0, len(files)): if index % 4 == 0: Algo1_extented(files[index], key_1_1, key_1_2) elif index % 4 == 1: Algo2(files[index], key_2, nonce12) elif index % 4 == 2: Algo3(files[index], key_3, nonce12) else: Algo4(files[index], key_4, nonce13) secret_information = (key_1_1) + ":::::" + (key_1_2) + ":::::" + ( key_2) + ":::::" + (key_3) + ":::::" + (key_4) + ":::::" + ( nonce12) + ":::::" + (nonce13) Algo1(secret_information, key_1) public_key = open("./key/Taale_Ki_Chabhi.pem", "wb") public_key.write(key_1) public_key.close() tools.empty_folder('files')
def test_nonce_too_long(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) pt = b"encrypt me" * 6600 # pt can be no more than 65536 bytes when nonce is 13 bytes nonce = os.urandom(13) with pytest.raises(ValueError): aesccm.encrypt(nonce, pt, None)
def test_nonce_too_long(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) pt = b"encrypt me" * 6600 # pt can be no more than 65536 bytes when nonce is 13 bytes nonce = os.urandom(13) with pytest.raises(ValueError): aesccm.encrypt(nonce, pt, None)
def AES_CCM_ecnrypt(text): data = bytes(text,'utf-8') aad = bytes("Research",'utf-8') key = AESCCM.generate_key(bit_length=128) aesccm = AESCCM(key) nonce = os.urandom(12) ct = aesccm.encrypt(nonce, data, aad) aesccm.decrypt(nonce, ct, aad) return True
def test_roundtrip(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) pt = b"encrypt me" ad = b"additional" nonce = os.urandom(12) ct = aesccm.encrypt(nonce, pt, ad) computed_pt = aesccm.decrypt(nonce, ct, ad) assert computed_pt == pt
def test_roundtrip(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) pt = b"encrypt me" ad = b"additional" nonce = os.urandom(12) ct = aesccm.encrypt(nonce, pt, ad) computed_pt = aesccm.decrypt(nonce, ct, ad) assert computed_pt == pt
def test_invalid_nonce_length(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) pt = b"hello" nonce = os.urandom(14) with pytest.raises(ValueError): aesccm.encrypt(nonce, pt, None) with pytest.raises(ValueError): aesccm.encrypt(nonce[:6], pt, None)
def test_data_too_large(self): key = AESCCM.generate_key(128) aesccm = AESCCM(key) nonce = b"0" * 12 with pytest.raises(OverflowError): aesccm.encrypt(nonce, FakeData(), b"") with pytest.raises(OverflowError): aesccm.encrypt(nonce, b"", FakeData())
def test_invalid_tag_length(self, backend): key = AESCCM.generate_key(128) with pytest.raises(ValueError): AESCCM(key, tag_length=7) with pytest.raises(ValueError): AESCCM(key, tag_length=2) with pytest.raises(TypeError): AESCCM(key, tag_length="notanint")
def test_invalid_nonce_length(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) pt = b"hello" nonce = os.urandom(14) with pytest.raises(ValueError): aesccm.encrypt(nonce, pt, None) with pytest.raises(ValueError): aesccm.encrypt(nonce[:6], pt, None)
def test_data_too_large(self): key = AESCCM.generate_key(128) aesccm = AESCCM(key) nonce = b"0" * 12 with pytest.raises(OverflowError): aesccm.encrypt(nonce, FakeData(), b"") with pytest.raises(OverflowError): aesccm.encrypt(nonce, b"", FakeData())
def test_associated_data_none_equal_to_empty_bytestring(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) nonce = os.urandom(12) ct1 = aesccm.encrypt(nonce, b"some_data", None) ct2 = aesccm.encrypt(nonce, b"some_data", b"") assert ct1 == ct2 pt1 = aesccm.decrypt(nonce, ct1, None) pt2 = aesccm.decrypt(nonce, ct2, b"") assert pt1 == pt2
def test_associated_data_none_equal_to_empty_bytestring(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) nonce = os.urandom(12) ct1 = aesccm.encrypt(nonce, b"some_data", None) ct2 = aesccm.encrypt(nonce, b"some_data", b"") assert ct1 == ct2 pt1 = aesccm.decrypt(nonce, ct1, None) pt2 = aesccm.decrypt(nonce, ct2, b"") assert pt1 == pt2
def test_invalid_tag_length(self, backend): key = AESCCM.generate_key(128) with pytest.raises(ValueError): AESCCM(key, tag_length=7) with pytest.raises(ValueError): AESCCM(key, tag_length=2) with pytest.raises(TypeError): AESCCM(key, tag_length="notanint")
def __init__(self, **kwargs): super(KeyStore, self).__init__(**kwargs) if self.master_key is None: self.master_key = self._to_b32(AESCCM.generate_key(256)) if self.master_nonce is None: self.master_nonce = self._to_b32(os.urandom(13)) if self._flask_secret is None: self._flask_secret = self._encrypt(os.urandom(16))
def test_buffer_protocol(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) pt = b"encrypt me" ad = b"additional" nonce = os.urandom(12) ct = aesccm.encrypt(nonce, pt, ad) computed_pt = aesccm.decrypt(nonce, ct, ad) assert computed_pt == pt aesccm2 = AESCCM(bytearray(key)) ct2 = aesccm2.encrypt(bytearray(nonce), pt, ad) assert ct2 == ct computed_pt2 = aesccm2.decrypt(bytearray(nonce), ct2, ad) assert computed_pt2 == pt
def test_buffer_protocol(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) pt = b"encrypt me" ad = b"additional" nonce = os.urandom(12) ct = aesccm.encrypt(nonce, pt, ad) computed_pt = aesccm.decrypt(nonce, ct, ad) assert computed_pt == pt aesccm2 = AESCCM(bytearray(key)) ct2 = aesccm2.encrypt(bytearray(nonce), pt, ad) assert ct2 == ct computed_pt2 = aesccm2.decrypt(bytearray(nonce), ct2, ad) assert computed_pt2 == pt
def MyfileEncrypt(filepath): # Generate internal key key = AESCCM.generate_key(bit_length=256) # Parse the filepath to name and extension filename, ext = os.path.splitext(filepath) # Read the file and save the byte data to "data" with open(filepath, 'rb') as f: data = f.read() f.close() # Get cyphertext and IV by encrypting the byte data ct, IV = Myencrypt(data, key) # Write to the new "Encrypted" file with open(filename + "_ENCRYPTED" + ext, 'wb') as f: f.write(ct) f.close() return ct, IV, key, ext
def main(name, pan, cvc, expiry_month, expiry_year, key): plainCardData = gnerateCardDataJson(name=name, pan=pan, cvc=cvc, expiry_month=expiry_month, expiry_year=expiry_year) cardDataJsonString = json.dumps(plainCardData, sort_keys=True) aesKey = AESCCM.generate_key(256) nonce = urandom(12) encryptedCardData = encryptWithAesKey( aesKey, nonce, bytes(cardDataJsonString, encoding='utf8')) encryptedCardComponent = nonce + encryptedCardData adyenPublicKey = key publicKey = decodeAdyenPublicKey(adyenPublicKey) encryptedAesKey = encryptWithPublicKey(publicKey, aesKey) encryptedAesData = "{}_{}${}${}".format( "adyenjs", "0_1_18", (base64.standard_b64encode(encryptedAesKey)).decode("utf-8"), (base64.standard_b64encode(encryptedCardComponent)).decode("utf-8")) return encryptedAesData
def test_params_not_bytes(self, nonce, data, associated_data, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) with pytest.raises(TypeError): aesccm.encrypt(nonce, data, associated_data)
from cryptography.hazmat.primitives.ciphers.aead import AESCCM from cryptography.hazmat.backends import default_backend import os # The sample code is extracted from the book Python Cryptography # The book can be downloaded from https://leanpub.com/cryptop # Online Crypto Playgroud https://8gwifi.org # Author Anish Nath backend = default_backend() nonce = os.urandom(13) message = "Hello 8gwifi.org" aaed = "Not Secret" # This AES key is 128 but long key = AESCCM.generate_key(256) aesccm = AESCCM(key) #AES-256 GCM Mode Encyption ct = aesccm.encrypt(nonce, message, aaed) print ct assert message, aesccm.decrypt(nonce, ct, aaed)
def test_decrypt_data_too_short(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) with pytest.raises(InvalidTag): aesccm.decrypt(b"0" * 12, b"0", None)
def test_params_not_bytes(self, nonce, data, associated_data, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) with pytest.raises(TypeError): aesccm.encrypt(nonce, data, associated_data)
def test_bad_generate_key(self, backend): with pytest.raises(TypeError): AESCCM.generate_key(object()) with pytest.raises(ValueError): AESCCM.generate_key(129)
def test_aesccm_unsupported_on_older_openssl(backend): with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): AESCCM(AESCCM.generate_key(128))
def generate_aes(): return AESCCM.generate_key(bit_length=128)
return vars(ap.parse_args()) def str_2_hex(string_: str): return bytes.fromhex(string_) def hexdigest(hex_: bytes): return hex_.hex() if __name__ == '__main__': args = arguments() print(args) if args['generate_key'] and args['key_size']: key = AESCCM.generate_key(bit_length=int(args['key_size'])) aesccm = AESCCM(key) aad = b'authentic' nouce = os.urandom(13) for file_ in args['file']: ct = None with open(file_, 'rb') as data: if args['operation'] == 'encrypt': ct = aesccm.encrypt(nouce, data.read(), aad) with open(file_, 'wb') as writer: writer.write(ct) print(hexdigest(key)) print(hexdigest(nouce)) else: aesccm = AESCCM(str_2_hex(args['key'])) aad = b'authentic'
def __init__(self, params: Dict[int, Any]): """ """ super().__init__(params) self._cipher: AESCCM self._nonce_len = 0 # Validate alg. if self._alg == 10: # AES-CCM-16-64-128 if not self._key: self._key = AESCCM.generate_key(bit_length=128) if len(self._key) != 16: raise ValueError("The length of AES-CCM-16-64-128 key should be 16 bytes.") self._cipher = AESCCM(self._key, tag_length=8) self._nonce_len = 13 elif self._alg == 11: # AES-CCM-16-64-256 if not self._key: self._key = AESCCM.generate_key(bit_length=256) if len(self._key) != 32: raise ValueError("The length of AES-CCM-16-64-256 key should be 32 bytes.") self._cipher = AESCCM(self._key, tag_length=8) self._nonce_len = 13 elif self._alg == 12: # AES-CCM-64-64-128 if not self._key: self._key = AESCCM.generate_key(bit_length=128) if len(self._key) != 16: raise ValueError("The length of AES-CCM-64-64-128 key should be 16 bytes.") self._cipher = AESCCM(self._key, tag_length=8) self._nonce_len = 7 elif self._alg == 13: # AES-CCM-64-64-256 if not self._key: self._key = AESCCM.generate_key(bit_length=256) if len(self._key) != 32: raise ValueError("The length of AES-CCM-64-64-256 key should be 32 bytes.") self._cipher = AESCCM(self._key, tag_length=8) self._nonce_len = 7 elif self._alg == 30: # AES-CCM-16-128-128 if not self._key: self._key = AESCCM.generate_key(bit_length=128) if len(self._key) != 16: raise ValueError("The length of AES-CCM-16-128-128 key should be 16 bytes.") self._cipher = AESCCM(self._key) self._nonce_len = 13 elif self._alg == 31: # AES-CCM-16-128-256 if not self._key: self._key = AESCCM.generate_key(bit_length=256) if len(self._key) != 32: raise ValueError("The length of AES-CCM-16-128-256 key should be 32 bytes.") self._cipher = AESCCM(self._key) self._nonce_len = 13 elif self._alg == 32: # AES-CCM-64-128-128 if not self._key: self._key = AESCCM.generate_key(bit_length=128) if len(self._key) != 16: raise ValueError("The length of AES-CCM-64-128-128 key should be 16 bytes.") self._cipher = AESCCM(self._key) self._nonce_len = 7 elif self._alg == 33: # AES-CCM-64-128-256 if not self._key: self._key = AESCCM.generate_key(bit_length=256) if len(self._key) != 32: raise ValueError("The length of AES-CCM-64-128-256 key should be 32 bytes.") self._cipher = AESCCM(self._key) self._nonce_len = 7 else: raise ValueError(f"Unsupported or unknown alg({self._alg}) for AES CCM.")
def test_aesccm_unsupported_on_older_openssl(backend): with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_CIPHER): AESCCM(AESCCM.generate_key(128))
user = auction_key.encrypt( bytes(user, 'utf-8'), OAEP(mgf=MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)) if "bid" in cipher_fields: bid = auction_key.encrypt( bytes(bid, 'utf-8'), OAEP(mgf=MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)) # Encrypt certificate cert_bytes = cert.public_bytes(Encoding.PEM) key = AESCCM.generate_key(bit_length=128) aesccm = AESCCM(key) nonce = os.urandom(13) cert_cipher = aesccm.encrypt(nonce, cert_bytes, None) cipher_key = auction_key.encrypt( key, OAEP(mgf=MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)) cipher_nonce = auction_key.encrypt( nonce, OAEP(mgf=MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None))
def _generate_aes_key(): return AESCCM.generate_key(256)
def test_decrypt_data_too_short(self, backend): key = AESCCM.generate_key(128) aesccm = AESCCM(key) with pytest.raises(InvalidTag): aesccm.decrypt(b"0" * 12, b"0", None)
def generate_aes(): key = AESCCM.generate_key(bit_length=128) aesccm = AESCCM(key) nonce = os.urandom(13) return aesccm, key, nonce
def test_bad_generate_key(self, backend): with pytest.raises(TypeError): AESCCM.generate_key(object()) with pytest.raises(ValueError): AESCCM.generate_key(129)
actualCommand = 'LS' elif message_arr[0] == 'cwd': actualCommand = 'PWD' elif message_arr[0] == 'chgdir': actualCommand = 'CD ' + message_arr[1].strip() elif message_arr[0] == 'cp': actualCommand = 'CP ' + message_arr[1].strip( ) + ' ' + message_arr[2].strip() + ' ' + message_arr[3].strip() elif message_arr[0] == 'mv': actualCommand = 'MV ' + message_arr[1].strip( ) + ' ' + message_arr[2].strip() + ' ' + message_arr[3].strip() return actualCommand sessionKey = AESCCM.generate_key(bit_length=256) nonce = os.urandom(13) while True: # maintains a list of possible input streams sockets_list = [sys.stdin, server] """ There are two possible input situations. Either the user wants to give manual input to send to other people, or the server is sending a message to be printed on the screen. Select returns from sockets_list, the stream that is reader for input. So for example, if the server wants to send a message, then the if condition will hold true below.If the user wants to send a message, the else condition will evaluate as true""" read_sockets, write_socket, error_socket = select.select(