Code Monkey home page Code Monkey logo

sm2java's Introduction

SM2Java

国密SM2,SM3 Java实现

实现了SM2中如下4部分

1.生成密钥对

2.签名与验签

3.密钥协商

4.公钥加解密

杂凑算法采用SM3 密钥派生算法参考国密办文档中的KDF实现

具体可查看resouces中三个文档

sm2java's People

Contributors

popezlotado avatar zhangqunshi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sm2java's Issues

SM3对大文件提取hash内存溢出

所有分支最终都是通过获取完整的byte[]来生成hash,处理大文件会溢出,可以先获取输入流,每次读取64字节,最后一次不足64再做padding补齐

大数在转化为byte数组时会溢出一位

BigInteger 在转化为byte数组时会会出现一个补位

BigInteger 在转化为byte数组时会会出现一个补位,导致最后的sm3-Hash的计算作物,ZA计算错误

需要在计算sm3杂凑值时,做如下处理

byte[] array = bigInteger.toByteArray();
if (array[0] == 0) {
    byte[] tmp = new byte[array.length - 1];
    System.arraycopy(array, 1, tmp, 0, tmp.length);
    array = tmp;
}

SM2.java中的byte[] ZA(String IDA,Ecpoint aPublicKey)中处理

//这一行的sm3hash中的参数都要做上面的处理
byte[] ZA=sm3hash(............................);

链接
BigInteger to byte[]

IDA

USERID说明

请参阅SM2椭圆曲线公钥密码算法-P54 用户其他信息一栏。
USERID非IDa

设用户A的身份是:[email protected]。用ASCII编码记ID A :
414C 49434531 32334059 41484F4F 2E434F4D。ENTL A =0090。

https://github.com/hyfree/SM2_SM3_SM4Encrypt/tree/add_pdf/pdf

#9

和js加密结合后问题:解密失败

1、生成key
SM2 sm02 = new SM2();
SM2KeyPair keys = sm02.generateKeyPair();
        ECPoint pubKey = keys.getPublicKey();
// out 2ffa1c356eb368e0c01246d56b6332332843f445331612333588d8b36edfe50bca3ef3eeed3525e8e4ecbfa1dd67d1bcf837fe246006808672f4c06998d43b41fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc
        BigInteger privKey = keys.getPrivateKey();
// out : 78880826087100835029051542171897323970932661971431311512363814016306171906887

2、js加密
https://github.com/lifesreason/SM2/

var encryptData = sm2Encrypt('测试数据', '2ffa1c356eb368e0c01246d56b6332332843f445331612333588d8b36edfe50bca3ef3eeed3525e8e4ecbfa1dd67d1bcf837fe246006808672f4c06998d43b41fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc');

3、java解密
BigInteger bi1=new BigInteger("78880826087100835029051542171897323970932661971431311512363814016306171906887");
		System.out.println("解密后明文:" + sm02.decrypt(SM2.hex2String("043ef984f9b18adb082518a29141b033db6dbe64995a72fcfee14440e0ac92bea7890ba276283efbcdc0e54962d64b75a018a73d54250d8af5d91b787305e66c45f84c14526587936c251867531cedb859569d14a4632c0333dfbe51a58f1d096aada78f2b539be43073a67f62"), bi1));

解密失败

SM2加解密

这里只有sm2的公钥加密和私钥解密,有没有私钥加密公钥解密的呢

实例化ECPoiint

您好,我已经有正确的公私钥对,公钥的坐标记作(x,y)。请问在您的代码中,怎么使用已知的公钥坐标正确的实例化 ECPoint PublicKey?
谢谢!

SM3 加盐

sm3 如何使用秘钥做盐,请教一下有没有资料

密钥解析失败

使用openssl中sm2算法生成的公私钥,用importPublicKey和importPrivateKey方法,会报错:
“Exception in thread "main" java.lang.IllegalArgumentException: Invalid point encoding 0x30
at org.bouncycastle.math.ec.ECCurve.decodePoint(Unknown Source)”

计算 ZA 的问题

byte[] ENTLA = new byte[] { (byte) (entlenA & 0xFF00), (byte) (entlenA & 0x00FF) };
是不是应该是
byte[] ENTLA = new byte[] { (byte) (entlenA>>8 & 0xFF), (byte) (entlenA & 0x00FF) };

something is running wrong !

BigInteger h = ecc_bc_spec.getH();
		if (h != null) {
			ECPoint S = publicKey.multiply(h);
			if (S.isInfinity())
				throw new IllegalStateException();
		}

in my environment it warns in ECPoint S = publicKey.multiply(h) of NULLPoint Exception , please check Exception in thread "main" java.lang.NullPointerException
at com.SM2.encrypt(SM2.java:146)
at com.SM2.main(SM2.java:816)

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.