def generateAesKeys(): params = AesKeyParams() memberDecryptKey = AesAlgorithm.generateKey(params) decryptionKeyBlob = memberDecryptKey.getKeyBits() memberEncryptKey = AesAlgorithm.deriveEncryptKey(decryptionKeyBlob) encryptionKeyBlob = memberEncryptKey.getKeyBits() return (encryptionKeyBlob, decryptionKeyBlob)
def generateAesKeys(): params = AesKeyParams() memberDecryptKey = AesAlgorithm.generateKey(params) decryptionKeyBlob = memberDecryptKey.getKeyBits() memberEncryptKey = AesAlgorithm.deriveEncryptKey(decryptionKeyBlob) encryptionKeyBlob = memberEncryptKey.getKeyBits() return (encryptionKeyBlob, decryptionKeyBlob)
def test_key_generation(self): keyParams = AesKeyParams(128) decryptKey = AesAlgorithm.generateKey(keyParams) encryptKey = AesAlgorithm.deriveEncryptKey(decryptKey.getKeyBits()) plainBlob = Blob(PLAINTEXT, False) # Encrypt/decrypt data in AES_CBC with auto-generated IV. encryptParams = EncryptParams(EncryptAlgorithmType.AesCbc, 16) cipherBlob = AesAlgorithm.encrypt(encryptKey.getKeyBits(), plainBlob, encryptParams) receivedBlob = AesAlgorithm.decrypt(decryptKey.getKeyBits(), cipherBlob, encryptParams) self.assertTrue(receivedBlob.equals(plainBlob))
def test_key_generation(self): keyParams = AesKeyParams(128) decryptKey = AesAlgorithm.generateKey(keyParams) encryptKey = AesAlgorithm.deriveEncryptKey(decryptKey.getKeyBits()) plainBlob = Blob(PLAINTEXT, False) # Encrypt/decrypt data in AES_CBC with auto-generated IV. encryptParams = EncryptParams(EncryptAlgorithmType.AesCbc, 16) cipherBlob = AesAlgorithm.encrypt( encryptKey.getKeyBits(), plainBlob, encryptParams) receivedBlob = AesAlgorithm.decrypt( decryptKey.getKeyBits(), cipherBlob, encryptParams) self.assertTrue(receivedBlob.equals(plainBlob))
def test_encryption_decryption(self): encryptParams = EncryptParams(EncryptAlgorithmType.AesEcb, 16) key = Blob(KEY, False) decryptKey = DecryptKey(key) encryptKey = AesAlgorithm.deriveEncryptKey(decryptKey.getKeyBits()) # Check key loading and key derivation. self.assertTrue(encryptKey.getKeyBits().equals(key)) self.assertTrue(decryptKey.getKeyBits().equals(key)) plainBlob = Blob(PLAINTEXT, False) # Encrypt data in AES_ECB. cipherBlob = AesAlgorithm.encrypt(encryptKey.getKeyBits(), plainBlob, encryptParams) self.assertTrue(cipherBlob.equals(Blob(CIPHERTEXT_ECB, False))) # Decrypt data in AES_ECB. receivedBlob = AesAlgorithm.decrypt(decryptKey.getKeyBits(), cipherBlob, encryptParams) self.assertTrue(receivedBlob.equals(plainBlob)) # Encrypt/decrypt data in AES_CBC with auto-generated IV. encryptParams.setAlgorithmType(EncryptAlgorithmType.AesCbc) cipherBlob = AesAlgorithm.encrypt(encryptKey.getKeyBits(), plainBlob, encryptParams) receivedBlob = AesAlgorithm.decrypt(decryptKey.getKeyBits(), cipherBlob, encryptParams) self.assertTrue(receivedBlob.equals(plainBlob)) # Encrypt data in AES_CBC with specified IV. initialVector = Blob(INITIAL_VECTOR, False) encryptParams.setInitialVector(initialVector) cipherBlob = AesAlgorithm.encrypt(encryptKey.getKeyBits(), plainBlob, encryptParams) self.assertTrue(cipherBlob.equals(Blob(CIPHERTEXT_CBC_IV, False))) # Decrypt data in AES_CBC with specified IV. receivedBlob = AesAlgorithm.decrypt(decryptKey.getKeyBits(), cipherBlob, encryptParams) self.assertTrue(receivedBlob.equals(plainBlob))
def test_encryption_decryption(self): encryptParams = EncryptParams(EncryptAlgorithmType.AesEcb, 16) key = Blob(KEY, False) decryptKey = DecryptKey(key) encryptKey = AesAlgorithm.deriveEncryptKey(decryptKey.getKeyBits()) # Check key loading and key derivation. self.assertTrue(encryptKey.getKeyBits().equals(key)) self.assertTrue(decryptKey.getKeyBits().equals(key)) plainBlob = Blob(PLAINTEXT, False) # Encrypt data in AES_ECB. cipherBlob = AesAlgorithm.encrypt( encryptKey.getKeyBits(), plainBlob, encryptParams) self.assertTrue(cipherBlob.equals(Blob(CIPHERTEXT_ECB, False))) # Decrypt data in AES_ECB. receivedBlob = AesAlgorithm.decrypt( decryptKey.getKeyBits(), cipherBlob, encryptParams) self.assertTrue(receivedBlob.equals(plainBlob)) # Encrypt/decrypt data in AES_CBC with auto-generated IV. encryptParams.setAlgorithmType(EncryptAlgorithmType.AesCbc) cipherBlob = AesAlgorithm.encrypt( encryptKey.getKeyBits(), plainBlob, encryptParams) receivedBlob = AesAlgorithm.decrypt( decryptKey.getKeyBits(), cipherBlob, encryptParams) self.assertTrue(receivedBlob.equals(plainBlob)) # Encrypt data in AES_CBC with specified IV. initialVector = Blob(INITIAL_VECTOR, False) encryptParams.setInitialVector(initialVector) cipherBlob = AesAlgorithm.encrypt( encryptKey.getKeyBits(), plainBlob, encryptParams) self.assertTrue(cipherBlob.equals(Blob(CIPHERTEXT_CBC_IV, False))) # Decrypt data in AES_CBC with specified IV. receivedBlob = AesAlgorithm.decrypt( decryptKey.getKeyBits(), cipherBlob, encryptParams) self.assertTrue(receivedBlob.equals(plainBlob))