Git Product home page Git Product logo

sm2_implementation's Introduction

SM2 impl sm2 with RFC6979

SM2 加解密

原理

加密算法

image

解密算法

image

获取公私钥

image

我们在SM2的加解密中要用到SM3作为哈希函数,所以先用python实现SM3。以标准文档中椭圆曲线标准参数为例,参数长度为256bit

add()函数为椭圆曲线上的点加

image

mul_add()为椭圆曲线上的点乘

当然,按照原理我们还实现了加密和解密函数

image

image

之后根据SM2的原理实现加解密功能,其中须生成随机参数时,将该参数设置为标准文档中的值(若需要随机生成,只需修改注释)

该样例为对m="liujinyuan"加解密

输出加密之后的结果m1,m2,m3以及解密之后的结果:liujinyuan

image

SM2 密钥交换实现

哈希函数以及椭圆曲线上运算与SM2加解密相同。

Alice随机生成一个ra计算[ra]G发送给bill,bill随机生成一个rb计算[rb]G发送给Alice

对于BILL,可以使用Alice的ID以及公钥计算Za,同样可以计算自己的Zb,之后利用Alice发送的Ra按照密钥交换 协议可以计算一个密钥。

对于Alice,同样可以计算Za和Zb,并利用Bill的Rb按照密钥交换协议计算一个密钥。

二者计算的密钥相同,从而达到密钥交换。

首先定义了一系列基础操作

image

定义填充规则

image

定义int类型到字符串类型的转化

image

我们还实现了最重要的密钥交换函数(A同理)

image

最终我们进行密钥的交换

image

结果展示

image

SM2签名

按照标准文档参数作为测试,在签名过程中生成的随机值用定值代替(使用随机值时只需修改注释)

按照签名和验签原理实现SM2签名和验签,签名需要消息m和Za,签名验证是需要签名、Za、消息和公钥

对于消息m="Liu"进行测试

image

结果展示

image

签名验证成功

sm2_implementation's People

Contributors

jinyuanliu1 avatar

Stargazers

Mipiace avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.