Python 实现国产SM3加密算法的示例代码
SM3是中华人民共和国政府采用的一种密码散列函数标准,由国家密码管理局于2010年12月17日发布。主要用于报告文件数字签名及验证。
Python3代码如下:
frommathimportceil ############################################################################## # #国产SM3加密算法 # ############################################################################## IV="7380166f4914b2b9172442d7da8a0600a96f30bc163138aae38dee4db0fb0e4e" IV=int(IV.replace("",""),16) a=[] foriinrange(0,8): a.append(0) a[i]=(IV>>((7-i)*32))&0xFFFFFFFF IV=a defout_hex(list1): foriinlist1: print("%08x"%i) print("\n") defrotate_left(a,k): k=k%32 return((a<>(32-k)) T_j=[] foriinrange(0,16): T_j.append(0) T_j[i]=0x79cc4519 foriinrange(16,64): T_j.append(0) T_j[i]=0x7a879d8a defFF_j(X,Y,Z,j): if0<=jandj<16: ret=X^Y^Z elif16<=jandj<64: ret=(X&Y)|(X&Z)|(Y&Z) returnret defGG_j(X,Y,Z,j): if0<=jandj<16: ret=X^Y^Z elif16<=jandj<64: #ret=(X|Y)&((2**32-1-X)|Z) ret=(X&Y)|((~X)&Z) returnret defP_0(X): returnX^(rotate_left(X,9))^(rotate_left(X,17)) defP_1(X): returnX^(rotate_left(X,15))^(rotate_left(X,23)) defCF(V_i,B_i): W=[] foriinrange(16): weight=0x1000000 data=0 forkinrange(i*4,(i+1)*4): data=data+B_i[k]*weight weight=int(weight/0x100) W.append(data) forjinrange(16,68): W.append(0) W[j]=P_1(W[j-16]^W[j-9]^(rotate_left(W[j-3],15)))^(rotate_left(W[j-13],7))^W[j-6] str1="%08x"%W[j] W_1=[] forjinrange(0,64): W_1.append(0) W_1[j]=W[j]^W[j+4] str1="%08x"%W_1[j] A,B,C,D,E,F,G,H=V_i """ print"00", out_hex([A,B,C,D,E,F,G,H]) """ forjinrange(0,64): SS1=rotate_left(((rotate_left(A,12))+E+(rotate_left(T_j[j],j)))&0xFFFFFFFF,7) SS2=SS1^(rotate_left(A,12)) TT1=(FF_j(A,B,C,j)+D+SS2+W_1[j])&0xFFFFFFFF TT2=(GG_j(E,F,G,j)+H+SS1+W[j])&0xFFFFFFFF D=C C=rotate_left(B,9) B=A A=TT1 H=G G=rotate_left(F,19) F=E E=P_0(TT2) A=A&0xFFFFFFFF B=B&0xFFFFFFFF C=C&0xFFFFFFFF D=D&0xFFFFFFFF E=E&0xFFFFFFFF F=F&0xFFFFFFFF G=G&0xFFFFFFFF H=H&0xFFFFFFFF V_i_1=[] V_i_1.append(A^V_i[0]) V_i_1.append(B^V_i[1]) V_i_1.append(C^V_i[2]) V_i_1.append(D^V_i[3]) V_i_1.append(E^V_i[4]) V_i_1.append(F^V_i[5]) V_i_1.append(G^V_i[6]) V_i_1.append(H^V_i[7]) returnV_i_1 defhash_msg(msg): #print(msg) len1=len(msg) reserve1=len1%64 msg.append(0x80) reserve1=reserve1+1 #56-64,add64byte range_end=56 ifreserve1>range_end: range_end=range_end+64 foriinrange(reserve1,range_end): msg.append(0x00) bit_length=(len1)*8 bit_length_str=[bit_length%0x100] foriinrange(7): bit_length=int(bit_length/0x100) bit_length_str.append(bit_length%0x100) foriinrange(8): msg.append(bit_length_str[7-i]) #print(msg) group_count=round(len(msg)/64) B=[] foriinrange(0,group_count): B.append(msg[i*64:(i+1)*64]) V=[] V.append(IV) foriinrange(0,group_count): V.append(CF(V[i],B[i])) y=V[i+1] result="" foriiny: result='%s%08x'%(result,i) returnresult defstr2byte(msg):#字符串转换成byte数组 ml=len(msg) msg_byte=[] msg_bytearray=msg#如果加密对象是字符串,则在此对msg做encode()编码即可,否则不编码 foriinrange(ml): msg_byte.append(msg_bytearray[i]) returnmsg_byte defbyte2str(msg):#byte数组转字符串 ml=len(msg) str1=b"" foriinrange(ml): str1+=b'%c'%msg[i] returnstr1.decode('utf-8') defhex2byte(msg):#16进制字符串转换成byte数组 ml=len(msg) ifml%2!=0: msg='0'+msg ml=int(len(msg)/2) msg_byte=[] foriinrange(ml): msg_byte.append(int(msg[i*2:i*2+2],16)) returnmsg_byte defbyte2hex(msg):#byte数组转换成16进制字符串 ml=len(msg) hexstr="" foriinrange(ml): hexstr=hexstr+('%02x'%msg[i]) returnhexstr defKDF(Z,klen):#Z为16进制表示的比特串(str),klen为密钥长度(单位byte) klen=int(klen) ct=0x00000001 rcnt=ceil(klen/32) Zin=hex2byte(Z) Ha="" foriinrange(int(rcnt)): msg=Zin+hex2byte('%08x'%ct) #print(msg) Ha=Ha+hash_msg(msg) #print(Ha) ct+=1 returnHa[0:klen*2] defsm3_hash(msg,Hexstr=0): """ 封装方法,外部调用 :parammsg:二进制流(如若需要传入字符串,则把str2byte方法里msg做encode()编码一下,否则不编码) :paramHexstr:0 :return:64位SM3加密结果 """ if(Hexstr): msg_byte=hex2byte(msg) else: msg_byte=str2byte(msg) returnhash_msg(msg_byte) if__name__=='__main__': print(sm3_hash(b'SM3Test'))#打印结果:901053b4681483b737dd2dd9f9a7f56805aa1b03337f8c1abb763a96776b8905
以上就是Python实现国产SM3加密算法的示例代码的详细内容,更多关于Python实现国产SM3加密算法的资料请关注毛票票其它相关文章!