【正文】
}printf(%d\n,r)。return r。}void main(){int p,q,e,d,m,n,t,c,r。char s。printf(please input the p,q: )。scanf(%d%d,amp。p,amp。q)。n=p*q。printf(the n is %3d\n,n)。t=(p1)*(q1)。printf(the t is %3d\n,t)。printf(please input the e: )。scanf(%d,amp。e)。if(e1||et){ printf(e is error,please input again: )。 scanf(%d,amp。e)。}d=1。while(((e*d)%t)!=1) d++。printf(then caculate out that the d is %d\n,d)。printf(the cipher please input 1\n)。printf(the plain please input 2\n)。scanf(%d,amp。r)。switch(r){ case 1: printf(input the m: )。 /*輸入要加密的明文數(shù)字*/ scanf(%d,amp。m)。 c=candp(m,e,n)。 printf(the cipher is %d\n,c)。break。 case 2: printf(input the c: )。 /*輸入要解密的密文數(shù)字*/ scanf(%d,amp。c)。 m=candp(c,d,n)。 printf(the cipher is %d\n,m)。break。}getch()。} 代碼的思想:首先隨意輸入兩個素數(shù)p和q,然后利用算法計算出p*q即n,再算出(p1)*(q1)即t,并且同時輸出計算的結果n和t,接下來輸入e,經過算法可以計算出d,由此可以知道RSA算法的公鑰和私鑰;接下來可以有兩個選擇:一選擇輸入明文,有明文經過算法可以計算出密文;二輸入密文,有密文經過算法可以計算出明文。 運行以上代碼就可以得到實驗的結果。五、實驗結果 實驗結果如下圖所示:六、實驗心得:通過這次的實驗,會運用一些現(xiàn)成的算法進行編程,對一些比較復雜的算法開始基本認識并深刻的掌握。在以后所涉及這方面的知識將會有全新的了解和掌握。讓我對RSA算法有了較通透的理解。