Giter Site home page Giter Site logo

sm-crypto's Issues

SM2椭圆曲线参数

你好,想请问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);
}

这是输出结果
image

使用其他第三方的库对相同数据进行加密只需要大约15ms左右

循环10次测试发现也要大约100-300ms左右

encrypt decrypt time very long

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

java版本的0.3.2版本,和js版本的0.3.8版本,无法互相签名验签

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 , 有空能不能提升一下性能,拜托了, 谢谢!

SM4的使用Sm4Options的加密无效,加密结果与不使用Sm4Options的加密结果一样

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

异常吞并,导致不能处理加解密异常

不建议直接吞并异常,建议应该与原方法保持一致,吞并异常不知道加解密是否有问题

    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;
    }

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.