不能弥合椭圆曲线的Diffie-Hellman使用javascript(Can't brid

2019-09-01 05:08发布

我知道这是一个很具体的问题,但我想PHP和使用基于2个库,似乎声音的椭圆曲线Diffie-Hellman算法(ECDH)的客户端之间交换密钥:

  1. https://github.com/mdanter/phpecc为PHP的一部分,

  2. http://www-cs-students.stanford.edu/~tjw/jsbn/ecdh.html为JS部分。

看来,参数(如可以在第二个演示中可以看出),只是不适合在:(1)马蒂亚斯Danter库!

我的尝试:

一个。 产生在PHP爱丽丝公钥

湾 取x和从(一)y值,并把在JS演示页斯坦福到位爱丽丝领域

C。 从第(二)Bob的公共点和私有密钥产生

d。 重新运行PHP中的(一),但现在重新Alice的属性以前的运行(我不得不在马蒂亚斯Danter类添加一些setter方法),然后使用Bob的公共价值和秘密密钥给Bob重置为JavaScript的价值观

即 导入密钥和比较

但是PHP捕获了一个错误:

Fatal error: Uncaught exception 'ErrorException' with message 'Curve CurveFp Object 
( [a:protected] => -3 [b:protected] => 
2455155546008943817740293915197451784769108058161191238065 [prime:protected] => 
6277101735386680763835789423207666416083908700390324961279 ) does not contain point 
( 1328803036204499271979785126753219480492435117174 , 
-228023147101697490181439300085858154675358736333 )' in 
/var/www/users/test/php/tests/ext/phpecc-master/classes/Point.php:53 

搜索我的代码,我发现是什么原因造成的错误:它是由Alice的公用密钥的计算过程:

public function calculateKey() {
  $this->agreed_key = Point::mul($this->secret, $this->receivedPubPoint)->getX();
}

(从JavaScript)Bob的公共密钥存储在EcDH::receivedPubPoint并生成错误!

下面是一些identation(ECDH类)对象爱丽丝:

Alice:
EcDH Object
(
  [generator:EcDH:private] => Point Object
  (
    [curve] => CurveFp Object
    (
      [a:protected] => -3
      [b:protected] => 2455155546008943817740293915197451784769108058161191238065
      [prime:protected] => 6277101735386680763835789423207666416083908700390324961279
    )

    [x] => 602046282375688656758213480587526111916698976636884684818
    [y] => 174050332293622031404857552280219410364023488927386650641
    [order] => 6277101735386680763835789423176059013767194773182842284081
  )

  [pubPoint:EcDH:private] => Point Object
  (
    [curve] => CurveFp Object
    (
      [a:protected] => -3
      [b:protected] => 2455155546008943817740293915197451784769108058161191238065
      [prime:protected] => 6277101735386680763835789423207666416083908700390324961279
    )

    [x] => 1230571492519579244570075682716266141492045436832711426918
    [y] => 925696034592317781055362853857916815608433923236519324844
    [order] =>
  )

  [receivedPubPoint:EcDH:private] =>
  [secret:EcDH:private] => 14506874945990177925841757912817895350330843517362
  [agreed_key:EcDH:private] =>
)

我的看法是,JavaScript的公共价值是不正确的,由于在“椭圆曲线参数”在执行方式领域http://www-cs-students.stanford.edu/~tjw/jsbn/ecdh.html 。

通过搜索我越发现,这个js与PHP版本相比,斯坦福大学的代码块是有问题的:

// ECCurveFp
// constructor
function ECCurveFp(q,a,b) {
    this.q = q;
    this.a = this.fromBigInteger(a);
    this.b = this.fromBigInteger(b);
    this.infinity = new ECPointFp(this, null, null);
}

// ----------------
// SECNamedCurves
function secp192k1() {
    // p = 2^192 - 2^32 - 2^12 - 2^8 - 2^7 - 2^6 - 2^3 - 1
    var p = fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37");
    var a = BigInteger.ZERO;
    var b = fromHex("3");
    //byte[] S = null;
    var n = fromHex("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D");
    var h = BigInteger.ONE;
    var curve = new ECCurveFp(p, a, b);
    var G = curve.decodePointHex("04"
                + "DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D"
                + "9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D");
    return new X9ECParameters(curve, G, n, h);
}

有没有什么建议?

谢谢。

Answer 1:

你应该确保JS和PHP库使用相同的曲线。 是吗?



文章来源: Can't bridge Elliptic Curve Diffie-Hellman with javascript