def decrypt_message(self, msg): raw_data = base64.b64decode(msg) decrypted = self.cipher.decrypt(raw_data) plaintext = PKCS7Encoder.decode(decrypted) decrypted_msg = json.loads(to_text(plaintext)) if decrypted_msg["watermark"]["appid"] != self.app_id: raise InvalidAppIdException() return decrypted_msg
def serve_imgs(request, imgs): if request.method == 'GET': try: plain_imgs = imgs_cipher.decrypt(base64.b64decode(imgs)) content = to_text(PKCS7Encoder.decode(plain_imgs)) token = content[:len(settings.WECHAT_TOKEN)] if token != settings.WECHAT_TOKEN: raise Exception('Invalid token.') img_ids = list( map(int, content[len(settings.WECHAT_TOKEN):].split(','))) except: return HttpResponseBadRequest() paths = ImageData.objects.filter(id__in=img_ids).values_list('image', flat=True) if paths.exists(): urls = [reverse('media', kwargs={'path': path}) for path in paths] return render(request, 'imgs.html', {'imgs': urls}) else: return Http404() else: return HttpResponseBadRequest()
def decrypt(cls, string): aes = AES.new(cls.key[:16], mode=AES.MODE_ECB) return PKCS7Encoder.decode(aes.decrypt(base64.decodestring(string)))