rsa计算

时间:2013-03-12 14:57:32

标签: encryption rsa

我正在尝试为2个小数字解决RSA。我可以计算n,phi和e但是当我必须计算d时我总是卡住。请帮我一样。 示例

        p = 3,      q = 7,
        n =  3*7 = 21,
        phi(21)= 2*6 = 12, 
        e = 5

        d = (5^-1) (mod 21) 

        or

        d * 5 = k * 12 + 1   (where k is some number)

我试图弄清楚d * 5 = 25 = 5 * 12 + 1的计算但这是小数字是否还有其他方法用简单的方法计算d

1 个答案:

答案 0 :(得分:3)

以下伪代码可以为您提供帮助(借助this link

// choose prime factors:
p = 3;
q = 7;

n = p * q; // =21
phi = (p-1)*(p-1); // = 12
// Choose e such that 1 < e < phi and e and n are coprime:
e = 5; 
// Compute a value for d such that (d * e) % phi = 1. 
// in other words, solve 5 * d % 12 = 1
d = 5; // since 5 * 5 = 25; modulo 12 = 1. How odd: d == e...

Public key is (e, n) => (5, 21) 
Private key is (d, n) => (5, 21) 

Testing this out on a "message" with the value of '2':
The encryption of m = 2 is 
    c = 2^5 % 21 = 32 % 21 = 11 
The decryption of c = 11 is 
    m = 11^5 % 21 = 161051 % 21 = 2

如您所见,我们在加密/解密步骤之后收到了“消息”。

请注意,由于e == d,这是(不幸的是)对称密码:如果再次应用加密,您也会收到消息。这表明e的选择很差。这是玩具RSA问题的一个问题......