hdu 1211 RSA 解密

 
RSA is one of the most powerful methods to encrypt data. The RSA algorithm is described as follow:

> choose two large prime integer p, q
> calculate n = p × q, calculate F(n) = (p - 1) × (q - 1)
> choose an integer e(1 < e < F(n)), making gcd(e, F(n)) = 1, e will be the public key
> calculate d, making d × e mod F(n) = 1 mod F(n), and d will be the private key

You can encrypt data with this method :

C = E(m) = m e mod n

When you want to decrypt data, use this method :

M = D(c) = c d mod n

Here, c is an integer ASCII value of a letter of cryptograph and m is an integer ASCII value of a letter of plain text.

Now given p, q, e and some cryptograph, your task is to "translate" the cryptograph into plain text.
 

Input
Each case will begin with four integers p, q, e, l followed by a line of cryptograph. The integers p, q, e, l will be in the range of 32-bit integer. The cryptograph consists of l integers separated by blanks.
 

Output
For each case, output the plain text in a single line. You may assume that the correct result of plain text are visual ASCII letters, you should output them as visualable letters with no blank between them.
 
题意:给出p,q,e 和c的个数,求M(即文本的ASCII码值);
解题思路:1、求M,必须先求d,而d*e mod F(n)=1 mod F(n),其中F(n)=(p-1)*(q-1);这就是模线性方程 a*x=b(% n);  a=e,b=1,n=F(n);
                   用模线性方程求出d;
                  2、再求 M = D(c) = c d mod n;即求同余幕,这也是整数运算中的一个比较有名的算法。
                
#include<iostream>
#include<cmath>
using namespace std;
__int64 modular(__int64 b,__int64 n,__int64 m)//求同余幕
{
    __int64 x=1,power=b%m;
    __int64 temp=n;
    int a[65];
    int k=0,i;
    while(temp>0)
    {
        a[k++]=temp%2;
        temp/=2;
    }
    for(i=0;i<k;i++)
    {
        //printf("%d\n",a[i]);
        if(a[i]==1)
            x=(x*power)%m;
        power=(power*power)%m;
    }
    return x;
}
__int64 extgcd(__int64 a ,__int64 b,__int64 &x,__int64 &y)//扩展欧几里算法
{
    if(b==0)
    {
        x=1;y=0;return a;
    }
    __int64 d=extgcd(b,a%b,x,y);
    __int64 t=x; x=y; y=t-a/b*y;
    return d;
}
__int64 modeq(__int64 a,__int64 b,__int64 n)//求线性方程
{
    __int64 e,i,d,x,y;
    d=extgcd(a,n,x,y);
    //printf("%I64d %I64d %I64d %I64d\n",b,d,x,y);
    if(b%d>0)printf("No answer!\n");
    else
    {
        e=(x*(b/d))%n;
        i=0;
        while(((e+i*(n/d))%n)<0)
        {
            i++;
        }
        return (e+i*(n/d))%n;
    }
    return -1;
}
int main()
{
    __int64 p,q,e,l,d,n,m,c;

    //m=modular(2,644,645);
    //printf("%I64d\n",m);
    while(scanf("%I64d %I64d %I64d %I64d",&p,&q,&e,&l)!=EOF)
    {
        n=p*q;
        d=modeq(e,1,(p-1)*(q-1));
        //printf("%I64d\n",d);
        for(int i=0;i<l;i++)
        {
            scanf("%I64d",&c);
            m=modular(c,d,n);
            printf(i<l-1?"%c":"%c\n",m);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

竹二木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值