guanzhi / GmSSL

支持国密SM2/SM3/SM4/SM9/SSL的密码工具箱
http://gmssl.org
Apache License 2.0
5.23k stars 1.67k forks source link

使用3.0版本编译出来的libgmssl.a进行sm2加密后,结果使用java无法解密 #1350

Open itollei opened 2 years ago

itollei commented 2 years ago

iOS端加密如下:

void sw_sm2_test(const char p, const char x, const char y, const char plaintext) { format_print(stderr, 0, 0, "sw_sm2_test\n"); SM2_KEY sm2_key; SM2_KEY pub_key;

sm2_key_generate(&sm2_key);
memcpy(&pub_key, &sm2_key, sizeof(SM2_POINT));
sm2_key_print(stderr, 0, 4, "sm2_key", &sm2_key);

size_t len;
unsigned char ciphertext[SM2_MAX_CIPHERTEXT_SIZE];
if (!sm2_encrypt(&pub_key, (uint8_t *)plaintext, strlen((const char *)plaintext), ciphertext, &len)){
    return;
}
sm2_ciphertext_print(stderr, 0, 4, "Ciphertext", ciphertext, len);
format_bytes(stderr, 0, 4, "ciphertext", ciphertext, len);

size_t outlen;
unsigned char outtext[SM2_MAX_CIPHERTEXT_SIZE];
if (!sm2_decrypt(&sm2_key, ciphertext, len, outtext, &outlen)){
    return;
}
format_string(stderr, 0, 4, "outtext", outtext, outlen);
if (0 == memcmp(plaintext, outtext, outlen)){
    format_print(stderr, 0, 0, "ok!!! \n");
}else {
    format_print(stderr, 0, 0, "fail!!!\n\n");
}

}

sw_sm2_test sm2_key publicKey x: 4C20C85AED71E9BB6CAA6ED833CB8C4258B02F5BCA9887AD34B710895737BCA4 y: C40774BED2A3C4B878FF5DDF5E20B9C3DCFE287502386A5DB98E7D5AADB9C363 privateKey: 58F36A4F596CDA03AAF33BD6107E7C66022322918F7BDB6305A8E262C7A232FB Ciphertext XCoordinate: 04AD997E3C2214EB4EBF70106F9425B6AEB7AD99809C8ED46A813BFC648C3A37 YCoordinate: 437BF6D8A42C41176357C566CD9F15571C58F4411D19BED8231BDACBCCA796A1 HASH: B475033FDC3CCEE8995443188426C66D4CAB60C47F4494D778E8AFBE9AC3E9F6 CipherText: E8E151252B1C ciphertext: 306E022004AD997E3C2214EB4EBF70106F9425B6AEB7AD99809C8ED46A813BFC648C3A370220437BF6D8A42C41176357C566CD9F15571C58F4411D19BED8231BDACBCCA796A10420B475033FDC3CCEE8995443188426C66D4CAB60C47F4494D778E8AFBE9AC3E9F60406E8E151252B1C outtext: 123456 ok!!!

java端解密报: org.bouncycastle.crypto.InvalidCipherTextException: invalid cipher text at org.bouncycastle.crypto.engines.SM2Engine.decrypt(Unknown Source) at org.bouncycastle.crypto.engines.SM2Engine.processBlock(Unknown Source) at org.zz.gmhelper.SM2Util.decrypt(SM2Util.java:144) at org.zz.gmhelper.test.SM2UtilTest.testEncryptAndDecrypt(SM2UtilTest.java:100) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

guanzhi commented 2 years ago

是否可以给出完整可编译的Java代码?

itollei commented 2 years ago

老师,我使用的java代码在https://github.com/itollei/gmhelper-master上,您抽空帮我看下,问题出在哪里?非常感谢!

voyager11 commented 2 years ago

老师,我使用的java代码在https://github.com/itollei/gmhelper-master上,您抽空帮我看下,问题出在哪里?非常感谢!

你好,你搞好了吗?我这边也是 他这个加密出来的密文HEX好奇怪,都是30开头的,应该是用什么格式或者加密过,请问怎么转回来?

open-shuaijun commented 2 years ago

关于GMSSL-SM2与其他主流SM2算法加解密不通过的问题,这里有一个案例可以参考

zhangq1005404376 commented 1 year ago

老师,我使用的java代码在https://github.com/itollei/gmhelper-master上,你抽空帮我看下,问题出在哪里?非常感谢!>> 你好,你搞好了吗?我这边也是他这个加密出来的密文HEX好奇怪,都是30开头的,应该是用什么格式或加密过,请问怎么转回来? 30开头应该der编码的。理论上java那边应该是可以直接解这个格式

wgc0303 commented 1 year ago

兼容.md

jiangyawei commented 1 year ago

你好,有使用sm2给数据长度大于255的数据加密吗?非常感谢告知