程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 杭電OJ——1211 RSA

杭電OJ——1211 RSA

編輯:C++入門知識

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) = me mod n   When you want to decrypt data, use this method :   M = D(c) = cd 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        這才開始做題沒多久,心思果然還不夠細膩!這道題不認真不還摸不著頭腦!細細品嘗才得出味道!代碼如下: [cpp]  #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;      }  */    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved