Giter VIP home page Giter VIP logo

Comments (3)

lpilp avatar lpilp commented on August 13, 2024

02, 03 开头的没有什么区别呢,y是偶数就是02,y是奇数就是03, 公钥的确是x,y 两部分,通过x可以算出y,

from phpsm2sm3sm4.

lpilp avatar lpilp commented on August 13, 2024
function decompressPublicKey($compressedKey) {
  // 获取压缩标志和X坐标
  $flag = substr($compressedKey, 0, 2);
  $x = substr($compressedKey, 2);
  
  // 将16进制字符串转换为大整数
  $p = gmp_init('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF', 16);
  $a = gmp_init('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC', 16);
  $b = gmp_init('28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93', 16);
  $gx = gmp_init('32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1711D7AFB1B8B4E16', 16);
  $gy = gmp_init('BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0', 16);
  $n = gmp_init('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123', 16);
  
  // 计算Y坐标
  $x = gmp_init($x, 16);
  $alpha = gmp_powm($x, 3, $p);
  $beta = gmp_add(gmp_mod(gmp_mul($a, $x), $p), $b);
  $y2 = gmp_mod(gmp_add($alpha, $beta), $p);
  $y = gmp_powm($y2, gmp_div_q(gmp_add($p, 1), 4), $p);
  
  if ($flag == "02") {
    // 如果压缩标志为“02”,则Y坐标为偶数
    if (gmp_strval(gmp_mod($y, 2)) == "0") {
      return "04" . gmp_strval($x, 16) . str_pad(gmp_strval($y, 16), 64, "0", STR_PAD_LEFT);
    } else {
      $y = gmp_sub($p, $y);
      return "04" . gmp_strval($x, 16) . str_pad(gmp_strval($y, 16), 64, "0", STR_PAD_LEFT);
    }
  } elseif ($flag == "03") {
    // 如果压缩标志为“03”,则Y坐标为奇数
    if (gmp_strval(gmp_mod($y, 2)) == "1") {
      return "04" . gmp_strval($x, 16) . str_pad(gmp_strval($y, 16), 64, "0", STR_PAD_LEFT);
    } else {
      $y = gmp_sub($p, $y);
      return "04" . gmp_strval($x, 16) . str_pad(gmp_strval($y, 16), 64, "0", STR_PAD_LEFT);
    }
  } else {
    return null;
  }
}

from phpsm2sm3sm4.

m-finder avatar m-finder commented on August 13, 2024

完美,感谢大佬🙏

from phpsm2sm3sm4.

Related Issues (20)

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.