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 _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 state_imgs(request, user, msg, data, step): with transaction.atomic(): tweets = TaskTweet.objects.select_for_update(skip_locked=True).filter( task__owner=user.user, new=True) if tweets.exists(): ids = tweets.values_list('tweet__images__id', flat=True) content = to_binary( settings.WECHAT_TOKEN + ','.join(map(lambda x: '' if x is None else str(x), ids))) cipher_text = views.imgs_cipher.encrypt( PKCS7Encoder.encode(content)) encoded = to_text(base64.b64encode(cipher_text)) url = request.build_absolute_uri( reverse('wechat_imgs', kwargs={'imgs': encoded})) tweets.update(new=False) return TextReply(content=url, message=msg), None else: return TextReply(content='还没有获取到照片,请稍后再试', message=msg), None
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 _encrypt(self, text): text = to_binary(text) text = PKCS7Encoder.encode(text) ciphertext = to_binary(self.cipher.encrypt(text)) return base64.b64encode(ciphertext)
def decryptEx(cls, string): aes = AES.new(cls.key[:16], mode=AES.MODE_ECB) return PKCS7Encoder.urlsafe_b64decode(aes.decrypt(base64.decodestring(string)))
def encryptEx(cls, string): aes = AES.new(cls.key[:16], mode=AES.MODE_ECB) return base64.urlsafe_b64encode(aes.encrypt(PKCS7Encoder.encode(string)))
def encrypt(cls, string): aes = AES.new(cls.key[:16], mode=AES.MODE_ECB) return base64.encodestring(aes.encrypt(PKCS7Encoder.encode(string)))
# coding=utf-8 import os import json import urllib import base64 import qrcode as qr from Crypto.Cipher import AES from wechatpy.crypto.pkcs7 import PKCS7Encoder url_prefix = r'http://pacs.winning.com.cn/scan/bind?card=' key = 'WinningSoftPACSFilmReportService' aes = AES.new(key[:16], mode=AES.MODE_ECB) print PKCS7Encoder.encode('') print base64.encodestring(aes.encrypt(PKCS7Encoder.encode('123'))) class EncryptConf(object): key = 'WinningSoftPACSFilmReportService' @classmethod def encrypt(cls, string): aes = AES.new(cls.key[:16], mode=AES.MODE_ECB) return base64.encodestring(aes.encrypt(PKCS7Encoder.encode(string))) @classmethod def decrypt(cls, string): aes = AES.new(cls.key[:16], mode=AES.MODE_ECB) return PKCS7Encoder.decode(aes.decrypt(base64.decodestring(string)))