对RSA的攻击

 第1关:计算φ(n)

根据提示,补全右侧编辑器中 Begin-End 区间的代码,给定nϕ(n),要求输出pq,即n的两个因子。具体要求如下:

  • 从后台获取两个数字nϕ(n),输出n的两个因子pq,输出要求p<q
  • #include<bits/stdc++.h>
    using namespace std;
    #define ll  long  long
    //在下面Begin和End之间补全代码,输出相应的结果
    int main()
    {
        ll n,phi;
        cin>>n>>phi;
        /*********** Begin ***********/
    
        for (ll p = 2; p * p <= n; ++p) {
            if (n % p == 0) {
                ll q = n / p;
                if ((p - 1) * (q - 1) == phi) {
                    cout << p << " " << q << endl;
                    return 0;
                }
            }
        }
    
        /*********** End ***********/
        return 0;
    }

第2关:解密指数 

根据提示,补全右侧编辑器中 Begin-End 区间的代码,给定n1n的四个平方根a,b,c,d,求出n的两个因子。具体要求如下:

  • 从后台获取nabcd,求出n的两个因子。 按照从小到大的顺序输出结果,即p<q
#include<bits/stdc++.h>
using namespace std;
#define ll  long  long
//在下面Begin和End之间补全代码,输出相应的结果
ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
int main()
{
    ll n,a,b,c,d;
    cin>>n;
    cin>>a>>b>>c>>d;
    /*********** Begin ***********/
    vector<ll> roots = {a, b, c, d};

    for (int i = 0; i < 4; ++i) {
        ll x = gcd(roots[i] + 1, n);
        if (x != 1 && x != n) {
            ll y = n / x;
            if (x > y) swap(x, y);
            cout << x << " " << y << endl;
            break;
        }
    }
    /*********** End ***********/
    return 0;
}

第3关:Wiener的低解密指数攻击

根据提示,补全右侧编辑器中 Begin-End 区间的代码,给定a,b,c,d,判断c/d是否是a/b的收敛子。具体要求如下:

  • 从后台获取四个整数a,b,c,d,判断c/d是否是a/b的收敛子,是的话输出YES,否则输出NO
  • #include<bits/stdc++.h>
    using namespace std;
    #define ll  long  long
    //在下面Begin和End之间补全代码,输出相应的结果
    // 辗转相除法求最大公约数
    int gcd(int a, int b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    
    // 计算分数的连分数展开
    std::vector<int> continuedFractionExpansion(double a, double b) {
        std::vector<int> cont_frac;
        while (std::fabs(b) > 1e-9) {
            int q = static_cast<int>(a / b);
            cont_frac.push_back(q);
            double temp = a;
            a = b;
            b = temp - q * b;
        }
        return cont_frac;
    }
    
    // 检查c/d是否是a/b的收敛子
    bool isConvergent(double a, double b, double c, double d) {
        std::vector<int> cont_frac_a = continuedFractionExpansion(a, b);
        std::vector<int> cont_frac_c = continuedFractionExpansion(c, d);
    
        int len_a = cont_frac_a.size();
        int len_c = cont_frac_c.size();
    
        if (len_c > len_a)
            return false;
    
        for (int i = 0; i < len_c; ++i) {
            if (cont_frac_a[i] != cont_frac_c[i])
                return false;
        }
    
        return true;
    }
    
    int main()
    {
        double a,b,c,d;
        cin>>a>>b>>c>>d;
        /*********** Begin ***********/
        if (isConvergent(a, b, c, d))
            std::cout << "YES";
        else
            std::cout << "NO";
    
        /*********** End ***********/
        return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值