How to use the rsa.encrypt function in rsa

To help you get started, we’ve selected a few rsa examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Pushjet / Pushjet-Server-Api / models / gcm.py View on Github external
:type message: Message
        """
        subscriptions = Subscription.query.filter_by(service=message.service).all()
        if len(subscriptions) == 0:
            return 0
        gcm_filter = Gcm.query.filter(Gcm.uuid.in_([l.device for l in subscriptions])).all()

        devices_plain = [r.gcmid for r in gcm_filter if r.pubkey is None]
        devices_crypto = [r for r in gcm_filter if r.pubkey is not None]

        if len(devices_plain) > 0:
            data = {"message": dumps(message.as_dict()), "encrypted": True}
            Gcm.gcm_send(devices_plain, data)
        for device in devices_crypto:
            pubkey = rsa.PublicKey.load_pkcs1(b64decode(device.pubkey), 'DER')
            message_enc = rsa.encrypt(dumps(message.as_dict()), pubkey)

            data = {"message": message_enc, "encrypted": True}
            Gcm.gcm_send([device.gcmid], data)

        if len(gcm_filter) > 0:
            uuids = [g.uuid for g in gcm_filter]
            gcm_subscriptions = Subscription.query.filter_by(service=message.service).filter(Subscription.device.in_(uuids)).all()
            for l in gcm_subscriptions:
                l.timestamp_checked = datetime.utcnow()
            db.session.commit()
        return len(gcm_filter)
github dyseo / AsyncLine / AsyncLine / auth.py View on Github external
def __rsa_crypt(self, message,RSA):
		pub_key = rsa.PublicKey(int(RSA.nvalue, 16), int(RSA.evalue, 16))
		crypto  = rsa.encrypt(message, pub_key)
		return crypto
github iofu728 / spider / bilibili / bilibili.py View on Github external
def encode_login_info(self, hash_salt: str, key: str):
        public_key = rsa.PublicKey.load_pkcs1_openssl_pem(key.encode())
        concate = rsa.encrypt(hash_salt + self.password).encode('utf-8')
        s = base64.b64encode(concate, public_key)
        s = urllib.parse.quote_plus(s)
        return s
github nannantingyu / spider-scrapy / crawl / Cmd / Cmd_weibo_login.py View on Github external
def get_password(self, password, servertime, nonce, pubkey):
        rsaPublickey = int(pubkey, 16)
        key = rsa.PublicKey(rsaPublickey, 65537)  # 创建公钥
        message = str(servertime) + '\t' + str(nonce) + '\n' + str(password)  # 拼接明文js加密文件中得到
        message = message.encode("utf-8")
        passwd = rsa.encrypt(message, key)  # 加密
        passwd = binascii.b2a_hex(passwd)  # 将加密信息转换为16进制。
        return passwd
github yoyzhou / weibo_scrapy / weibo_login.py View on Github external
http://stuvel.eu/files/python-rsa-doc/index.html
    """
    #n, n parameter of RSA public key, which is published by WEIBO.COM
    #hardcoded here but you can also find it from values return from prelogin status above
    weibo_rsa_n = 'EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443'
    
    #e, exponent parameter of RSA public key, WEIBO uses 0x10001, which is 65537 in Decimal
    weibo_rsa_e = 65537
   
    message = str(servertime) + '\t' + str(nonce) + '\n' + str(pwd)
    
    #construct WEIBO RSA Publickey using n and e above, note that n is a hex string
    key = rsa.PublicKey(int(weibo_rsa_n, 16), weibo_rsa_e)
    
    #get encrypted password
    encropy_pwd = rsa.encrypt(message, key)

    #trun back encrypted password binaries to hex string
    return binascii.b2a_hex(encropy_pwd)
github Dawnnnnnn / bilibili-toolkit / login.py View on Github external
self.cookie = f"sid={''.join(random.choices(string.ascii_lowercase + string.digits, k=8))}"
            url = "https://passport.snm0516.aisee.tv/api/captcha?token=5598158bcd8511e2"
            headers = {'Cookie': self.cookie,
                       'Host': "snm0516.aisee.tv",
                       'User-Agent': BiliLogin.ua}
            response = self.get(url, headers=headers, decode=False)
            if response is None:
                continue
            url = "http://106.75.36.27:19951/captcha/v1"
            img = base64.b64encode(response)
            img = str(img, encoding="utf-8")
            json = {'image': img}
            response = self.post(url, json=json, decode=True)
            printer.printer(f"验证码识别结果为: {response['message']}", "Running", "green")
            url = "https://passport.snm0516.aisee.tv/api/tv/login"
            param = f"appkey={appKey}&captcha={response['message']}&channel=master&guid=XYEBAA3E54D502E37BD606F0589A356902FCF&mobi_app={mobi_app}&password={parse.quote_plus(base64.b64encode(rsa.encrypt(f'{keyHash}{self.password}'.encode(), pubKey)))}&platform=android&token=5598158bcd8511e2&ts=0&username={parse.quote_plus(self.username)}"
            data = f"{param}&sign={self.getSign(param)}"
            headers = {'Content-type': "application/x-www-form-urlencoded",
                       'Cookie': self.cookie}
            response = self.post(url, data=data, headers=headers)
        if response and response.get('code') == 0:
            cookie_info = self.access_token_2_cookies(response['data']['token_info']['access_token'])
            for key, value in cookie_info.items():
                self.cookie = self.cookie + key + "=" + value + ";"

            self.access_token = response['data']['token_info']['access_token']
            printer.printer(f"{self.username}登录成功 {self.cookie} {self.access_token}", "Running", "green")
            with open("cookies.txt", "a+", encoding="utf-8")as f:
                f.write(f"{self.username}----{self.cookie}----{self.access_token}\n")
            return self.username, self.cookie, self.access_token
        else:
            printer.printer(f"{self.username}登录失败 {response}", "Error", "red")
github niro8 / weibo_crawler / weibo_search.py View on Github external
def get_password(self, password, servertime, nonce, pubkey):
        rsaPublickey = int(pubkey, 16)
        key = rsa.PublicKey(rsaPublickey, 65537)
        message = str(servertime) + '\t' + str(nonce) + '\n' + str(password)
        message = message.encode("utf-8")
        passwd = rsa.encrypt(message, key)
        passwd = binascii.b2a_hex(passwd)
        return passwd
github OneCodeMonkey / WeiboCrawler / wblogin / login.py View on Github external
def get_password(password, servertime, nonce, pubkey):
    rsa_publickey = int(pubkey, 16)
    key = rsa.PublicKey(rsa_publickey, 65537)
    message = str(servertime) + '\t' + str(nonce) + '\n' + str(password)
    message = message.encode("UTF-8")
    password = rsa.encrypt(message, key)
    password = binascii.b2a_hex(password)

    return password
github xchaoinfo / fuck-login / 019 bilibili / bilibili.py View on Github external
def rsaEncrypt(password):
    url = 'http://passport.bilibili.com/login?act=getkey'
    try:
        getKeyRes = session.get(url)
        token = json.loads(getKeyRes.content.decode('utf-8'))
        pw = str(token['hash'] + password).encode('utf-8')

        key = token['key']
        key = rsa.PublicKey.load_pkcs1_openssl_pem(key)

        pw = rsa.encrypt(pw, key)
        password = binascii.b2a_base64(pw)
        return password
    except:
        return False
github cielpy / WeiboPictureWorkflow / weibo / weibo.py View on Github external
def generate_form_data(nonce, pubkey, servertime, rsakv, username, password):
    rsa_public_key = int(pubkey, 16)
    key = rsa.PublicKey(rsa_public_key, 65537)
    message = str(servertime) + '\t' + str(nonce) + '\n' + str(password)
    passwd = rsa.encrypt(message, key)
    passwd = binascii.b2a_hex(passwd)
    username = urllib2.quote(username)
    username = base64.encodestring(username)
    form_data = {
        'entry': 'weibo',
        'gateway': '1',
        'from': '',
        'savestate': '7',
        'useticket': '1',
        'pagerefer': 'http://weibo.com/p/1005052679342531/home?from=page_100505&mod=TAB&pids=plc_main',
        'vsnf': '1',
        'su': username,
        'service': 'miniblog',
        'servertime': servertime,
        'nonce': nonce,
        'pwencode': 'rsa2',