← 历史存档

这是原站讨论区的存档(快照时间 2016-10-19),只读。

RSA的学习资料

奥利奥 · 2016-04-14 19:39:31 · RSA

关于RSA的学习资料的汇总

7 个回答

Sayalic · 2016-04-14 23:24:14

这题挺有意思的,计划好好写点东西……先放一个简单的提示:

  • msieve或者yafu暴力分解模数

    • msieve只有C源码,需要自己编译

    • yafu直接是windows下可执行程序(exe)

  • 利用Mathematica求逆元(D)

WeaponX · 2016-04-15 11:04:09

继续补充,可以使用在线mma计算

http://www.wolframalpha.com/input/?i=17x+%2B+3120y+%3D1

查看 Integer solution 部分


补充,可以直接使用 BigInteger的modInverse...

System.out.println(new BigInteger("17").modInverse(new BigInteger("3120")));

抛一个Java 拓展欧几里得算法

import java.math.BigInteger;

// 拓展欧几里得算法
public class EGCD {

    // e * d % f = 1 求e对f的乘法逆元
    public static BigInteger egcd(String e, String f) {
        return egcd(new BigInteger(e), new BigInteger(f));
    }

    public static BigInteger egcd(BigInteger e, BigInteger f) {
        BigInteger x2 = BigInteger.ZERO;
        BigInteger x3 = f;
        BigInteger y2 = BigInteger.ONE;
        BigInteger y3 = e;
        BigInteger q, t2, t3;

        while (true) {
            if (y3.equals(BigInteger.ZERO))
                return BigInteger.ZERO;
            if (y3.equals(BigInteger.ONE)) {
                if (y2.signum() < 0)
                    y2 = y2.add(f);
                return y2;
            }
            q = x3.divide(y3);
            t2 = x2.subtract(q.multiply(y2));
            t3 = x3.subtract(q.multiply(y3));
            x2 = y2;
            x3 = y3;
            y2 = t2;
            y3 = t3;
        }
    }

    public static void main(String[] args) {
        System.out.println(egcd("17", "3120"));
    }
}

RogerAFH · 2016-04-15 13:29:17

Msieve + MMA/python
linux 下 Msieve 编译安装需要zlib库和gmp库
windows下好像有个已编译版

mdjhny · 2016-04-16 21:52:37

  1. 分解质因数,用homebrew就可以安装msieve,我机器上大概七分钟分解出来;

  2. wolfram alpha算私钥;

  3. php用bcpowmod($cipher, $d, $modulus)直接一步到位算明文,比bcpow然后再bcmod不知道快到哪里去了。

StdioA · 2016-04-26 20:46:02

  1. 分解质因数用 yafu

  2. WolframAlpha 求逆元
    加密密钥及解密密钥的要求为:
    d^e mod phi(n) = 1

  3. Python 快速幂

    def calc(num, exp, mod):
        """快速幂"""
        if exp == 0:
            return 1
        elif exp == 1:
            return num
        elif exp % 2 == 0:
            return (calc(num, exp/2, mod)**2)%mod
        else:
            return (calc(num, exp/2, mod)**2*num)%mod

答!案!特!别!污!

skysider · 2016-05-01 12:42:57

借鉴楼上的方法:

  1. yafu分解因子

     `yafu "factor(n)"`
  2. 求乘法逆元
    发现python有个gmpy2的包,安装后直接调用gmpy2.invert(e, (p-1)*(q-1))即可求d

  3. 解密
    才注意到python有个内置函数,可以直接求c^d mod n,于是用pow(c,d,n)秒解。。。。。。

cheyulin · 2016-10-18 21:19:58

答案无节操啊。。