def decrypt_secret_key(self, passphrase): if hasattr(passphrase, 'encode'): passphrase = passphrase.encode('utf-8') packet = copy.copy(self._message or self._key) # Do not mutate original cipher, key_bytes, key_block_bytes = self.get_cipher(packet.symmetric_algorithm) cipher = cipher(packet.s2k.make_key(passphrase, key_bytes)) cipher = cipher(packet.encrypted_data[:key_block_bytes]) material = self._block_pad_unpad(key_block_bytes, packet.encrypted_data[key_block_bytes:], lambda x: cipher.decrypt(x)) if packet.s2k_useage == 254: chk = material[-20:] material = material[:-20] if(chk != hashlib.sha1(material)): return None else: chk = unpack('!H', material[-2:])[0] material = material[:-2] if chk != OpenPGP.checksum(material): return None packet.s2k_usage = 0 packet.symmetric_alorithm = 0 packet.encrypted_data = None packet.input = OpenPGP.PushbackGenerator(OpenPGP._gen_one(material)) packet.length = len(material) packet.key_from_input() packet.input = None return packet
def decrypt_secret_key(self, passphrase): if hasattr(passphrase, 'encode'): passphrase = passphrase.encode('utf-8') packet = copy.copy(self._message or self._key) # Do not mutate original cipher, key_bytes, key_block_bytes = self.get_cipher(packet.symmetric_algorithm) cipher = cipher(packet.s2k.make_key(passphrase, key_bytes)) cipher = cipher(packet.encrypted_data[:key_block_bytes]).decryptor() pad_amount = key_block_bytes - (len(packet.encrypted_data[key_block_bytes:]) % key_block_bytes) material = cipher.update(packet.encrypted_data[key_block_bytes:] + (pad_amount*b'\0')) material += cipher.finalize() material = material[:-pad_amount] if packet.s2k_useage == 254: chk = material[-20:] material = material[:-20] if(chk != hashlib.sha1(material)): return None else: chk = unpack('!H', material[-2:])[0] material = material[:-2] if chk != OpenPGP.checksum(material): return None packet.s2k_usage = 0 packet.symmetric_alorithm = 0 packet.encrypted_data = None packet.input = OpenPGP.PushbackGenerator(OpenPGP._gen_one(material)) packet.length = len(material) packet.key_from_input() packet.input = None return packet
def decrypt_secret_key(self, passphrase): if hasattr(passphrase, "encode"): passphrase = passphrase.encode("utf-8") packet = copy.copy(self._message or self._key) # Do not mutate original cipher, key_bytes, key_block_bytes = self.get_cipher(packet.symmetric_algorithm) cipher = cipher(packet.s2k.make_key(passphrase, key_bytes)) cipher = cipher(packet.encrypted_data[:key_block_bytes]).decryptor() pad_amount = key_block_bytes - (len(packet.encrypted_data[key_block_bytes:]) % key_block_bytes) material = cipher.update(packet.encrypted_data[key_block_bytes:] + (pad_amount * b"\0")) material += cipher.finalize() material = material[:-pad_amount] if packet.s2k_useage == 254: chk = material[-20:] material = material[:-20] if chk != hashlib.sha1(material): return None else: chk = unpack("!H", material[-2:])[0] material = material[:-2] if chk != OpenPGP.checksum(material): return None packet.s2k_usage = 0 packet.symmetric_alorithm = 0 packet.encrypted_data = None packet.input = OpenPGP.PushbackGenerator(OpenPGP._gen_one(material)) packet.length = len(material) packet.key_from_input() packet.input = None return packet