def _decrypt(self, joinRN, aes_key, aes_iv):
        """解密

        :param joinRN:
        :return:
        """
        maxbuf = 8192
        a_key = aes_key.encode('iso-8859-15')
        a_iv = aes_iv.encode('iso-8859-15')
        crypto = AESCrypto(a_key, a_iv)

        try:
            fileObject = open(joinRN, 'rb')
            fileStr = fileObject.read()
            fileObject.close()

            print(fileStr)
            data_decrypt = crypto.cbc_decrypt(fileStr)

            fileObject = open(joinRN, 'wb')
            # fileObject.write(bytes(data_decrypt))
            fileObject.write(bytes(data_decrypt, 'utf-8'))
            fileObject.close()

            return data_decrypt

        except PermissionError:
            print('解密时文件被占用,等待1秒再读取')
            time.sleep(1)
            self._decrypt(joinRN, aes_key, aes_iv)
示例#2
0
文件: aes.py 项目: gzshop/taskserver
def decrypt(word, key='LXJMTZCVFKQZNQ2J'):
    try:
        word = word.encode('utf-8')
        crypto = AESCrypto(key, key)
        data = base64.b64decode(word)
        return crypto.decrypt(data)
    except Exception as e:
        print(str(e))
        raise PubErrorCustom("请求格式有误!")
示例#3
0
    def decrypt(self, encryptedData, iv):
        # base64 decode
        sessionKey = base64.b64decode(self.sessionKey)
        encryptedData = base64.b64decode(encryptedData)
        iv = base64.b64decode(iv)

        cipher = AESCrypto(sessionKey, iv)
        decrypted = json.loads(cipher.decrypt(encryptedData))

        if decrypted['watermark']['appid'] != self.appId:
            raise Exception('Invalid Buffer')

        return decrypted
示例#4
0
文件: utils.py 项目: tcwechat/gzh
 def decrypt(self, text, appid):
     """对解密后的明文进行补位删除
     @param text: 密文
     @return: 删除填充补位后的明文
     """
     try:
         cryptor = AESCrypto(self.key, self.key[:16])
         # 使用BASE64对密文进行解码,然后AES-CBC解密
         plain_text = cryptor.decrypt(base64.b64decode(text))
     except Exception as e:
         # print(e)
         raise Exception(ierror.WXBizMsgCrypt_DecryptAES_Error)
     try:
         plain_text = "<xml>" + plain_text.split("<xml>")[1]
         xml_content = plain_text.split("</xml>")[0] + '</xml>'
         from_appid = plain_text.split("</xml>")[1]
     except Exception as e:
         raise Exception(ierror.WXBizMsgCrypt_IllegalBuffer)
     if from_appid != appid:
         raise Exception(ierror.WXBizMsgCrypt_ValidateAppid_Error)
     return xml_content
示例#5
0
文件: utils.py 项目: tcwechat/gzh
 def encrypt(self, text, appid):
     """对明文进行加密
     @param text: 需要加密的明文
     @return: 加密得到的字符串
     """
     # 16位随机字符串添加到明文开头
     len_str = struct.pack("I", socket.htonl(len(text.encode())))
     text = self.get_random_str() + len_str + text.encode() + appid
     # 使用自定义的填充方式对明文进行补位填充
     pkcs7 = PKCS7Encoder()
     text = pkcs7.encode(text)
     # 加密
     cryptor = AESCrypto(self.key, self.key[:16])
     try:
         ciphertext = cryptor.encrypt(text, mode='cbc')
         # 使用BASE64对加密后的字符串进行编码
         return ierror.WXBizMsgCrypt_OK, base64.b64encode(
             ciphertext).decode('utf8')
     except Exception as e:
         #print(e)
         return ierror.WXBizMsgCrypt_EncryptAES_Error, None
示例#6
0
文件: aes.py 项目: gzshop/taskserver
def encrypt(word, key='LXJMTZCVFKQZNQ2J'):
    crypto = AESCrypto(key, key)
    data = crypto.encrypt(word, mode='cbc')
    return base64.b64encode(data)
示例#7
0
import time
def decrypt(word):
    word = word.encode('utf-8')
    crypto = AESCrypto(key, key)
    data = base64.b64decode(word)
    return crypto.decrypt(data)
def encrypt(word):
    crypto = AESCrypto(key, key)
    data = crypto.encrypt(word, mode='cbc')
    return base64.b64encode(data)