使用 Javascript 进行 Diffie-Hellman 密钥交换有时会出错 [英] Diffie-Hellman Key Exchange with Javascript sometimes wrong

查看:90
本文介绍了使用 Javascript 进行 Diffie-Hellman 密钥交换有时会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看完这个视频后http://youtu.be/3QnD2c4Xovk

我一直试图一步一步地遵循它,但一直无法产生相同的结果.

I've been trying to follow it step by step, and haven't been able to produce the same results.

值得注意的是,当我尝试做 Math.pow(3, 54)%17 时,我得到 7.而演讲者得到 15.

Notably, when I try to do Math.pow(3, 54)%17, I get 7. While the speaker gets 15.

我编写了一个方法,该方法应该使用我在 http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange

I wrote a method that is supposed to simulate Diffie Hellman's key exchange using exactly what I found on http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange

这是我的代码:

function diffieHellman(generator, prime, alice_secret, bob_secret){
  var alice_public = Math.pow(generator, alice_secret)%prime
    , bob_public = Math.pow(generator, bob_secret)%prime
    , alice_private = Math.pow(bob_public, alice_secret)%prime
    , bob_private = Math.pow(alice_public, bob_secret)%prime;
  console.log("alice"
  , "\n\t", "secret -- ", alice_secret
  , "\n\t", "public -- ", alice_public
  , "\n\t", "private -- ", alice_private
  )
  console.log("bob"
  , "\n\t", "secret -- ", bob_secret
  , "\n\t", "public -- ", bob_public
  , "\n\t", "private -- ", bob_private
  )
  return {
    alice:{
      secret: alice_secret
    , public: alice_public
    , private: alice_private
    },
    bob:{
      secret: bob_secret
    , public: bob_public
    , private: bob_private
    }
  }
};

这些示例有效:

diffieHellman(3, 17, 4, 12) // 1, 1
diffieHellman(3, 23, 6, 19) // 12, 12
diffieHellman(3, 13, 8, 4) // 9, 9

但是,有些数字不起作用

However, some numbers don't work

diffieHellman(3, 17, 40, 120) // 13, 0
diffieHellman(3, 23, 16, 129) // 21, 2
diffieHellman(3, 13, 44, 11) // 9, 1

我做错了什么?

编辑——我不是要在 Javascript 中为项目实现 Diffie-Hellman 的密钥交换.这只是我最熟悉的语言,但我担心这可能是 javascript 的限制.

Edit -- I'm not trying to implement Diffie-Hellman's Key Exchange in Javascript for a project. It's just the language I'm most comfortable with, but I am afraid if this could be a javascript limitation.

推荐答案

3^54 is 58149737003040059690390169. 它会导致溢出,因此你应该实现 模块化指数,因为我不太了解 javascript,所以我编写了 ac 代码,应该很容易在 javascript 中实现:

3^54 is 58149737003040059690390169. It causes an overflow, therefore you should implement modular exponentation, since i don't know javascript too well i have written a c code which should be easy to implement in javascript :

int power(int a, int b, int prime){
      int result;
      if(b == 0){
           result = 1;
      }else if(b == 1){
           result = a % prime;
      }else if(b % 2 == 0){
           result = power((a*a) % prime, b/2, prime);
           result = result % prime;
      }else{
           result = power((a*a) % prime, b/2, prime);
           result = (result * a) % prime;
      }
  return result;
  }

现在你可以调用这个函数了:

Now you can call this function :

int value = power(3, 54, 17);

它应该可以工作.

添加了 javascript 版本

added javascript version

function power(a, b, prime) {
    if (b <= 0) {
        return 1;
    } else if (b === 1) {
        return a % prime;
    } else if (b % 2 === 0) {
        return power((a * a) % prime, b / 2 | 0, prime) % prime;
    } else {
        return (power((a * a) % prime, b / 2 | 0, prime) * a) % prime;
    }
}

这篇关于使用 Javascript 进行 Diffie-Hellman 密钥交换有时会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆