-
大小: 8KB文件类型: .zip金币: 2下载: 1 次发布日期: 2021-06-07
- 语言: Python
- 标签: Python;SM2
资源简介
用Python实现符合国家保密局文档的SM2密钥分配、加解密、数字签名和SM3杂凑值的计算,算法自己写的,供学习使用。
代码片段和文件信息
“““
ALi 2017.12.23
for python3
“““
“““
国家密码管理局推荐的曲线参数
椭圆曲线方程:y^2 = x^3+a*x+b
曲线参数:
p=FFFFFFFE FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 00000000 FFFFFFFF FFFFFFFF
a=FFFFFFFE FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 00000000 FFFFFFFF FFFFFFFC
b=28E9FA9E 9D9F5E34 4D5A9E4B CF6509A7 F39789F5 15AB8F92 DDBCBD41 4D940E93
n=FFFFFFFE FFFFFFFF FFFFFFFF FFFFFFFF 7203DF6B 21C6052B 53BBF409 39D54123
Gx=32C4AE2C 1F198119 5F990446 6A39C994 8FE30BBF F2660BE1 715A4589 334C74C7
Gy=BC3736A2 F4F6779C 59BDCEE3 6B692153 D0A9877C C62A4740 02DF32E5 2139F0A0
“““
import random
import math
def SM2_Create(a p n G):#SM2密钥对的生成
d_B = random.randint(1 n-2)
P_B = SM2_Mulyipoint(d_B G a p)
return [d_B P_B]
def SM2_Encrypt(a b p n G P_B M):#加密
h = int((p**0.5+1)**2/n)
klen = len(M)*8
t = “0“
b = True
while int(t 2) == 0:
k = random.randint(1 n-1)
C_1 = SM2_Mulyipoint(k G a p)
C_1 = “00000100“+bin(C_1[0]).replace(‘0b‘ ‘‘).rjust(256 ‘0‘)+bin(C_1[1]).replace(‘0b‘ ‘‘).rjust(256 ‘0‘)
S = SM2_Mulyipoint(h P_B a p)
if S == [float(‘inf‘) float(‘inf‘)]:
print(“ERROR“)
C = “0000“
break
else:
b = False
[x_2 y_2] = SM2_Mulyipoint(k P_B a p)
t = SM2_KDF(bin(x_2).replace(‘0b‘ ‘‘).rjust(256 ‘0‘)+bin(y_2).replace(‘0b‘ ‘‘).rjust(256 ‘0‘) klen)
if b == False:
M = ““.join([bin(ord(i)).replace(‘0b‘ ‘‘).rjust(8 ‘0‘) for i in M])
C_2 = bin(int(M 2) ^ int(t 2)).replace(‘0b‘ ‘‘).rjust(klen ‘0‘)
C_3 = SM3(bin(x_2).replace(‘0b‘ ‘‘).rjust(256 ‘0‘)+M+bin(y_2).replace(‘0b‘ ‘‘).rjust(256 ‘0‘))
C = C_1+C_2+C_3
return C
def SM2_Decrypt(a b p n G d_B C):#解密
h = int((p**0.5+1)**2/n)
klen = len(C)-520-256
C_1 = C[0:520]
C_2 = C[520:520+klen]
C_3 = C[520+klen:776+klen]
x_1 = int(C_1[8:264] 2)
y_1 = int(C_1[264:520] 2)
M = “\0“
L = y_1**2%p
R = (x_1**3+a*x_1+b)%p
if L == R:
S = SM2_Mulyipoint(h [x_1 y_1] a p)
if S != [float(‘inf‘) float(‘inf‘)]:
[x_2 y_2] = SM2_Mulyipoint(d_B [x_1 y_1] a p)
t = SM2_KDF(bin(x_2).replace(‘0b‘ ‘‘).rjust(256 ‘0‘)+bin(y_2).replace(‘0b‘ ‘‘).rjust(256 ‘0‘) klen)
if int(t 2) != 0:
M = bin(int(C_2 2) ^ int(t 2)).replace(‘0b‘ ‘‘).rjust(klen ‘0‘)
u = SM3(bin(x_2).replace(‘0b‘ ‘‘).rjust(256 ‘0‘)+M+bin(y_2).replace(‘0b‘ ‘‘).rjust(256 ‘0‘))
if u == C_3:
M = ““.join([chr(i) for i in [int(M[j:j+8] 2) for j in range(0 len(M) 8)]])
else:
print(“ERROR“)
else:
print(“ERROR“)
else:
print(“ERROR“)
else:
print(“ERROR“)
return M
def SM2_KDF(Z klen):#KDF
c = “00000000000000000000000000000001“
H = []
i = 0
while i < math.ceil(klen/256):
H.append(SM3(Z+c))
c = bin((int(c 2)+1)).replace(‘0b‘ ‘‘).rjust(32 ‘0‘)
i += 1
if klen/32-math.ceil(klen/256) == 0:
H_H = H[math.ceil(klen/256)-1]
else:
H_H = H[math.ceil(klen/256)-1][:klen-256*math.floor(klen/256)]
H.pop()
K = ““
i = 0
while i < len(H):
K = K+H[i]
i
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 9055 2017-12-24 12:22 SM2.py
文件 7909 2017-12-24 14:10 SM2_SA.py
文件 4140 2017-12-23 19:41 SM3.py
- 上一篇:图像目标识别分类
- 下一篇:masscan-python接口
评论
共有 条评论