Bài tập môn An toàn Bảo mật thông tin - Thực hành phần mật mã

THThiNganHang
2 Downloads
73
0

Vorschau wird generiert...

Inhalt des Dokuments

Seite 1

BÀI TẬP THỰC HÀNH PHẦN MẬT MÃ (12 tiêt) Bài 1 : Kiểm tra số nguyên tố : #include <iostream.h> #include <conio.h> void main() { clrscr(); int x , n; cout<<"nhap vao so nguyen can kiem tra: ";cin>>x; n=2; while(x%n>0) { n++; } if (n==x ) cout<<"La so nguyen to"; else cout<<"Khongla so NT"; getch(); } Bài 2 :Tạo N số nguyên tố đầu tiên #include <stdio.h> #include <math.h> #define N 100 int i,j,k; long int q,r,n,pr[N+1]; int main() { pr[1]=2;n=3;j=1; label1:j++;pr[j]=n; if (j==N) goto label4; label2: n+=2;k=2; label3: q=n/pr[k];r=n%pr[k]; if(r==0) goto label2; if (q<=pr[k]) goto label1; k++;goto label3 ; label4: for(i=1;i<=N;i++) { ThiNganHang.com

Seite 2

printf("%1d ",pr[i]); if(i%10==0) printf(" "); } getchar(); return(n,j,k); }/*main*/ Bài 3 Biểu diễn theo cơ số B /*BIEU DIEN CO SO B*/ #include<stdio.h> #include<math.h> /* input : two non_negative intergers a,b ,b>=2 output : base b representation of a */ int a,b; long repre(int a,int b); long repre0(int a,int b); void main() { a=12;b=2; repre(a,b); a=8;b=4; repre(a,b); a=15245;b=32; repre(a,b); a=748;b=16; repre(a,b); a=14;b=2; repre0(a,b); getchar(); }/* main */ long repre(int a,int b) { int n,i,A[20];long x,q; n=0;q=a/b;A[n]=a%b; while(q>0) { n++;x=q;q=x/b;A[n]=x%b; }/* end while */ printf("a=%1d b=%1d n=%1d\n " ,a,b,n); for(i=0;i<=n;i++) printf(" A[%d]=%1d\n ",i,A[i]); } /* b-representation */ long repre0(int a,int b) { int A0,A1; ThiNganHang.com

Seite 3

A0=a%b;A1=(a/b)%b; printf("a=%ld d=%1d A0=%1d A1=%1d ",a,b,A0,A1); }/* b-representation */ Bài 4 : Cộng hai số biểu diễn theo cơ số B #include<stdio.h> #include<math.h> /* input : Two positive integers x,y in base b representation output : x+y in base b representation */ long a,b,x,y,z,t; int A[100],X[100],Y[100],Z[100],i,n,N; long repre(long a,long b); long add(long x,long y); void main() { x=2748;y=659260;b=32; a=x; /*printf("a=%d b=%d x=%d y=%d \n",a,b,x,y);*/ repre(a,b); N=n; for(i=0;i<=n;i++) X[i]=A[i]; a=y;repre(a,b);if(N<n) N=n; for(i=0;i<=n;i++) Y[i]=A[i]; printf("x=%ld y=%ld b=%ld N=%d \n",x,y,b,N); for(i=0;i<=N;i++) printf("X[%d]=%d Y[%d]=%d \n",i,X[i],i,Y[i]); add(x,y); for(i=0;i<=N;i++) printf("Z[%d]=%d \n",i,Z[i]); /* verification */ z=Z[0];t=1; for(i=1;i<=N+1;i++) {t*=b;z+=(Z[i]*t);} printf("z=%ld x+y=%ld \n",z,x+y);getchar(); }/* main */ long repre(long a,long b) { long q,qq ; n=0 ; q =a/b ; A[n]=a%b ; while(q>0) {n++ ; qq=q ; q=qq/b ; A[n]=qq%b;} } /* b - representation */ long add(long x,long y) { int ii,c=0,tem; /* c is the carry digit */ for(ii=0;ii<=n;ii++) { tem=(X[ii]+Y[ii]+c); if(tem<b) {Z[ii]=tem;c=0;} else {Z[ii]=tem-b;c=1;} }/* ii */ Z[N+1]=c; }/* add */ Bài 5 Nhân hai số theo cơ số B # include<stdio.h> # include<math.h> ThiNganHang.com

Seite 4

/* input : 2 positive integers x , y in base b representation, b>=2 , having N+1 digits respectively ; output : x*y in base b representation , with N+M+2 digits */ long a,x,y,z,t,bb,b,k,B[20] ; int A[20],X[20],Y[20],Z[20],i,n,N,M,A0,A1; long repres(long x,long b); long mult(long x,long y); void main () /* cr5i. C , 21 / 6 / 1998 */ { x=659260 ; y=2748 ; b=32 ; repres(x,b) ; N=n ; for(i=0 ; i<=n ; i++ ) X[i]=A[i] ; repres(y,b) ; N=n ; for(i=0 ; i<=n ; i++) Y[i]=A[i] ; printf("x=%ld y=%ld b=%ld N=%ld M=%ld ",x,y,b,N,M); for (i=0 ; i<=N ;i++) printf("X[%d]=%d Y[%d]=%d",i,X[i],i,Y[i]); mult (x,y) ; for (i=0 ; i<=N+M+1 ; i++) printf("Z[%d]=%d ",i,Z[i]); /* verification */ z=Z[0] ; t=1 ; for(i=0 ; i<=(N+M+1) ; i++) {t*=b ; z+=(Z[i]*t) ;} printf ( "z=%ld x*y=%ld" ,z,x*y ) ; } /*MAIN*/ long mult (long x,long y) { int ii,jj,ca ; for(ii=0 ; ii<= N+M+1 ; ii++ ) Z[ii]=0 ; for (ii=0 ; ii<=M ; ii++) { ca=0 ; for(jj=0 ; jj<=N ; jj++ ) { a=Z[ii+jj]+(X[jj]*Y[ii])+ca ; k=1 ; while(k*b<=a) k++ ; k-- ; A1=k ; A0=a-k*b; Z[ii+jj]=A0 ; ca=A1; } /* jj */ Z[ii+N+1]=A1; } /* ii */ } /* mult */ long a0; int b0 ; long repes (long a0 ,int b0 ) { n=0 ; bb=1 ; B[0]=1 ; while(bb<a0) {bb*=b0 ; n++ ; B[n]=bb ; } n-- ; for (i=n ; i>0 ; i--) { bb=B[i] ; k=1 ; while(( k*bb) <= a0) k++ ; k--; A[i]=k ; a0-=k*bb ; } A[0]=a0; } /* b - repres*/ ThiNganHang.com

Seite 5

Bài 5: Thuật giải Beazout tính gcd của a & b /*Bezout*/ #include <stdio.h> #include <math.h> long A,B; long bezout(long A,long B); void main() /*main*/ { A=9; B=0; bezout(A,B); A=13; B=17;bezout(A,B); A=21; B=36; bezout(A,B); A=2145378312; B=1256993; bezout(A,B); getchar(); } /*Main*/ long a,b; long bezout(long a,long b) { long d,q,r,x,x1,x2,y,y1,y2,tem; if (b==0) { d=a;x=1;y=0; } else { x2=1;x1=0;y2=0;y1=1; while (b>0) { q=a/b;r=a-q*b;x=x2-q*x1;y=y2-q*y1; a=b;b=r;x2=x1;x1=x;y2=y1;y1=y; } d=a;x=x2;y=y2; } tem=A*x+B*y-d; /* if (y<0) y=A+y;*/ printf("A=%ld B=%ld d=%ld x=%ld y=%ld tem=%ld ",A,B,d,x,y,tem); return(a,b); }/*bezout*/ ThiNganHang.com

Seite 6

Bài 6 : Tính lũy thừa modulo nghich đảo /* Multiplicative inverse (ok!) */ #include <stdio.h> #include <math.h> long A,B; long inverse(long B, long A); void main() { B=28; A=25; inverse(B,A); B=13; A=3; inverse(B,A); B=12; A=5; inverse(B,A); B=13; A=3; inverse(B,A); getchar(); } long a,b; long inverse(long b,long a) { long d,q,r,x,x1,x2,y,y1,y2,tem; if (b==0) { d=a;x=1;y=0; } else { x2=1;x1=0;y2=0;y1=1; while (b>0) { q=a/b;r=a-q*b;x=x2-q*x1;y=y2-q*y1; a=b;b=r;x2=x1;x1=x;y2=y1;y1=y; } d=a;x=x2;y=y2; } ThiNganHang.com

Seite 7

tem=A*x+B*y-d; if (y<0) y=A+y; printf("A=%ld B=%ld d=%ld x=%ld y=%ld tem=%ld \n ",A,B,d,x,y,tem); } Bài 7 : Chương trình mã –dich dùng RSA /* Chuong trinh MA-DICH tren RSA . Plain text duoc bieu dien duoi dang mot day so nguyen */ #include <stdio.h> #include <math.h> #include <conio.h> #define I 4 #include<time.h> long Q[40]; int n,N,M; typedef long vector[I+1]; long MP(long long x,long y,long z); long inverse(long x,long m),ME(long x,long y,long z); long BR(long s); void main() {/*main*/ clrscr(); long q1,p1,M1,x1,phi1,e1,d1,xx,xx1,xx2,xx3, p2,q2,M2,phi2,e2,d2,yy,yy1,yy2,yy3,now0,now,user_time; now0=time(NULL); p1=2699;q1=795659; M1=p1*q1; printf("p1=%ld q1=%ld M1=%ld ",p1,q1,M1); phi1=(p1-1)*(q1-1);e1=3674911; d1=inverse(e1,phi1); printf("phi1=%ld e1=%ld d1=%ld \n",phi1,e1,d1);printf(""); p2=5843;q2=367531;M2=p2*q2;phi2=(p2-1)*(q2-1); e2=3674911;printf("p2=%ld q2=%ld M2=%ld ",p2,q2,M2); d2=inverse(e2,phi2); printf("phi2=%ld e2=%ld d2=%ld \n",phi2,e2,d2);printf(""); xx=12345; xx1=ME(xx,M1,e1);xx2=ME(xx1,M1,e1);xx3=ME(xx2,M1,e1); ThiNganHang.com

Die ersten 7 Seiten werden angezeigt. Laden Sie das Dokument herunter, um alles zu sehen.

Kommentare (0)

Anmelden um an der Diskussion teilzunehmen

Noch keine Kommentare. Seien Sie der Erste!