def encryptImg(self,img, refkey, mimetype): print(mimetype) if(mimetype == "audio/wav"): derivative = HKDFv3().deriveSecrets(binascii.unhexlify(refkey), "WhatsApp Audio Keys", 112) else: derivative = HKDFv3().deriveSecrets(binascii.unhexlify(refkey), "WhatsApp Image Keys".encode(), 112) parts = ByteUtil.split(derivative, 16, 32) iv = parts[0] cipherKey = parts[1] macKey=derivative[48:80] mac = hmac.new(macKey,digestmod=hashlib.sha256) mac.update(iv) cipher = AES.new(key=cipherKey, mode=AES.MODE_CBC, IV=iv) imgEnc = cipher.encrypt(self.pad(img)) mac.update(imgEnc) hash = mac.digest() hashKey = ByteUtil.trim(mac.digest(), 10) finalEnc = imgEnc + hashKey return finalEnc
def download_media(self, media_msg, force_download=False): if not force_download: try: if media_msg.content: return BytesIO(b64decode(media_msg.content)) except AttributeError: pass file_data = self.download_file(media_msg.client_url) if not file_data: raise Exception('Impossible to download file') media_key = b64decode(media_msg.media_key) derivative = HKDFv3().deriveSecrets( media_key, binascii.unhexlify(media_msg.crypt_keys[media_msg.type]), 112) parts = ByteUtil.split(derivative, 16, 32) iv = parts[0] cipher_key = parts[1] e_file = file_data[:-10] cr_obj = Cipher(algorithms.AES(cipher_key), modes.CBC(iv), backend=default_backend()) decryptor = cr_obj.decryptor() return BytesIO(decryptor.update(e_file) + decryptor.finalize())
async def download_media(driver: BaseWhalesongDriver, model: MediaMixin) -> BytesIO: """ Download message's attached media file. It will decrypt media file using key on message object. :param driver: :param model: MediaMixin :return: Media stream. """ file_data = (await driver.download_file(model.client_url)).read() try: media_key = b64decode(model.media_key) except Exception: media_key = b64decode(model.media_key + ('=' * (len(model.media_key) % 3))) try: derivative = HKDFv3().deriveSecrets( media_key, binascii.unhexlify(CRYPT_KEYS[model.type]), 112) except KeyError: raise ValueError('Invalid message type') parts = ByteUtil.split(derivative, 16, 32) iv = parts[0] cipher_key = parts[1] e_file = file_data[:-10] cr_obj = Cipher(algorithms.AES(cipher_key), modes.CBC(iv), backend=default_backend()) decryptor = cr_obj.decryptor() return BytesIO(decryptor.update(e_file) + decryptor.finalize())
def decrypt_file(enc_path, media_key, out_path=""): media_key = binascii.hexlify(media_key) derivative = HKDFv3().deriveSecrets(binascii.unhexlify(media_key), binascii.unhexlify(WHATSAPP_KEY), 112) splits = ByteUtil.split(derivative, 16, 32) iv = splits[0] cipher_key = splits[1] cipher = AES.new(key=cipher_key, mode=AES.MODE_CBC, IV=iv) if (out_path == ""): out_path = os.path.splitext(enc_path)[0] chunk_size = 4096 * 10 with open(enc_path, "rb") as in_file: with open(out_path, "wb") as out_file: while True: chunk = in_file.read(chunk_size) try: piece = cipher.decrypt(chunk) except: # Last chunk most likely to get into here # Because cipher needs a multiple of 16 piece = cipher.decrypt(pad(chunk)) if len(chunk) == 0: break # end of file out_file.write(piece) return out_path
def decrypt_file(enc_path, media_key, wakey, out_path): media_key = binascii.hexlify(media_key) derivative = HKDFv3().deriveSecrets(binascii.unhexlify(media_key), binascii.unhexlify(wakey), 112) splits = ByteUtil.split(derivative, 16, 32) iv = splits[0] cipher_key = splits[1] bs = AES.block_size cipher = AES.new(key=cipher_key, mode=AES.MODE_CBC, IV=iv) chunk_size = 4096 * bs with open(enc_path, "rb") as in_file: with open(out_path, "wb") as out_file: while True: chunk = in_file.read(chunk_size) try: piece = cipher.decrypt(chunk) except: # Last chunk most likely to get into here # Because cipher needs a multiple of 16 chunk = chunk[:-10] # assert len(chunk) % bs == 0 piece = cipher.decrypt(chunk) padding_len = piece[-1] piece = piece[:-padding_len] if len(chunk) == 0: break # end of file out_file.write(piece) return out_path
def decrypt(self, encaud, refkey): derivative = HKDFv3().deriveSecrets(refkey, binascii.unhexlify(self.cryptKeys), 112) parts = ByteUtil.split(derivative, 16, 32) iv = parts[0] cipherKey = parts[1] e_aud = encaud[:-10] AES.key_size = 128 cr_obj = AES.new(key=cipherKey, mode=AES.MODE_CBC, IV=iv) return cr_obj.decrypt(e_aud)
def decrypt(self, encimg, refkey): derivative = HKDFv3().deriveSecrets( refkey, binascii.unhexlify("576861747341707020496d616765204b657973"), 112) parts = ByteUtil.split(derivative, 16, 32) iv = parts[0] cipherKey = parts[1] e_img = encimg[:-10] AES.key_size = 128 cr_obj = AES.new(key=cipherKey, mode=AES.MODE_CBC, IV=iv) return cr_obj.decrypt(e_img)
def decrypt(self, encimg, refkey, tipo="image"): cryptKeys = self.getCryptKeys(tipo) refkey = base64.b64decode(refkey) derivative = HKDFv3().deriveSecrets(refkey, binascii.unhexlify(cryptKeys), 112) parts = ByteUtil.split(derivative, 16, 32) iv = parts[0] cipherKey = parts[1] e_img = encimg[:-10] cr_obj = AES.new(key=cipherKey, mode=AES.MODE_CBC, IV=iv) return cr_obj.decrypt(e_img)
def encrypt(self, plaintext, ref_key, media_info): derived = HKDFv3().deriveSecrets(ref_key, media_info, 112) parts = ByteUtil.split(derived, 16, 32) iv = parts[0] key = parts[1] mac_key = derived[48:80] cipher_encryptor = Cipher( algorithms.AES(key), modes.CBC(iv), backend=default_backend() ).encryptor() ciphertext = cipher_encryptor.update(plaintext) + cipher_encryptor.finalize() mac = hmac.new(mac_key, digestmod=hashlib.sha256) mac.update(iv) mac.update(ciphertext) return ciphertext + mac.digest()[:10]
async def download_media(driver, model): file_data = (await driver.download_file(model.client_url)).read() media_key = b64decode(model.media_key) try: derivative = HKDFv3().deriveSecrets(media_key, binascii.unhexlify(CRYPT_KEYS[model.type]), 112) except KeyError: raise ValueError('Invalid message type') parts = ByteUtil.split(derivative, 16, 32) iv = parts[0] cipher_key = parts[1] e_file = file_data[:-10] cr_obj = Cipher(algorithms.AES(cipher_key), modes.CBC(iv), backend=default_backend()) decryptor = cr_obj.decryptor() return BytesIO(decryptor.update(e_file) + decryptor.finalize())
def decrypt(Url,MediaKey,filename,tipo): try: CipherData = urlopen(Url).read() CipherImage = CipherData[:-10] cryptKeys = getCryptKeys(tipo) # SecretsRaw = HKDFv3().deriveSecrets(MediaKey, "WhatsApp Image Keys", 112) SecretsRaw = HKDFv3().deriveSecrets(MediaKey, binascii.unhexlify(cryptKeys), 112) Secrets = ByteUtil.split(SecretsRaw, 16, 32) iv = Secrets[0] CipherKey = Secrets[1] AES.key_size=128 AESInstance = AES.new(key=CipherKey, mode=AES.MODE_CBC, IV=iv) PlainImage = AESInstance.decrypt(CipherImage) with open(filename, 'wb') as f: f.write(PlainImage) f.close() return "success"; except: return;
def decrypt(self, ciphertext, ref_key, media_info): derived = HKDFv3().deriveSecrets(ref_key, media_info, 112) parts = ByteUtil.split(derived, 16, 32) iv = parts[0] key = parts[1] mac_key = derived[48:80] media_ciphertext = ciphertext[:-10] mac_value = ciphertext[-10:] mac = hmac.new(mac_key, digestmod=hashlib.sha256) mac.update(iv) mac.update(media_ciphertext) if mac_value != mac.digest()[:10]: raise ValueError("Invalid MAC") cipher_decryptor = Cipher( algorithms.AES(key), modes.CBC(iv), backend=default_backend() ).decryptor() return cipher_decryptor.update(media_ciphertext) + cipher_decryptor.finalize()
def encryptImg(self, img, refkey): derivative = HKDFv3().deriveSecrets(binascii.unhexlify(refkey), binascii.unhexlify(WHATSAPP_KEY), 112) parts = ByteUtil.split(derivative, 16, 32) iv = parts[0] cipherKey = parts[1] macKey = derivative[48:80] mac = hmac.new(macKey, digestmod=hashlib.sha256) mac.update(iv) cipher = AES.new(key=cipherKey, mode=AES.MODE_CBC, IV=iv) imgEnc = cipher.encrypt(self.pad(img)) mac.update(imgEnc) hash = mac.digest() hashKey = ByteUtil.trim(mac.digest(), 10) finalEnc = imgEnc + hashKey return finalEnc
def setup_decrypt(salt, passphrase): hash = pp = bytes(passphrase, encoding='ascii') digest = hashlib.sha512() digest.update(salt) for i in range(250000): digest.update(hash) digest.update(pp) hash = digest.digest() digest = hashlib.sha512() backup_key = hash[:32] derivative = HKDFv3().deriveSecrets(backup_key, b'Backup Export', 64) cipher_key = derivative[:32] # 1st 32 bytes mac_key = derivative[32:] # 2nd 32 bytes # AES secret key aes_cipher = AES.new(cipher_key, AES.MODE_ECB) b_crypto.backup_key = backup_key b_crypto.cipher_key = cipher_key b_crypto.mac_key = mac_key b_crypto.aes_cipher = aes_cipher
def download_media(self, media_msg): try: if media_msg.content: return BytesIO(b64decode(self.content)) except AttributeError: pass file_data = self.download_file(media_msg.client_url) media_key = b64decode(media_msg.media_key) derivative = HKDFv3().deriveSecrets( media_key, binascii.unhexlify(media_msg.crypt_keys[media_msg.type]), 112) parts = ByteUtil.split(derivative, 16, 32) iv = parts[0] cipher_key = parts[1] e_file = file_data[:-10] AES.key_size = 128 cr_obj = AES.new(key=cipher_key, mode=AES.MODE_CBC, IV=iv) return BytesIO(cr_obj.decrypt(e_file))