资源简介
python3实现的国密SM2+SM3,SM3包括KDF功能,可配合SM2加解密。SM2实现了各种素域下的签名、验签和加解密功能。
代码片段和文件信息
from random import choice
import SM3
# 选择素域,设置椭圆曲线参数
sm2_N = int(‘FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123‘ 16)
sm2_P = int(‘FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF‘ 16)
sm2_G = ‘32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0‘ # G点
sm2_a = int(‘FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC‘16)
sm2_b = int(‘28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93‘16)
sm2_a_3 = (sm2_a + 3) % sm2_P # 倍点用到的中间值
Fp = 256
# sm2_N = int(‘BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677‘ 16)
# sm2_P = int(‘BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F‘ 16)
# sm2_G = ‘4AD5F7048DE709AD51236DE65E4D4B482C836DC6E410664002BB3A02D4AAADACAE24817A4CA3A1B014B5270432DB27D2‘# G点
# sm2_a = int(‘BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985‘16)
# sm2_b = int(‘1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1‘16)
# sm2_a_3 = (sm2_a + 3) % sm2_P # 倍点用到的中间值
# Fp = 192
def get_random_str(strlen):
letterlist = [‘0‘‘1‘‘2‘‘3‘‘4‘‘5‘‘6‘‘7‘‘8‘‘9‘‘a‘‘b‘‘c‘‘d‘‘e‘‘f‘]
str = ‘‘
for i in range(strlen):
a = choice(letterlist)
str = ‘%s%s‘ % (stra)
return str
def kG(k Pointlen_para): # kP运算
Point = ‘%s%s‘ % (Point ‘1‘)
mask_str = ‘8‘
for i in range(len_para-1):
mask_str += ‘0‘
# print(mask_str)
mask = int(mask_str 16)
Temp = Point
flag = False
for n in range(len_para * 4):
if (flag):
Temp = DoublePoint(Templen_para)
if (k & mask) != 0:
if (flag):
Temp = AddPoint(Temp Pointlen_para)
else:
flag = True
Temp = Point
k = k << 1
return ConvertJacb2Nor(Templen_para)
def DoublePoint(Pointlen_para): # 倍点
l = len(Point)
len_2 = 2 * len_para
if l< len_para*2:
return None
else:
x1 = int(Point[0:len_para] 16)
y1 = int(Point[len_para:len_2] 16)
if l == len_2:
z1 = 1
else:
z1 = int(Point[len_2:] 16)
T6 = (z1 * z1) % sm2_P
T2 = (y1 * y1) % sm2_P
T3 = (x1 + T6) % sm2_P
T4 = (x1 - T6) % sm2_P
T1 = (T3 * T4) % sm2_P
T3 = (y1 * z1) % sm2_P
T4 = (T2 * 8) % sm2_P
T5 = (x1 * T4) % sm2_P
T1 = (T1 * 3) % sm2_P
T6 = (T6 * T6) % sm2_P
T6 = (sm2_a_3 * T6) % sm2_P
T1 = (T1 + T6) % sm2_P
z3 = (T3 + T3) % sm2_P
T3 = (T1 * T1) % sm2_P
T2 = (T2 * T4) % sm2_P
x3 = (T3 - T5) % sm2_P
if (T5 % 2) == 1:
T4 = (T5 + ((T5 + sm2_P) >> 1) - T3) % sm2_P
else:
T4 = (T5 + (T5 >> 1) - T3) % sm2_P
T1 = (T1 * T4) % sm2_P
y3 = (T1 - T2) % sm2_P
form = ‘%%0%dx‘ % len_para
form = form * 3
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 9599 2018-03-15 11:11 SM2&SM3\SM2.py
文件 5276 2017-11-29 09:36 SM2&SM3\SM3.py
目录 0 2018-03-15 11:12 SM2&SM3
----------- --------- ---------- ----- ----
14875 3
评论
共有 条评论