def test_AES(self): key = a2b_hex( "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4") IV = a2b_hex("000102030405060708090A0B0C0D0E0F") plaintext = a2b_hex("6bc1bee22e409f96e93d7e117393172a") ciphertext = a2b_hex("f58c4c04d6e5f1ba779eabfb5f7bfbd6") assert (AES.create(key, IV).encrypt(plaintext) == ciphertext) assert (AES.create(key, IV).decrypt(ciphertext) == plaintext)
def _encrypt(self, password): encKey, authKey = self._deriveKeys(password, self.salt, self.iter_count) ciphertext = AES.create(encKey, bytearray(16)).encrypt(self.private_key.getRawKey()) macData = ciphertext + self.public_key.getRawKey() mac = Digest.HMAC_SHA256(authKey, macData) self.ciphertext = ciphertext self.mac = mac
def _encrypt(self, password): encKey, authKey = self._deriveKeys(password, self.salt, self.iter_count) ciphertext = AES.create(encKey, bytearray(16)).encrypt( self.private_key.getRawKey()) macData = ciphertext + self.public_key.getRawKey() mac = Digest.HMAC_SHA256(authKey, macData) self.ciphertext = ciphertext self.mac = mac
def _decrypt(self, password): encKey, authKey = self._deriveKeys(password, self.salt, self.iter_count) macData = self.ciphertext + self.public_key.getRawKey() calcMac = Digest.HMAC_SHA256(authKey, macData) if not Util.constTimeCompare(calcMac, self.mac): raise InvalidPasswordException("Bad password") return AES.create(encKey, bytearray(16)).decrypt(self.ciphertext)