Erlang中有模逆函数吗?- Erlang

guz6ccqo  于 2023-11-15  发布在  Erlang
关注(0)|答案(3)|浏览(317)

我试图重新创建一个RSA加密程序,我已经在Java到Erlang。我不知道如何生成私钥,虽然。我原来的Java代码:

  1. privateKey = publicKey.modInverse(phi);

字符串
我在网上找不到任何通用的算法来找到'd'或私钥。大多数都是一些小的简单方程,无法在更大的问题中实现,或者教程只是给出私钥而没有解释过程。有人能给我指出一个方向,这样我就可以学习生成私钥了吗?或者如果Erlang中有一个模逆函数,请指出它是什么。
先谢谢你了。
编辑:实际要解的方程是[e*d mod(phi)= 1],其中e是公钥,d是私钥,phi = [p-1][q-1]。我很想在所有其他变量都已知的情况下解出d
EDIT 2:Wolframalpha.com返回多个可能的d值。这是如何工作的?

bvn4nwqk

bvn4nwqk1#

虽然在crypto或public_key模块中可能隐藏着类似的东西,但它不是它们的公共API的一部分。
由于Erlang内置了大整数(普通整数实际上可以是任意大小),因此实现one of the usual algorithms来计算值非常容易。
例如,Extended Euclidean Algorithm特别容易用递归函数实现。

cbwuti44

cbwuti442#

下面的代码完成了这项工作,在wolfram页面中缺少的是你必须选择大于2和小于phi的解决方案,然后只有一个解决方案。

**[编辑]**添加一个测试来检测M和C何时不是相对素数,然后返回一个比之前报告的算术错误更明确的错误元组

  1. -module (decod).
  2. -export ([private/2]).
  3. % In the key generation you start by choosing P and Q 2 big primes
  4. % N = P*Q
  5. % M = (P-1)*(Q-1)
  6. % C a number smaller than M such as gcd(M,C) = 1
  7. % the public key is made of {N,C}
  8. % then lets find U and V such as C*U + M*V = 1 and 2 < U < M
  9. % the private key is {U,N}
  10. private(M,C) when is_integer(M), is_integer(C), M > C->
  11. P = private1({M,0},{C,1}),
  12. adjust(P,M).
  13. private1(_,{1,P}) -> P;
  14. private1(_,{0,_}) -> {error,gcd_not_1};
  15. private1({R1,U1},{R2,U2}=A2) ->
  16. F = R1 div R2,
  17. private1(A2,{R1-R2*F,U1-U2*F}).
  18. adjust(R,_) when is_tuple(R) ->
  19. R;
  20. adjust(P1,M) when P1 =< 2 ->
  21. K = (2 - P1) div M + 1,
  22. P1 + K * M;
  23. adjust(P1,M) when P1 >= M ->
  24. K = (P1 - M) div M + 1,
  25. P1 - K * M;
  26. adjust(P1,_) -> P1.

字符串

展开查看全部
xzv2uavs

xzv2uavs3#

不存在模逆这种东西,因为从技术上讲,模逆的解是无限多的。
重新创建可用版本的唯一方法是使用模本身

  1. int rest = number%dividor;
  2. int quotient = (number-rest)/dividor;
  3. int modInv = dividor*quotient+rest;

字符串

相关问题