mirror of
https://github.com/luzhisheng/js_reverse.git
synced 2025-04-21 00:25:09 +08:00
56js加密经典入门数据加密-RSA
This commit is contained in:
parent
2bf0210357
commit
180d4b7f35
36
猿人学练习/56js加密经典入门数据加密-RSA/main.py
Normal file
36
猿人学练习/56js加密经典入门数据加密-RSA/main.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import json
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
def challenge59(page):
|
||||||
|
url = "https://www.python-spider.com/api/challenge59"
|
||||||
|
payload = f"page={page}"
|
||||||
|
session = requests.session()
|
||||||
|
headers = {
|
||||||
|
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
||||||
|
}
|
||||||
|
session.headers = headers
|
||||||
|
response = session.request("POST", url, data=payload)
|
||||||
|
return response.text
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
data_num = 0
|
||||||
|
for page in range(1, 101):
|
||||||
|
res_dict = json.loads(challenge59(page))
|
||||||
|
data_list = res_dict.get('data')
|
||||||
|
|
||||||
|
if page == 51:
|
||||||
|
data_list[0]['value'] = '5734\r'
|
||||||
|
|
||||||
|
data_list_num = []
|
||||||
|
for data in data_list:
|
||||||
|
data_list_num.append(int(data.get('value')))
|
||||||
|
data_num += int(data.get('value'))
|
||||||
|
print(data_list_num, page)
|
||||||
|
print(data_num)
|
||||||
|
print(data_num)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run()
|
0
猿人学练习/56js加密经典入门数据加密-RSA/readme.md
Normal file
0
猿人学练习/56js加密经典入门数据加密-RSA/readme.md
Normal file
78
猿人学练习/56js加密经典入门数据加密-RSA/rsa_encrypt.py
Normal file
78
猿人学练习/56js加密经典入门数据加密-RSA/rsa_encrypt.py
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
import base64
|
||||||
|
import rsa
|
||||||
|
from rsa import common
|
||||||
|
|
||||||
|
|
||||||
|
class RsaUtil(object):
|
||||||
|
PUBLIC_KEY_PATH = 'public_key.pem' # 公钥
|
||||||
|
PRIVATE_KEY_PATH = 'private_key.pem' # 私钥
|
||||||
|
|
||||||
|
# 初始化key
|
||||||
|
def __init__(self,
|
||||||
|
company_pub_file=PUBLIC_KEY_PATH,
|
||||||
|
company_pri_file=PRIVATE_KEY_PATH):
|
||||||
|
|
||||||
|
if company_pub_file:
|
||||||
|
self.company_public_key = rsa.PublicKey.load_pkcs1_openssl_pem(open(company_pub_file).read())
|
||||||
|
if company_pri_file:
|
||||||
|
self.company_private_key = rsa.PrivateKey.load_pkcs1(open(company_pri_file).read())
|
||||||
|
|
||||||
|
def get_max_length(self, rsa_key, encrypt=True):
|
||||||
|
"""加密内容过长时 需要分段加密 换算每一段的长度.
|
||||||
|
:param rsa_key: 钥匙.
|
||||||
|
:param encrypt: 是否是加密.
|
||||||
|
"""
|
||||||
|
blocksize = common.byte_size(rsa_key.n)
|
||||||
|
reserve_size = 11 # 预留位为11
|
||||||
|
if not encrypt: # 解密时不需要考虑预留位
|
||||||
|
reserve_size = 0
|
||||||
|
maxlength = blocksize - reserve_size
|
||||||
|
return maxlength
|
||||||
|
|
||||||
|
def encrypt_by_public_key(self, message):
|
||||||
|
"""使用公钥加密.
|
||||||
|
:param message: 需要加密的内容.
|
||||||
|
加密之后需要对接过进行base64转码
|
||||||
|
"""
|
||||||
|
encrypt_result = b''
|
||||||
|
max_length = self.get_max_length(self.company_public_key)
|
||||||
|
while message:
|
||||||
|
input = message[:max_length]
|
||||||
|
message = message[max_length:]
|
||||||
|
out = rsa.encrypt(input, self.company_public_key)
|
||||||
|
encrypt_result += out
|
||||||
|
encrypt_result = base64.b64encode(encrypt_result)
|
||||||
|
return encrypt_result
|
||||||
|
|
||||||
|
def decrypt_by_private_key(self, message):
|
||||||
|
"""使用私钥解密.
|
||||||
|
:param message: 需要加密的内容.
|
||||||
|
解密之后的内容直接是字符串,不需要在进行转义
|
||||||
|
"""
|
||||||
|
decrypt_result = b""
|
||||||
|
|
||||||
|
max_length = self.get_max_length(self.company_private_key, False)
|
||||||
|
decrypt_message = base64.b64decode(message)
|
||||||
|
while decrypt_message:
|
||||||
|
input = decrypt_message[:max_length]
|
||||||
|
decrypt_message = decrypt_message[max_length:]
|
||||||
|
out = rsa.decrypt(input, self.company_private_key)
|
||||||
|
decrypt_result += out
|
||||||
|
return decrypt_result
|
||||||
|
|
||||||
|
def sign_by_private_key(self, data):
|
||||||
|
"""私钥签名.
|
||||||
|
:param data: 需要签名的内容.
|
||||||
|
使用SHA-1 方法进行签名(也可以使用MD5)
|
||||||
|
签名之后,需要转义后输出
|
||||||
|
"""
|
||||||
|
signature = rsa.sign(str(data), priv_key=self.company_private_key, hash='SHA-1')
|
||||||
|
return base64.b64encode(signature)
|
||||||
|
|
||||||
|
def verify_by_public_key(self, message, signature):
|
||||||
|
"""公钥验签.
|
||||||
|
:param message: 验签的内容.
|
||||||
|
:param signature: 对验签内容签名的值(签名之后,会进行b64encode转码,所以验签前也需转码).
|
||||||
|
"""
|
||||||
|
signature = base64.b64decode(signature)
|
||||||
|
return rsa.verify(message, signature, self.company_public_key)
|
Loading…
x
Reference in New Issue
Block a user