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

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

Open
wqh331589996 opened this issue Nov 15, 2022 · 3 comments

Comments

@wqh331589996
Copy link

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

@luocong2016
Copy link

@wqh331589996

哈哈哈

@luocong2016
Copy link

luocong2016 commented Nov 15, 2022

  1. sm4.js 文件存在 map 的 ES6 语法,script 引擎解析错误
  2. sm4 加密参数 msg 小于16位且{padding: 'none' },会按空处理

luocong2016 added a commit to luocong2016/sm-crypto that referenced this issue Nov 16, 2022
antherd#14 修复java script 引擎兼容
@byg0
Copy link

byg0 commented Feb 19, 2024

我也遇到了同样的问题,sm4Options4 不同的iv每次加密出来结果都一样

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

3 participants