lpilp / phpsm2sm3sm4

php版本,支持国密SM2的签名算法,非对称加解密,SM3的hash, SM4的对称加解密
314 stars 72 forks source link

使用自带tsm2_encrypt.php有极低概率解密失败抛出异常 #13

Closed netcccyun closed 2 years ago

netcccyun commented 2 years ago

用的就是test目录下的tsm2_encrypt.php,密钥和字符串都没改。有极低概率会在第二个字符串解密的时候出现异常

加密后: 04223eeb12b1932812c63fa4b8c3666960a5a197e9edd39c9ec042f1c50a2d8e65b8c16d8f1e5bba2fd1a24cfb366eac34d16a829bd7efa16b81d7d1dc905e48feab153332508c70993ac2b235b39e1481b2bad8c8bc856c735ff742ac63f4e302787668634d7907529ae065cded3f9c9cec Fatal error: Uncaught Mdanter\Ecc\Exception\PointNotOnCurveException: Curve curve(115792089210356248756420345214020892766250353991924191454421193933289684991996, 18505919022281880113072981827955639221458448578012075254857346196103069175443, 115792089210356248756420345214020892766250353991924191454421193933289684991999) does not contain point (1869758440365169714186716832290710150786431124184013618421006632868895141262, 46010032557580391633438832132725706206560994461458522435651677310790484909640) in D:\wwwroot\sm\vendor\mdanter\ecc\src\Primitives\Point.php:102 Stack trace:

0 D:\wwwroot\sm\vendor\lpilp\guomi\src\sm\RtSm2.php(141): Mdanter\Ecc\Primitives\Point->__construct(Object(Mdanter\Ecc\Math\GmpMath), Object(Mdanter\Ecc\Curves\NamedCurveFp), Object(GMP), Object(GMP))

1 D:\wwwroot\sm\tsm2_encrypt.php(19): Rtgm\sm\RtSm2->doDecrypt('04223eeb12b1932...', 'a7763cd4fe7db2a...')

2 {main}

thrown in D:\wwwroot\sm\vendor\mdanter\ecc\src\Primitives\Point.php on line 102

lpilp commented 2 years ago

找到bug了,密码对生成串的返回问题,如果生成的公钥太小转成hex时没有达到128位,而又未进行补足,造成bug. 而加密的时候会用到一个第三方椭圆(调用密码生成对函数),第三方椭圆的公钥当作C1,这时生成的C1就出了问题,无法通过椭圆验证 !已更新,感谢反馈!