单向ElGamal代理重新加密实现

时间:2017-07-20 18:42:36

标签: javascript encryption cryptography modular-arithmetic elgamal

我已经在JavaScript中实现了一个ElGamal方案(代码非常糟糕,只是想快速测试一下),基于this解释。

var forge = require('node-forge');
var bigInt = require("big-integer");

var bits = 160;
forge.prime.generateProbablePrime(bits, function(err, num) {
  // Create prime factor and convert to bigInt
  var factor = bigInt(num.toString(10));
  // Find a larger prime of which factor is prime factor
  // Determine a large even number as a co-factor
  var coFactor = bigInt.randBetween("2e260", "3e260"); // should be bitLength(prime) - bitLength(factor)
  var prime = 4;
  while(!coFactor.isEven() || !prime.isPrime()) {
    coFactor = bigInt.randBetween("2e260", "3e260"); // should be bitLength(prime) - bitLength(factor)
    prime = coFactor.multiply(factor);
    prime = prime.add(1);
  }
  // Get a generator g for the multiplicative group mod factor
  var j = prime.minus(1).divide(factor);
  var h = bigInt.randBetween(2, prime.minus(1));
  var g = h.modPow(j, factor);
  // Alice's keys
  // Secret key
  var a = bigInt.randBetween(2, factor.minus(2));
  // Public key
  var A = g.modPow(a, prime);
  // Bob's keys
  // Secret key
  var b = bigInt.randBetween(2, factor.minus(2));
  // Public key
  var B = g.modPow(b, prime);
  // Shared secret
  // Calculated by Alice
  var Sa = B.modPow(a, prime);
  // Calculated by Bob
  var Sb = A.modPow(b, prime);
  // Check
  // Encryption by Alice
  var k = bigInt.randBetween(1, factor.minus(1));
  var c1 = g.modPow(k, prime);
  // Using Bob's public key
  var m = bigInt(2234266) // our message
  var c2 = m.multiply(B.modPow(k, prime));
  // Decryption by Bob
  var decrypt = c1.modPow((prime.minus(b).minus(bigInt(1))), prime).multiply(c2).mod(prime);
  console.log(decrypt); // should be 2234266

这似乎有效,最后的解密步骤返回原始数字。我现在想根据以下想法将其转换为单向代理重新加密方案,该方案取自this论文(第6页,左栏)。

因此,您不必阅读论文,其背后的逻辑是,我们可以将xx1两部分中的私钥x2拆分为x = x1 + x2 {1}}。代理将获得x1并使用x1解密,并将结果传递给最终用户,最终用户将使用x2进行解密。下图使用x1详细描述了代理的第一个数学运算。

Convertion to unidirection ElGamal

其中:

  • m =明文消息
  • g =小组的发电机
  • r =从Zq
  • 随机选择的整数
  • x =密钥

下一步是代理将其传递给最终用户,最终用户将使用x2获取明文m(功能类似于上面的那个)。

现在,我已尝试通过添加代码来实现此功能

  // Proxy re-encryption test
  // x is secret key
  var x = bigInt.randBetween(1, factor.minus(1));
  var x1 = bigInt.randBetween(1, x);
  var x2 = x.minus(x1);
  // y is public key
  var y = g.modPow(x, prime);
  var r = bigInt.randBetween(1, factor.minus(1));
  var c3 = g.modPow(r, prime);
  // mg^xr
  var c4 = bigInt(2234266).multiply(y.modPow(r, prime));

  var _decryptP = c4.divide(g.modPow(x1.multiply(r), prime));
  var _decryptF = _decryptP.divide(g.modPow(x2.multiply(r), prime));
});

遵循与上述等式相同的逻辑。但是,_decryptF不会返回2234266。奇怪的是,它总是返回0.

我的问题是:谁能看到出错的地方?

1 个答案:

答案 0 :(得分:2)

你至少有两个问题:

  • divide除以两个数字。由于两个数都很大,因此divide不太可能是除数的倍数,所以你总是得到0。模块化除法实际上是模乘逆的乘法。因此,a / b实际上是指a * (b-1 (mod p)) (mod p)

  • multiply将两个数字相乘。您可能并且可能使用此功能跳出组(我的意思是您可以获得大于或等于prime的数字)。您必须对结果应用mod操作。从技术上讲,您只需要为最后一个multiply执行此操作,但为中间步骤执行此操作可以显着提高性能,因为数字较小。

以下是生成的代码:

  // Proxy re-encryption test
  // x is secret key
  var x = bigInt.randBetween(1, factor.minus(1));
  var x1 = bigInt.randBetween(1, x);
  var x2 = x.minus(x1);
  // y is public key
  var y = g.modPow(x, prime);
  var r = bigInt.randBetween(1, factor.minus(1));
  var c3 = g.modPow(r, prime);
  // mg^xr
  var c4 = m.multiply(y.modPow(r, prime)).mod(prime);

  var _decryptP = c4.multiply(c3.modPow(x1, prime).modInv(prime)).mod(prime);
  var _decryptF = _decryptP.multiply(c3.modPow(x2, prime).modInv(prime)).mod(prime);
  console.log(_decryptF); // should be 2234266
});

Full code