ECDSA私钥der格式

由于博客内容为空,暂无法提供包含关键信息的摘要。

 

#!/usr/bin/env python3 from ecdsa import NIST521p, VerifyingKey, SigningKey, util from ecdsa.der import UnexpectedDER # 新版库导入位置 import hashlib import binascii # 加载公钥 with open("C:\Users\liuzhiyong\Desktop\public.pem", "rb") as f: vk = VerifyingKey.from_pem(f.read()) curve_order = NIST521p.order # 解析签名文件 signatures = [] with open("C:\Users\liuzhiyong\Desktop\signatures.txt") as f: for line in f: if ':' not in line: continue msg_hex, sig_hex = line.strip().split(':', 1) signatures.append((msg_hex, sig_hex)) # nonce函数(与task.py一致) def nonce(i): seed = hashlib.sha512(b"bias" + bytes([i])).digest() return int.from_bytes(seed, "big") % curve_order # 消息哈希函数 def hash_message(msg_hex): msg_bytes = binascii.unhexlify(msg_hex) return int.from_bytes(hashlib.sha512(msg_bytes).digest(), 'big') % curve_order # 遍历签名 for i, (msg_hex, sig_hex) in enumerate(signatures): try: sig_bytes = binascii.unhexlify(sig_hex) # 双模式签名解析 try: # 优先尝试原始格式 r, s = util.sigdecode_string(sig_bytes, curve_order) except (UnexpectedDER, ValueError): # 回退到DER格式 r, s = util.sigdecode_der(sig_bytes, curve_order) # 计算H(m_i)和k_i h_m = hash_message(msg_hex) k_i = nonce(i) # 计算私钥 r_inv = pow(r, -1, curve_order) d = (s * k_i - h_m) * r_inv % curve_order # 验证私钥 test_msg = b"Verification" sk = SigningKey.from_secret_exponent(d, curve=NIST521p) if vk.verify(sk.sign(test_msg), test_msg): print(f"[+] 使用消息{i}恢复私钥成功!") print(f"[+] 私钥 d = {hex(d)}") break except Exception as e: print(f"[-] 消息{i}处理失败: {str(e)}") else: print("[-] 未能恢复有效私钥") 这样吗
最新发布
12-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值