⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diffie-hellman.js

📁 可以进行任意多位的大整数的乘方、开方、四则运算等
💻 JS
字号:
///////////////////////////////////////////////////////////////////
// Diffie-Hellman Key Exchange Script
// ----------------------------------
//
// This JScript code shows an example of Diffie-Hellman method for 
// key agreement: 160-bit Diffie-Hellman exchange.
//
// Summary: Exchanging Diffie-Hellman Keys.
// 
// The Diffie-Hellman algorithm makes it possible for two or more 
// hosts to create and share an identical, secret encryption key, 
// by simply sharing information over an insecure network. The 
// information shared over the network is in the form of a couple 
// of constant values and a D-H public key. The process used by two 
// key-exchange participants is as follows: 
// 
// Both participants agree to the "Diffie-Hellman parameters"; a 
// prime number, P (larger than 2) and a base number, G (an integer 
// that is smaller than P). 
// 
// The participants each secretly generate a private number (called 
// X for participant 1 and Y for participant 2), which is less than 
// P-1.
//
// Participant 1 sends its D-H public key to participant 2. 
// 
// Participant 2 computes the secret encryption key by using the 
// information contained in participant 1's public key: 
//
//        M = G^X mod P  (for participant 1)
//
// Participant 2 sends participant 1 its D-H public key. 
//
// Participant 1 computes the secret encryption key by using the 
// information contained in participant 2's public key. 
// 
//        N = G^Y mod P  (for participant 2)
//
// Both participants now exchange the public components (M and N), 
// and the exchanged numbers are converted into a secret session 
// key (called K for participant 1 and L for participant 2) using:
//
//        K = N^X mod P  (for participant 1)
//        L = M^Y mod P  (for participant 2)
//
// Now, K and L should be equal.
//
///////////////////////////////////////////////////////////////////

var strHexP, strHexG, strHexX, strHexY
var strHexM, strHexN, strHexK, strHexL

// A 160-bit prime number, P:
strHexP = "B2 0D B0 B1 01 DF 0C 66 " + 
          "24 FC 13 92 BA 55 F7 7D " + 
          "57 74 81 E5"

// A base, G:
strHexG = "0B 9C 69 FD DC D3 CA 5C " + 
          "5F 9F F6 41 DF 16 0E 65 " + 
          "F3 15 5D 2F"

// Participant 1 generates a private number, X:
strHexX = "69 FD DC D3 CA 5C 5F 9F " + 
          "F6 41 DF 16 0E 65 F3 15 " + 
          "5D 2F 86 5D"

// Participant 2 generates a private number, Y:
strHexY = "0D B2 97 AA 77 FC 4C 54 " + 
          "9F 68 A5 D6 43 87 47 82 " + 
          "D4 6D D3 18"

// Participant 1 generates his D-H public key, M = G^X mod P: 
strHexM = Hpmbmath.ModPow(strHexG, strHexX, strHexP) 

// Participant 2 generates his D-H public key, N = G^Y mod P:
strHexN = Hpmbmath.ModPow(strHexG, strHexY, strHexP) 

// Participant 1 computes the secret encryption key, K = N^X mod P:
strHexK = Hpmbmath.ModPow(strHexN, strHexX, strHexP) 

// Participant 2 computes the secret encryption key, L = M^Y mod P:
strHexL = Hpmbmath.ModPow(strHexM, strHexY, strHexP) 

// L should be same as K:
Hpmbmath.Output("K = " + strHexK)
Hpmbmath.Output("L = " + strHexL)
Hpmbmath.Output("\r\nL should be same as K.")

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -