ww-rm / gmalg Goto Github PK
View Code? Open in Web Editor NEW国密算法的纯 Python 实现.
Home Page: https://gmalg.readthedocs.io
License: MIT License
国密算法的纯 Python 实现.
Home Page: https://gmalg.readthedocs.io
License: MIT License
我按照测试用例重新写了一个函数,可以自己控制临时私钥,测试过程中发现,uid都是默认值31323334353637383132333435363738的时候,计算结果正常,和其他工具的结果也对得上,但是把uid换成自定义的以后,协商出来的密钥就不一样了
def sm2keyexchange(sk_a, sk_a_temp, uid_a, sk_b, sk_b_temp, uid_b, klen):
klen = 16
sk_a = bytes.fromhex(sk_a)
sk_b = bytes.fromhex(sk_b)
sk_a_temp = int(sk_a_temp, 16)
sk_b_temp = int(sk_b_temp, 16)
pk_a = gmalg.SM2().generate_pk(sk=sk_a)
pk_b = gmalg.SM2().generate_pk(sk=sk_b)
uid_a = uid_a.encode("utf-8")
uid_b = uid_b.encode("utf-8")
sm2A = gmalg.SM2(sk_a, uid_a, pk_a, rnd_fn=lambda _: sk_a_temp)
sm2B = gmalg.SM2(sk_b, uid_b, pk_b, rnd_fn=lambda _: sk_b_temp)
RA, tA = sm2A.begin_key_exchange()
RB, tB = sm2B.begin_key_exchange()
KB = sm2B.end_key_exchange(klen, tB, RA, uid_b, pk_a, gmalg.KEYXCHG_MODE.RESPONDER)
KA = sm2A.end_key_exchange(klen, tA, RB, uid_a, pk_b, gmalg.KEYXCHG_MODE.INITIATOR)
return KA, KB
sk_a = '264c35c1da0dd1b6a705c036cff29b0a541f303a66a6d0cb92f2376e87984903'
sk_a_temp = '5b0eb96ba076ed880c1caa3eed939879138e6ea9910bd3db609dd04c465157bf'
uid_a = 'alice'
sk_b = '4632d797236e0173047c83cb056dac6bdbbdbef34c1697edb47b64cdedd3939f'
sk_b_temp = '46dc55b1474641e25a8fd070daff288a8d12d91ccfdc2ed0f78c6ceda7ff71f5'
uid_b = 'white'
klen = 16
ka, kb = sm2keyexchange(sk_a, sk_a_temp, uid_a, sk_b, sk_b_temp, uid_b, klen)
print(ka.hex())
print(kb.hex())
ka:81396b31831d7216c6d83f59d5b4510c
kb:3b79d4cc40d4ace5b81f3bd461a3894d
uia_a和uid_b的位置搞反了,关闭
sk = bytes.fromhex('4A1ED9ED547F5B957033A17D126AC8A3A53A81AA28458BF0F9C4D5E59823D970')
cipher_text = bytes.fromhex('044B3E911512FD8229C728F5F7DCEA2FFEA328A3E00F633584EBD5C07C7AD62751CC8FE24A275C7C2EB4585B14A673B72CEA871B28D4524D0268A0CDEA554973D25D034079BBB81B483AC4D029C40762F3C5FF187A071CE1F9820AB7DF89A83357273FAF5B3D7A5FFF98169288BCF855B9')
sm2_decrypt_outside = gmalg.SM2(sk=sk)
plain = sm2_decrypt_outside.encrypt(sk)
print('SM2解密出的数据:', plain.hex())
SM2解密出的数据:0406c4d7089bcc6153212c517ad057f94545168cc2c79328f4cb38c7b504f0b43de0297369ad81b35fe514bbabed46ceb3ed25d8bc4118d1bad18684b73f66471f067fe8928462afe986658e1fd0a9375475efc7c5b64d8e8d78ed930f34419f8c32e24604a65a35c130a4a4cd17932cd2f8521587812bda962f10d8c9039e3361
正确答案应该是:D1968C224CAD0C59EA860EA2FB110EA5
这是我们的一道考题,试了好多工具都解不出来,最后在https://the-x.cn/cryptography/Sm2.aspx解出了正确答案。
我有一个私钥
hex:5f10c8b55b530898c4f95f851417e78e3c92400f73ff3f556346d1be6f0008f
dec:2687460962942752655476196615361824446058989685087458871033485227498287399055
用这个私钥计算公钥,KA = gmalg.SM2().generate_pk(bytes.fromhex(K)),计算时报错。
ValueError: non-hexadecimal number found in fromhex() arg at position 63
国标给的私钥范围,我看了下我这个私钥应该是符合的。
d∈[1,n-2],从1到115792089210356248756420345214020892766061623724957744567843809356293439045921
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.