当前位置: 首页> 汽车> 时评 > 开发平台 learn_广州白云发布通告_今日头条指数查询_站长工具视频

开发平台 learn_广州白云发布通告_今日头条指数查询_站长工具视频

时间:2025/7/12 9:18:41来源:https://blog.csdn.net/a545958498/article/details/144589384 浏览次数: 0次
开发平台 learn_广州白云发布通告_今日头条指数查询_站长工具视频

使用了ecdh算法,非标准椭圆曲线参数。

分析过程懒得写了,直接上demo

import os
import base64
from util.ENCUtils import ENCUtils
from ecdsa.ellipticcurve import Point, CurveFpclass SoulCustomCurve:_INSTANCE = Nonedef __new__(cls, *args, **kwargs):if not cls._INSTANCE:cls._INSTANCE = super(SoulCustomCurve, cls).__new__(cls, *args, **kwargs)return cls._INSTANCE@staticmethoddef get_instance():if SoulCustomCurve._INSTANCE is None:SoulCustomCurve._INSTANCE = SoulCustomCurve()return SoulCustomCurve._INSTANCEdef __init__(self):# 自定义的椭圆曲线参数self.p = 6277101735386680763835789423207666416102355444459739541047  # p 参数self.a = 0  # a 参数self.b = 3  # b 参数self.Gx = 5377521262291226325198505011805525673063229037935769709693  # G 的 x 坐标self.Gy = 3805108391982600717572440947423858335415441070543209377693  # G 的 y 坐标self.curve = CurveFp(self.p, self.a, self.b)  # 定义椭圆曲线self.G = Point(self.curve, self.Gx, self.Gy)  # 定义椭圆曲线上的基点 Gself.n = 6277101735386680763835789423061264271957123915200845512077  # n 参数# 创建私钥和公钥self.client_private_key = int.from_bytes(os.urandom(24), byteorder='big')  # 使用 os.urandom 生成私钥self.client_public_key = self.point_multiply(self.client_private_key, self.G)self.server_public_key = (5400262085967213433717992037101256438732384858453335109798,6167642468723156401569091099299085180686314126654549364092)self.shared_key = Noneself.shared_key_encoded = Nonedef get_curve(self):# 返回椭圆曲线return self.curvedef get_base_point(self):# 返回基点 Greturn self.Gdef get_order(self):# 返回阶数return self.n# 倍点运算:计算 k * Pdef point_multiply(self, k, p: Point):"""点乘 kP = Q (mod p)"""R = (0, 0)  # 零点Q = (p.x(), p.y())while k:if k & 1:R = self.point_add(R, Q)Q = self.point_add(Q, Q)k >>= 1return Rdef point_add(self, P, Q):"""点加法 P + Q = R (mod p)"""if P == (0, 0):  # 零点,返回 Qreturn Qif Q == (0, 0):  # 零点,返回 Preturn P# 如果 P 或 Q 是 Point 对象,则需要使用 P.x 和 P.y 来获取坐标if P[0] == 0 and P[1] == 0:return Qif Q[0] == 0 and Q[1] == 0:return P# 计算斜率 λif P != Q:m = (Q[1] - P[1]) * pow(Q[0] - P[0], self.curve.p() - 2, self.curve.p()) % self.curve.p()else:m = (3 * P[0] ** 2 + self.curve.a()) * pow(2 * P[1], self.curve.p() - 2, self.curve.p()) % self.curve.p()# 计算新的 x 和 y 坐标x_r = (m ** 2 - P[0] - Q[0]) % self.curve.p()y_r = (m * (P[0] - x_r) - P[1]) % self.curve.p()return x_r, y_rdef get_client_public_key(self):return b"\x04" + \self.client_public_key[0].to_bytes(24, byteorder="big") + \self.client_public_key[1].to_bytes(24, byteorder="big")# return bytes.fromhex("04cf6b0577c58d70bac18ca92f435620f42712ba76a1ba5dc2552c9df82aff1a0f5a12e3fa08c03c4e5e79032b83fb613f")def get_server_public_key(self):return b"\x04" + \self.server_public_key[0].to_bytes(24, byteorder="big") + \self.server_public_key[1].to_bytes(24, byteorder="big")def get_client_private_key(self):return self.client_private_key.to_bytes(24, byteorder="big")def get_shared_key(self):if self.shared_key is None:pub_key = self.server_public_keypub_key_point = Point(self.get_curve(), pub_key[0], pub_key[1])shared_key_tuple = self.client_private_key * pub_key_pointshared_key: int = shared_key_tuple.x()self.shared_key = shared_key.to_bytes(24, byteorder="big")return self.shared_keydef get_shared_key_encoded(self):if self.shared_key_encoded is None:shared_key = self.get_shared_key()shared_key_base64: str = base64.b64encode(shared_key[0:16]).decode("utf-8")salt = ENCUtils.md5("w[" + shared_key_base64[0:4] + "t}")self.shared_key_encoded = salt[-2:] + shared_key_base64 + salt[0:2]key = self.shared_key_encoded.encode("utf-8")if len(key) < 16:key = key + ("\x00" * (16 - len(key)))return key# return bytes.fromhex("343179723633667448627543777366535a34324c4e7635513d3d3664").decode("utf-8")class ExchangeKeyUtil:_COMMON_AES_KEY = b"V0hRuZT+zZmj\x00\x00\x00\x00"@staticmethoddef get_shared_key():return SoulCustomCurve.get_instance().get_shared_key_encoded()@staticmethoddef encrypted_pub_key():client_pub_key = SoulCustomCurve.get_instance().get_client_public_key()return ExchangeKeyUtil._encrypt(client_pub_key, ExchangeKeyUtil._COMMON_AES_KEY)@staticmethoddef encrypt_data(d):key = SoulCustomCurve.get_instance().get_shared_key_encoded()return ExchangeKeyUtil._encrypt(d, key)@staticmethoddef decrypt_data(d):key = SoulCustomCurve.get_instance().get_shared_key_encoded()return ExchangeKeyUtil._decrypt(d, key)@staticmethoddef _decrypt(d, aes_key):not_use = d[0:2]random = d[2:6]zero = d[6:7]raw_encrypted = d[7:]iv = ENCUtils.sha1(random)[0:16]decrypted = ENCUtils.aes_decrypt(raw_encrypted, aes_key[0:16], iv)return decrypted@staticmethoddef _encrypt(d, aes_key):# key = SoulCustomCurve.get_instance().get_shared_key_encoded()random = os.urandom(4)iv = ENCUtils.sha1(random)[0:16]pad_len = 16 - (len(d) & 0xF)final_data = d + (pad_len * pad_len.to_bytes(1, byteorder="big"))encrypted = ENCUtils.aes_encrypt_no_padding(final_data, aes_key[0:16], iv)return bytes.fromhex("0203") + random + b"\x00" + encryptedif __name__ == "__main__":# 使用自定义曲线custom_curve = SoulCustomCurve.get_instance()print(custom_curve.get_shared_key_encoded())print(ExchangeKeyUtil.encrypted_pub_key().hex())

关键字:开发平台 learn_广州白云发布通告_今日头条指数查询_站长工具视频

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: