antherd / sm-crypto Goto Github PK
View Code? Open in Web Editor NEW国密算法sm2、sm3和sm4的java版。基于js版本进行封装,无缝兼容js版公私钥加解密。
License: MIT License
国密算法sm2、sm3和sm4的java版。基于js版本进行封装,无缝兼容js版公私钥加解密。
License: MIT License
SM4没有ECB加密
SM3目前只支持String类型的,可以支持byte[] 或文件流吗?JS版本应该是支持的。
你好,想请问SM2使用的椭圆曲线方程和安全参数,在Java版本中的SM2代码部分没看见。
简单加密一下居然要花接近4s
@Test
public void testSm2Encrypt() {
String data = "{\"abc\":123}";
String publicKey = "041130412b93ca6ba1b7143d821d363fa12a98bc093dd324e5f47ca7401a75cb8cd098851f40852b5193ca31203f34cc24548dc51ca409933e1d0cd30e4cea6e05";
String privateKey = "16c8160303ed9cf0af3c8d61f93be5e091c2115276bc9152a9cfe2d7b6a903bc";
Long before = System.currentTimeMillis();
String encrypt = Sm2.doEncrypt(data, publicKey);
Long after = System.currentTimeMillis();
System.out.println("加密 " + (after - before) + " ms");
System.out.println("encrypt = " + encrypt);
before = System.currentTimeMillis();
String decrypt = Sm2.doDecrypt(encrypt, privateKey);
after = System.currentTimeMillis();
System.out.println("解密 " + (after - before) + " ms");
System.out.println("decrypt = " + decrypt);
}
使用其他第三方的库对相同数据进行加密只需要大约15ms左右
循环10次测试发现也要大约100-300ms左右
public static void main(String[] args) {
String msg = "159";
// 16 进制字符串,要求为 128 比特
String key = "3124456789abcdeffedcba9876543210";
// 加密,默认使用 pkcs#5 填充,输出16进制字符串
long ens = System.currentTimeMillis();
String encryptData1 = Sm4.encrypt(msg, key);
long ene = System.currentTimeMillis();
System.out.println("encrypt 1 : " + encryptData1);
// 解密,默认使用 pkcs#5 填充,输出 utf8 字符串
long des = System.currentTimeMillis();
String decryptData1 = Sm4.decrypt(encryptData1, key);
long dee = System.currentTimeMillis();
System.out.println("decrypt 1 : " + decryptData1);
System.out.println("encrypt time:" + (dee - des) + "ms" + ". decrypt time:" + (ene-ens) + "ms");
}
out result:
encrypt 1 : a3121e371c1da5e7237d7b18305ce6fd
decrypt 1 : 159
encrypt time:139ms. decrypt time:2582ms
您好,能扩展下SM3摘要带公钥和UserId的实现吗?
publickey: 044310f0521b99a95fcb0a44f21fb0a77e9bda35516eb42208fc3a576ebc8cae93fa94834e22060abd8528483bad0ee010bda0bb32ef111bd8773a91aeb517f8db
privatekey: 00d243de79a2741a3b37cc978add7ff39d7ac0d7c7f24ebadd95f3c3c071d4d5cd
原始串: 1234567890
java版本签名:3044022010ba478cbc88d381b9f9e645e6572ae50878f2ce46eccfcfff753ef7231b834502204511da8360a98384294c3e8893ea2276e18ccc0b954c50671ade8c613a2091b2
js版本验签不通过
bc版本java签名:304602210089d4f88e39ddbd9308a815b9d02aa2190c21a836126f881458d140db486a32a9022100a01db5715825d050ffef9a42121c6ab72a46db0e43004cce5383f1b12a40bb72
js版本验签可以通过
SM4 c# 加密,java解密,可以正常解密,但是中文存在乱码
java 0.3.2版 在6核12线程 基础频率 2.5ghz cpu 上 sm2解密耗时 160ms - 220 ms , 有空能不能提升一下性能,拜托了, 谢谢!
String msg = "hello world! 我是 antherd.";
String key = "0123456789abcdeffedcba9876543210"; // 16 进制字符串,要求为 128 比特
String encryptData1 = Sm4.encrypt(msg, key); // 加密,默认使用 pkcs#5 填充,输出16进制字符串
System.out.println(encryptData1);
Sm4Options sm4Options2 = new Sm4Options();
sm4Options2.setPadding("none");
String encryptData2 = Sm4.encrypt(msg, key, sm4Options2); // 加密,不使用 padding,输出16进制字符串
System.out.println(encryptData2);
Sm4Options sm4Options3 = new Sm4Options();
sm4Options3.setPadding("none");
byte[] encryptData3 = Sm4.hexToBytes(Sm4.encrypt(msg, key, sm4Options3)); // 加密,不使用 padding,输出转为字节数组
System.out.println(encryptData3);
Sm4Options sm4Options4 = new Sm4Options();
sm4Options4.setMode("cbc");
sm4Options4.setIv("fedcba98765432100123456789abcdef");
String encryptData4 = Sm4.encrypt(msg, key, sm4Options4); // 加密,cbc 模式,输出16进制字符串
System.out.println(encryptData4);
四种加密结果都等于 0e395deb10f6e8a17e17823e1fd9bd98ba9d414a1328bb62fd1a83cdedd7e4dd
sm4Options4 不同的iv每次加密出来结果都一样
为什么这个验签比bcprov-jdk慢至少2秒?
加载JS文件,感觉在springboot打包成jar运行时读取不到文件,不过非常了不起,我也是才知道JDK1.8可用运行JS脚本,思路奇特,大佬有sm2加解密的性能测试数据吗?
医保局的要求是将签名返回的字节码进行base64编码,我把string转字节后编码的 ,怎么也不对,作者能帮忙吗 联系方式 13104690 (qq微信都是这个)
sm3 缺少参数
{
key: 'xxxx'
}
如题,烦请解答,谢谢
android上使用报错,Sm2 类 依赖 javax.script.ScriptEngineManager,但是这个类不存在 android SDK中
有什么好的办法吗
不建议直接吞并异常,建议应该与原方法保持一致,吞并异常不知道加解密是否有问题
public static String decrypt(String encryptData, String key, Sm4Options sm4Options) {
if (encryptData == null || encryptData.trim().isEmpty()) return "";
String decryptData = null;
try {
decryptData = (String) invocable.invokeFunction("decrypt", encryptData, key, getOptionsMap(sm4Options));
} catch (ScriptException | NoSuchMethodException e) {
e.printStackTrace();
}
return decryptData;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.