Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

加密解密速度太慢 #8

Closed
Montaro2017 opened this issue May 19, 2022 · 2 comments
Closed

加密解密速度太慢 #8

Montaro2017 opened this issue May 19, 2022 · 2 comments

Comments

@Montaro2017
Copy link

Montaro2017 commented May 19, 2022

简单加密一下居然要花接近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左右

@antherds
Copy link
Collaborator

antherds commented May 20, 2022

简单加密一下居然要花接近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左右

你好,这个代码库是移植版,主要通过Java框架中的js引擎加载原生js文件进行调用。主要耗时在于文件加载的过程,以及对js代码的转义过程,优化空间比较有限。后续可以考虑出个Java原生版本兼容前端js加解密。可以参考这个issue 优化应用代码,或者寻找其他第三方兼容库。也欢迎其他大佬优化提pr。

@Montaro2017
Copy link
Author

已使用hutool解决

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants