




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
(網絡安全方案設計基礎)實驗報告時間:2021年05月姓名班級學號指導教師實驗成績實驗名稱RSA公鑰密碼實驗目的1、了解和掌握RSA加密算法原理及過程;2、了解生成大素數的方法;3、能夠編寫代碼實現RSA算法。實驗內容1、編寫程序構造RSA密鑰;2、編寫程序生成大素數;3、實現RSA密碼體制。實驗過程#include<stdio.h>#include<stdlib.h>#include<time.h>#include<string.h>#defineACCURACY5#defineSINGLE_MAX100#defineEXPONENT_MAX1000#defineBUF_SIZE1024intmodpow(longa,longb,intc){intres=1;while(b>0){/*Needlongmultiplicationelsethiswilloverflow...*/if^b&1){res=(res*a)%c;}b=b>>1;a=(a*a)%c;/*Samedealhere*/}returnres;}intjacobi(inta,intn){inttwos,temp;intmult=1;while(a>1&&a!=n){a=a%n;if(a<=1||a==n)break;twos=0;while(a%2==0&&++twos)a/=2;/*Factoroutmultiplesof2*/if(twos>0&&twos%2==1)mult*=(n%8==111n%8==7)*2-1;if(a<=1||a==n)break;if(n%4!=1&&a%4!=1)mult*=-1;/*Coefficientforflipping*/temp=a;a=n;n=temp;}if(a==0)return0;elseif(a==1)returnmult;elsereturn0;/*a==n=>gcd(a,n)!=1*/}intsolovayPrime(inta,intn){intx=jacobi(a,n);if(x==-1)x=n-1;returnx!=0&&modpow(a,(n-1)/2,n)==x;}intprobablePrime(intn,intk){if(n==2)return1;elseif(n%2==0||n==1)return0;while(k-->0){if(!solovayPrime(rand()%(n-2)+2,n))return0;}return1;}intrandPrime(intn){intprime=rand()%n;n+=n%2;/*nneedstobeevensomodulowrappingpreservesoddness*/prime+=1-prime%2;while(1){if(probablePrime(prime,ACCURACY))returnprime;prime=(prime+2)%n;}intgcd(inta,intb){inttemp;while(b!=0){temp=b;b=a%b;a=temp;}returna;}intrandExponent(intphi,intn){inte=rand()%n;while(1){if(gcd(e,phi)==1)returne;e=(e+1)%n;if(e<=2)e=3;}}intinverse(intn,intmodulus){inta=n,b=modulus;intx=0,y=1,x0=1,y0=0,q,temp;while(b!=0){q=a/b;temp=a%b;a=b;b=temp;temp=x;x=x0-q*x;x0=temp;temp=y;y=y0-q*y;y0=temp;}if(x0<0)x0+=modulus;returnx0;}intreadFile(FILE*fd,char**buffer,intbytes){intlen=0,cap=BUF_SIZE,r;charbuf[BUF_SIZE];*buffer=(char*)malloc(BUF_SIZE*sizeof(char));while((r=fread(buf,sizeof(char),BUF_SIZE,fd))>0){if(len+r>=cap){cap*=2;*buffer=(char*)realloc(*buffer,cap);}memcpy(&(*buffer)[len],buf,r);len+=r;if(len+bytes-len%bytes>cap)*buffer=(char*)realloc(*buffer,len+bytes-len%bytes);do{(*buffer)[len]=''0';len++;}while(len%bytes!=0);returnlen;}intencode(intm,inte,intn){returnmodpow(m,e,n);}intdecode(intc,intd,intn){returnmodpow(c,d,n);}int*encodeMessage(intlen,intbytes,char*message,intexponent,intmodulus){int*encoded=(int*)malloc((len/bytes)*sizeof(int));intx,i,j;for(i=0;i<len;i+=bytes){x=0;for(j=0;j<bytes;j++)x+=message[i+j]*(1<<(7*j));encoded[i/bytes]=encode(x,exponent,modulus);#ifndefMEASUREprintf("%d",encoded[i/bytes]);#endif}returnencoded;}int*decodeMessage(intlen,intbytes,int*cryptogram,intexponent,intmodulus){int*decoded=(int*)malloc(len*bytes*sizeof(int));intx,i,j;for(i=0;i<len;i++){x=decode(cryptogram[i],exponent,modulus);for(j=0;j<bytes;j++){decoded[i*bytes+j]=(x>>(7*j))%128;#ifndefMEASUREif(decoded[i*bytes+j]!='\0')printf("%c",decoded[i*bytes+j]);#endifreturndecoded;…{intp,q,n,phi,e,d,bytes,len;int*encoded,*decoded;char*buffer;FILE*f;srand(time(NULL));while(1){p=randPrime(SINGLE_MAX);printf("p=%d\n",p);q=randPrime(SINGLE_MAX);printf("q=%d\n",q);n=p*q;printf("n=%d\n",n);if^n<128){}elsebreak;if(n>>21)bytes=3;elseif(n>>14)bytes=2;elsebytes=1;phi=(p-1)*(q-1);printf("phi=(p-1)*(q-1)=%d\n",phi);e=randExponent(phi,EXPONENT_MAX);printf("e=%d\n公鑰:(%d,%d)\n",e,e,n);d=inverse(e,phi);prints”d=%d\n私鑰:(%d,%d)\n”,d,d,n);f=fopen("text.txt","r");iff==NULL){
printf("Failedtoopenfile\"text.txt\".Doesitexist?\n");returnEXIT_FAILURE;}len=readFile(f,&buffer,bytes);/*lenwillbeamultipleofbytes,tosendwholechunks*/fclose(f);printf("\n密文是:");encoded=encodeMessage(len,bytes,buffer,e,n);printf("\n明文是:");decoded=decodeMessage(len/bytes,bytes,encoded,d,n);printf("\n");free(encoded);free(decoded);free(buffer);returnEXIT_SUCCESS;}主程序運行(圖1):實驗結果p=53實驗結果q67n=3551phi=(p-1)*(q-1)=3432史=991公用:(991,3551)d=3103和助;(3103,S551)密文足:325217913066145714571791186618661S66353
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 文化餐飲創新趨勢2025年主題餐廳營銷策略創新研究報告
- 藝術市場數字化交易平臺在藝術品交易中的法律風險識別與應對報告2025
- 快消品企業2025年包裝設計綠色包裝材料應用案例分析報告001
- 2025年可穿戴醫療設備在慢性呼吸系統疾病監測市場的需求與技術創新趨勢
- 城市出行變革:2025年自動駕駛汽車商業化案例分析報告
- 大潤發超市收貨管理制度
- 化妝品公司崗位管理制度
- 團干選拔培訓與管理制度
- 地震綜合觀測站管理制度
- 公司檔案規范與管理制度
- 山東鐵投集團招聘招聘筆試真題2024
- 胰島素皮下注射團體標準解讀 2
- 2025-2030霍爾效應傳感器行業市場現狀供需分析及投資評估規劃分析研究報告
- 《眼科手術新技術》課件
- 《SLT631-2025水利水電工程單元工程施工質量驗收標準》知識培訓
- 2025春季學期國開電大本科《馬克思主義基本原理》一平臺在線形考(專題檢測一至八)試題及答案
- 如何進行護理個案收集
- 全波整流電路周彩霞課件
- 《浙江省中藥飲片炮制規范》 2015年版
- 中國車路云一體化發展研究報告(2024)-賽文研究院
- 瀝青砼路面施工技術難點及解決方案
評論
0/150
提交評論