杭电OJ——1211 RSA

RSA

Problem Description
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.
 

Sample Input
   
   
   
   
101 103 7 11 7716 7746 7497 126 8486 4708 7746 623 7298 7357 3239
 

Sample Output
   
   
   
   
I-LOVE-ACM.
 

Author
JGShining(极光炫影)
 

Source
杭电ACM省赛集训队选拔赛之热身赛
 

Recommend
Eddy
 
     这才开始做题没多久,心思果然还不够细腻!这道题不认真不还摸不着头脑!细细品尝才得出味道!代码如下:
#include<iostream>
using namespace std;

int main()
{
	int i,sum,k,fn,temp;
	int p,q,e,d,l,n;
	while(scanf("%d%d%d%d",&p,&q,&e,&l)!=EOF)
	{
		n=p*q;
		fn=(p-1)*(q-1); d=1;
		//第一步,先要求出d值,直接用暴力法求!
		while(d*e%fn !=1) d++;
		for(k=1;k<=l;k++)
		{
			cin>>temp;
			for(i=1,sum=1;i<=d;i++)
			{
				sum=sum*temp;
				sum=sum%n;
			}//求出sum,这里是一边求乘积,一边取余,结果和求完乘积后再取余是一致的!
			printf("%c",sum);
		}
		cout<<endl;
	}
	system("pause");
	return 0;
}
/*
#include <stdio.h>
int main()
{
    int p,q,e,l;
    int c;//密文 
    while(scanf("%d%d%d%d",&p,&q,&e,&l)!=EOF){
        int n=p*q;//计算出 p、q 的积 n = p × q
        int fn=(p-1)*(q-1); 
        int d=1;
        while((d*e)%fn!=1){//计算出 d, 使 d × e mod F(n) = 1 mod F(n), d 将用来作私钥
            d++;                   
        } //e=7,d=8743
        while(l--)
        {
              scanf("%d",&c);
              //int m=(c*d)%n; 
              //char m1=(char)m;
             // printf("%c",m1);      
             int t=1;
             for(int j=1;j<=d;j++){//M = D(c) = c^d mod n 解密公式
                 t*=c;        
                 t%=n;        
             } 
             char tc=t;
             printf("%c",tc);                                                  
        }
        printf("\n");
    }
    return 0;    
}
*/


你可能感兴趣的:(杭电OJ——1211 RSA)