Erlang中有模逆函数吗?- Erlang

guz6ccqo  于 7个月前  发布在  Erlang
关注(0)|答案(3)|浏览(72)

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

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何时不是相对素数,然后返回一个比之前报告的算术错误更明确的错误元组

-module (decod).

-export ([private/2]).

% In the key generation you start by choosing P and Q 2 big primes
% N = P*Q
% M = (P-1)*(Q-1)
% C a number smaller than M such as gcd(M,C) = 1
% the public key is made of {N,C}
% then lets find U and V such as C*U + M*V = 1 and 2 < U < M
% the private key is {U,N}
private(M,C) when is_integer(M), is_integer(C), M > C-> 
    P = private1({M,0},{C,1}),
    adjust(P,M).

private1(_,{1,P}) -> P;
private1(_,{0,_}) -> {error,gcd_not_1};
private1({R1,U1},{R2,U2}=A2) ->
    F = R1 div R2,
    private1(A2,{R1-R2*F,U1-U2*F}).

adjust(R,_) when is_tuple(R) -> 
    R;
adjust(P1,M) when P1 =< 2 ->
    K = (2 - P1) div M + 1,
    P1 + K * M;
adjust(P1,M) when P1 >= M ->
    K = (P1 - M) div M + 1,
    P1 - K * M;
adjust(P1,_) -> P1.

字符串

xzv2uavs

xzv2uavs3#

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

int rest = number%dividor;
int quotient = (number-rest)/dividor;

int modInv = dividor*quotient+rest;

字符串

相关问题