Skip to content

Commit

Permalink
Add methods to decrypt received images.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbollacke committed Jul 28, 2016
1 parent 7e5d2e0 commit 1aa1c55
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
from yowsup.common.tools import WATools
from yowsup.common.tools import MimeTools
import os
from Crypto.Cipher import AES
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
from axolotl.kdf.hkdfv3 import HKDFv3
from axolotl.util.byteutil import ByteUtil
import binascii
import base64
class DownloadableMediaMessageProtocolEntity(MediaMessageProtocolEntity):
'''
<message t="{{TIME_STAMP}}" from="{{CONTACT_JID}}"
Expand Down Expand Up @@ -36,6 +45,25 @@ def __str__(self):
out += "File name: %s\n" % self.fileName
return out

def decrypt(self, encimg, refkey):
derivative = HKDFv3().deriveSecrets(refkey, binascii.unhexlify(self.cryptKeys), 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 isEncrypted(self):
return self.cryptKeys and self.mediaKey

def getMediaContent(self):
data = urlopen(self.url).read()
if self.isEncrypted():
data = self.decrypt(data, self.mediaKey)
return bytearray(data)

def getMediaSize(self):
return self.size

Expand Down

0 comments on commit 1aa1c55

Please sign in to comment.