Python

# coding:utf-8

import requests


class RC4:
    def __init__(self, public_key=None):
        if not public_key:
            public_key = 'none_public_key'
        self.public_key = public_key
        self.index_i = 0
        self.index_j = 0
        self._init_box()

    def _init_box(self):
        """
        初始化 置换盒
        """

        self.Box = range(256)
        key_length = len(self.public_key)
        j = 0
        for i in range(256):
            index = ord(self.public_key[(i % key_length)])
            j = (j + self.Box[i] + index) % 256
            self.Box[i], self.Box[j] = self.Box[j], self.Box[i]

    def do_crypt(self, string):
        """
        加密/解密
        string : 待加/解密的字符串
        """

        out = []
        for s in string:
            self.index_i = (self.index_i + 1) % 256
            self.index_j = (self.index_j + self.Box[self.index_i]) % 256
            self.Box[self.index_i], self.Box[self.index_j] = self.Box[
                self.index_j], self.Box[self.index_i]

            r = (self.Box[self.index_i] + self.Box[self.index_j]) % 256
            R = self.Box[r]  # 生成伪随机数
            out.append(chr(ord(s) ^ R))

        return ''.join(out)


class Decryption(object):
    corp_secret = "{你的企业的corp_secret}"
    access_token = "{你的企业的access_token}"

    def __init__(self, url):
        self.url = url

    def get_data(self):
        response = requests.get(self.url,
                                params={"access_token": self.access_token})
        data = response.json()
        #****************PY2代码示例********************
        en_data = data["data"].decode("base64") # base64 解出 bytes
        rc4 = RC4(self.corp_secret)
        return rc4.do_crypt(en_data) # 解密 bytes 为 json string


        #***************PY3代码示例********************
        unic_data = data["data"]  # str<unicode>
        en_data = base64.b64decode(unic_data)
        rc4 = RC4(self.corp_secret)
        return rc4.do_crypt(en_data).encode('latin_1').decode('u8')  # 解密 bytes 为 json string