Example #1
0
class BasePrpCrypto(object):
    def __init__(self, key):
        self.cipher = WeChatCipher(key)

    def get_random_string(self):
        return random_string(16)

    def _encrypt(self, text, _id):
        text = to_binary(text)
        tmp_list = []
        tmp_list.append(to_binary(self.get_random_string()))
        length = struct.pack(b'I', socket.htonl(len(text)))
        tmp_list.append(length)
        tmp_list.append(text)
        tmp_list.append(to_binary(_id))

        text = b''.join(tmp_list)
        text = PKCS7Encoder.encode(text)

        ciphertext = to_binary(self.cipher.encrypt(text))
        return base64.b64encode(ciphertext)

    def _decrypt(self, text, _id, exception=None):
        text = to_binary(text)
        plain_text = self.cipher.decrypt(base64.b64decode(text))
        padding = byte2int(plain_text[-1])
        content = plain_text[16:-padding]
        xml_length = socket.ntohl(struct.unpack(b'I', content[:4])[0])
        xml_content = to_text(content[4:xml_length + 4])
        from_id = to_text(content[xml_length + 4:])
        if from_id != _id:
            exception = exception or Exception
            raise exception()
        return xml_content
Example #2
0
class BasePrpCrypto(object):

    def __init__(self, key):
        self.cipher = WeChatCipher(key)

    def get_random_string(self):
        return random_string(16)

    def _encrypt(self, text, _id):
        text = to_binary(text)
        tmp_list = []
        tmp_list.append(to_binary(self.get_random_string()))
        length = struct.pack(b'I', socket.htonl(len(text)))
        tmp_list.append(length)
        tmp_list.append(text)
        tmp_list.append(to_binary(_id))

        text = b''.join(tmp_list)
        text = PKCS7Encoder.encode(text)

        ciphertext = to_binary(self.cipher.encrypt(text))
        return base64.b64encode(ciphertext)

    def _decrypt(self, text, _id, exception=None):
        text = to_binary(text)
        plain_text = self.cipher.decrypt(base64.b64decode(text))
        padding = byte2int(plain_text[-1])
        content = plain_text[16:-padding]
        xml_length = socket.ntohl(struct.unpack(b'I', content[:4])[0])
        xml_content = to_text(content[4:xml_length + 4])
        from_id = to_text(content[xml_length + 4:])
        if from_id != _id:
            exception = exception or Exception
            raise exception()
        return xml_content
Example #3
0
 def __init__(self, key):
     self.cipher = WeChatCipher(key)
Example #4
0
 def __init__(self, key):
     self.cipher = WeChatCipher(key)
Example #5
0
from wechatpy.crypto.pkcs7 import PKCS7Encoder
from wechatpy.utils import to_text
from wechatpy.replies import EmptyReply
from django.views.decorators.csrf import csrf_exempt
from wechat.models import WechatUser, Message, TextMessage
from twitter_image.models import ImageData
import logging
import base64
from django.core.cache import cache
from django.shortcuts import render, reverse
from wechat import actions

logger = logging.getLogger('wechat')
crypto = WeChatCrypto(settings.WECHAT_TOKEN, settings.WECHAT_AES_KEY,
                      settings.WECHAT_APPID)
imgs_cipher = WeChatCipher(crypto.key)


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',