美文网首页
RSA破解作业

RSA破解作业

作者: 小小程序员一枚 | 来源:发表于2017-11-01 17:12 被阅读0次

Alice decides to use RSA with the public key N = 1889570071. In order to guard against transmission errors, Alice has Bob encrypt his message twice, once using the encryption exponent e1 = 1021763679 and once using the encryption exponent e2 = 519424709. Eve intercepts the two encrypted messages c1 = 1244183534 and c2 = 732959706. Assuming that Eve also knows N and the two encryption exponents e1 and e2. Please help Eve recover Bob’s plaintext without finding a factorization of N.
解:由题意可知:
c1=M^e1 mod N
c2=M^e2 mod N
计算得知:gcd(e1,e2)=1 即e1与e2 互质
则由扩展欧几里得算法可知:e1r+e2s=1(存在 r,s且 r>0,s<0)
c2^s= c2^(-1(-s)) ; c2 c2^(-1)=1 mod N
再由扩展欧几里得算出: c2^(-1)
因此:(c1^r * c2^(-1(-s) ) mod N
=(c1^r * c2^s) mod N
= ((M^e1 mod N)^r * (M^e2 mod N)^s) mod N
=(M^(e1
r) * M^(e2s))mod N
=(M^(e1
r+e2*s)) mod N
=M mod N
即可求出原文M
代码如下:

#include<iostream>
#include"string.h"
using namespace std;

typedef long long int lli;

lli gcd(lli e1,lli e2)
{
    if(e1%e2==0) return e2;
    return gcd(e2,e1%e2);
}

void exgcd(lli e1,lli e2,lli g,lli &s,lli &t)  //用扩展欧几里得算法求s,t,g=gcd(e1,e2)
{
    lli q,r=-1;
    lli x,y;
    lli x0=1;
    lli x1=0;
    lli y0=0;
    lli y1=1;
    lli a=e1;
    lli b=e2;
    while(r!=g)
    {
        r=a%b;
        q=a/b;
        x=x0-q*x1;   //迭代求x,y
        x0=x1;
        x1=x;
        y=y0-q*y1;
        y0=y1;
        y1=y;
        a=b;
        b=r;
    }
    s=x;
    t=y;
}

int transmit(lli r,char *exp)    //将r转换成二进制保存在字符数组exp中
{
    int i=0;
    while(r!=0)
    {
      exp[i++]=r%2+'0';
      r=r/2;
    }
    return i;
}

lli mul_mod(lli c,char *exp,lli n,int length)   //求c^exp mod n的值,length为exp的长度,即转换后的二进制串的长度
{
    lli x=1;
   for(int i=length-1;i>=0;i--)
   {
       x=x*x;
       x=x%n;
       if(exp[i]=='1')
       {
           x=x*c;
           x=x%n;
       }
   }
   return x;
}

int main()
{
    lli e1=1021763679;
    lli e2=519424709;
    lli c1=1244183534;
    lli c2=732959706;
    lli n=1889570071;
    lli r,s,ic2,q;
    lli x,y,m;
    int length;
    char exp[64];
    memset(exp,0,sizeof(exp));
    lli temp=gcd(e1,e2);
    exgcd(e1,e2,temp,r,s);    //求r,s
    exgcd(n,c2,1,q,ic2);        //求c2在mod n乘法下的逆元ic2
    length=transmit(r,exp);
    x=mul_mod(c1,exp,n,length);
    length=transmit(-s,exp);       //s<0,-s>0
    y=mul_mod(ic2,exp,n,length);
    m=(x*y)%n;
    cout<<"result:"<<m<<endl;
}

result:1054592380

相关文章

  • RSA破解作业

    Alice decides to use RSA with the public key N = 18895700...

  • RSA破解作业

    算法思想: 加密过程:c=M^e mod N。解密过程:M=c^d mod N。取c1=2^e mod N,将c和...

  • RSA破解作业

    截至:11月07日23:59提交作业 Alice decides to use RSA with the publ...

  • RSA破解作业

    题目: Alice decides to use RSA with the public key N = 1889...

  • RSA破解作业

    题目 Alice decides to use RSA with the public key N = 18895...

  • RSA破解作业

    Problem: Alice decides to use RSA with the public key N =...

  • RSA破解

    问题: Alice decides to use RSA with the public key N = 1889...

  • RSA破解

    转载请注明出处:http://www.jianshu.com/p/64dcc0133394 题目: You are...

  • 密码学初见

    最早的密码学: 密码本加密 持续到了上世纪的70年代 RSA加密 只能通过因式分解的方式来破解,破解难度巨大rsa...

  • 简单RSA破解

    问题描述 Alice decides to use RSA with the public key N = 188...

网友评论

      本文标题:RSA破解作业

      本文链接:https://www.haomeiwen.com/subject/egdgpxtx.html