Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def RSA_encrypt(public_key, message):
rsakey = rsa.PublicKey.load_pkcs1_openssl_pem(public_key)
encrypted = rsa.encrypt(message.encode('utf-8'), rsakey)
return base64.encodestring(encrypted).decode('utf-8').replace('\n', '')
def _encrypt(self, password):
#获取加密的token
response = self.get('http://passport.bilibili.com/login?act=getkey')
token = json.loads(response.content.decode('utf-8'))
password = str(token['hash'] + password).encode('utf-8')
pub_key = token['key']
pub_key = rsa.PublicKey.load_pkcs1_openssl_pem(pub_key)
message = rsa.encrypt(password, pub_key)
message = binascii.b2a_base64(message)
return message
def get_pwd(self, username, password):
url = 'https://passport.bilibili.com/api/oauth2/getKey'
temp_params = 'appkey=1d8b6e7d45233436'
sign = self.calc_sign(temp_params)
params = {'appkey': '1d8b6e7d45233436', 'sign': sign}
response = requests.post(url, data=params)
value = response.json()['data']
key = value['key']
Hash = str(value['hash'])
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(key.encode())
password = base64.b64encode(rsa.encrypt((Hash + password).encode('utf-8'), pubkey))
password = parse.quote_plus(password)
username = parse.quote_plus(username)
return username, password
def get_key():
url = f"{self.protocol}://passport.bilibili.com/api/oauth2/getKey"
payload = {
'appkey': Bilibili.app_key,
'sign': self.calc_sign(f"appkey={Bilibili.app_key}"),
}
while True:
response = self._requests("post", url, data=payload)
if response and response.get("code") == 0:
return {
'key_hash': response['data']['hash'],
'pub_key': rsa.PublicKey.load_pkcs1_openssl_pem(response['data']['key'].encode()),
}
else:
time.sleep(1)
json = r.json()
data = json['data']
return data['hash'], data['key']
def cnn_captcha(img):
url = "http://47.95.255.188:5000/code"
data = {"image": img}
r = requests.post(url, data=data)
return r.text
self.session.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
h, k = getkey()
pwd = base64.b64encode(
rsa.encrypt(
(h + pwd).encode('utf-8'),
rsa.PublicKey.load_pkcs1_openssl_pem(k.encode()),
),
)
user = parse.quote_plus(user)
pwd = parse.quote_plus(pwd)
r = self.session.post(
'https://passport.bilibili.com/api/v2/oauth2/login',
signed_body('appkey={appkey}&password={password}&username={username}'
.format(appkey=APPKEY, username=user, password=pwd)),
)
json = r.json()
if json['code'] == -105:
# need captcha
self.session.headers['cookie'] = 'sid=xxxxxxxx'
r = self.session.get('https://passport.bilibili.com/captcha')
def login(user, passwd, captchaData=None):
encryptData = getPublicKey()
if not encryptData:
print(u'获取加密公钥失败')
return False
# user = 'pdss@qq.com'
# passwd = '123456'
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(encryptData['public_key'].encode('utf-8'))
postData = {
'_encrypt_code': encryptData['field_value'],
'name': base64.b64encode(rsa.encrypt(user, pubkey)),
'password': base64.b64encode(rsa.encrypt(passwd, pubkey)),
'verify_code': ''
}
# 如需验证码,则添加上验证码
if captchaData is not None:
postData['verify_code'] = captchaData['captcha']
postData['_captcha_code'] = captchaData['_captcha_code']
headers = {'Referer': 'https://lantouzi.com/login'}
login_url = 'https://lantouzi.com/api/uc/login'
# data = {"code":1,"message":"","data":{"id":"534287","name":"opdss","email":"opdss@qq.com","mobile":"13001928646","two_step_login_url":"https:\/\/u.dawanjia.com.cn\/user\/api\/two_step_login?token=0db3e59cb14ae35f48fae043f8669921"}}
data = session.post(login_url, data=postData, headers=headers).content.decode('utf-8')
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
def check_ali_sign(message, sign):
"""
验证ali签名
:param message:
:param sign:
:return:
"""
sign = base64.b64decode(sign)
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(alipay_config.RSA_ALIPAY_PUBLIC)
res = False
try:
res = rsa.verify(message, sign, pubkey)
except Exception as e:
print e
res = False
return res
def verify_with_rsa(public_key, message, sign):
public_key = fill_public_key_marker(public_key)
sign = base64.b64decode(sign)
return rsa.verify(message, sign, rsa.PublicKey.load_pkcs1_openssl_pem(public_key))
def encript_password(password, pubkey):
pub = rsa.PublicKey.load_pkcs1_openssl_pem(pubkey.encode('utf-8'))
encript_passwd = rsa.encrypt(password.encode('utf-8'), pub)
return base64.b64encode(encript_passwd).decode('utf-8')