Tải bản đầy đủ (.pdf) (73 trang)

Bài tập môn an toàn bảo mật thông tin

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.13 MB, 73 trang )

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++)
{
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;
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>
/* 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*/

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*/

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;
}
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);
yy1=ME(xx3,M2,e2);yy2=ME(yy1,M2,e2);yy3=ME(yy2,M2,e2)
;
cprintf("\n KET QUA MA : ",yy3);
printf("xx=%ld \n\nxx1=%ld xx2=%ld xx3=%ld
\n",xx,xx1,xx2,xx3);
printf("yy1=%ld yy2=%ld yy3=%ld \n",yy1,yy2,yy3);
yy2=ME(yy3,M2,d2);yy1=ME(yy2,M2,d2);xx3=ME(yy1,M2,d2)
;
xx2=ME(xx3,M1,d1);xx1=ME(xx2,M1,d1);xx=ME(xx1,M1,d1);
/* m=c^d modNN=RO */
cprintf("\n KET QUA DICH : ",xx);
printf("xx=%ld \n\nxx1=%ld xx2=%ld xx3=%ld
\n",xx,xx1,xx2,xx3);
printf("yy1=%ld yy2=%ld yy3=%ld \n",yy1,yy2,yy3);
now=time(NULL);user_time=now-now0;
printf("\nuser_time = %d seconds
",user_time,"seconds");getchar();
}/*main*/

long x0,m0,p0;
long ME(long x0,long m0,long p0)
{
long A0,p1,Z[40];int i1;
BR(p0);N=n;for (i1=0;i1<=N;i1++) Z[i1]=Q[i1];
/*for (i1=0;i1<=N;i1++)
printf("Q[%d]=%ld \n",i1,Q[i1]);getchar();*/
A0=x0%m0;p1=1;/* A0=x1modMM B pesentation of x1*/
if(p0>0);
{
if (Z[0]>0) p1=A0;
for(i1=1;i1<=N;i1++)
{
A0=MP(A0,A0,m0);
if(Z[i1]>0) p1=MP(A0,p1,m0);

}/*i1*/
}
return (p1);
}/*phep tinh luy thua modulo*/
long s;
long BR(long s)
{
int ii;
long x,q;
ii=0;x=s;q=(x>>1);Q[ii]=x-(q<<1);
while (q>0)
{
ii++;x=q;q=(x>>1);Q[ii]=x-(q<<1);
}

n=ii;
}/*Bit-Representation,bieu dien x theo bimary*/
long x,y,k;
long MP(long x,long y,long k)
{
int ii;long P,t1,t2,Y[40];double tem;t1=x;t2=y;
if(x<0) {x=-x;x=k-(x%k);} else x=x%k;
if(y<0) {y=-y;y=k-(y%k);} else y=y%k;
if ((x==0)||(y==0)) {P=0;goto label1;}
BR(y); M=n;
for(ii=0;ii<=M;ii++) Y[ii]=Q[ii];
if(Y[0]>0) P=x; else P=0;
for(ii=1;ii<=M;ii++)
{
tem=2.0*x;
if(tem>=k) tem-=k;
x=(long)tem;
if(Y[ii]>0)
{
tem+=P;
if(tem>=k) P=(long)(tem-k);
else P=(long)tem;
}
}/*ii*/
x=t1;
y=t2;
label1:return(P);
}/*phep nhan modulo*/
long b,a;/*0<b<a,Multicative inverce of b mod a*/
long inverse(long b,long a)

{
long A,B,d,q,r,x,x1,x2,y,y1,y2,tem;
A=a;B=b; /*Dung Beazout*/
if(b==0)
{
d=a;x=1;y=0;
}/*if*/
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;
}/*while*/
d=a;x=x2;y=y2;
}/*else*/
tem=A*x+B*y-d;
if(y<0)
y=A+y;
return(y);
}/*inverse*/

Bài 8 : RSA mã chuỗi ký tự : plain text được nhập vào dưới dạng một chuỗi ký tự
#include <iostream.h>
#include <conio.h>
#include <math.h>

#include <stdlib.h>
#include <stdio.h>
#include<string.h>
#include<iomanip.h>
long MP(long long ,long ,long ); //Nhan luy thua
long inverse(long ,long ); //Modulo nghich dao
long ME(long ,long ,long ); //Luy thua modulo
void BR(long ); //Doi sang dang bit (co so 2)
int SNT(long);
int snt( long);
long Bezout (long a, long b);//tinh gcd
long Q[40];
int n,N,M;
long MP(long long x,long y,long k){ //Nhan luy thua
int ii;
long P,t1,t2,Y[40];
double tem;
t1=x; t2=y;
if(x<0){
x = -x;
x = k - (x%k);
}
else x = x%k;
if(y<0){
y = -y;
y = k - (y%k);
}
else y = y%k;
if(x==0 || y==0){
P=0;

return P;
}
BR(y); M=n;
for(ii=0;ii<=M;ii++)
Y[ii]=Q[ii];
if(Y[0]>0)
P = x;
else P = 0;
for(ii=1;ii<=M;ii++){
tem=2.0*x;
if(tem>=k)
tem -= k;
x = (long)tem;
if(Y[ii] > 0){
tem +=P;
if(tem>=k) P = (long)(tem-k);
else P = (long)tem;
}
}
x = t1;
y = t2;
return P;
}
long inverse(long b,long a){ //Modulo nghich dao
long A,q,r,x,x1,x2,y,y1,y2;
A = a;
if(b == 0){
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;
}
x = x2; y = y2;
}
if(y<0)
y = A + y;
return y;
}
long ME(long x0,long m0,long p0){
//Tính Luy thua Modulo
long A0,p1,Z[40];
int i1;
BR(p0);
N=n;
for(i1=0;i1<=N;i1++)
Z[i1] = Q[i1];
A0 = x0%m0;
p1 = 1;
if(p0>0);{
if(Z[0]>0)
p1=A0;
for(i1=1;i1<=N;i1++){
A0=MP(A0,A0,m0);

if(Z[i1]>0)
p1=MP(A0,p1,m0);
}
}
return p1;
}

void BR(long s){ //Doi sang dang bit (co so 2)
int ii;
long x,q;
ii = 0; x = s; q = (x>>1); Q[ii] = x - (q<<1);
while(q>0){
ii++; x = q;
q = (x>>1); Q[ii] = x - (q<<1);
}
n = ii;
}
int SNT(long n){
if(n == 1 || n == 2)
return 1;
int i=3;while(n%i!=0)i++;i ;
if(n == i)
return -1;
return 1;
}
void main(){
clrscr();
long
q1,p1,M1,phi1,e1,d1,xx[25]={0},xx1[25]={0},xx2[25]={0},xx3
[25]={0};

//tao ngau nhien SNT
randomize();
long arr[30]={0},z,y,x;
cout<<"\n Day so nguyen to(Ban co the chon!):";
int i=0;
for( int j=0;j<50;j++)
{
if((z=snt(random(10000)))!=0)
{
arr[i]=z;i++;
}
}
for( j=0;j<i;j++)
cout<<setw(10)<<arr[j];
cout<<"\nNHAP CAP SO NGUYEN TO THU NHAT:";
do{
cout<<"\nNhap so nguyen to thu nhat p1 = ";
cin>>p1;
}while(SNT(p1)<0);
do{
cout<<"\nNhap so nguyen to thu hai q1 = ";
cin>>q1;
}while(SNT(q1)<0);
M1 = p1*q1;
cout<<"p1 = "<<p1<<" q1 = "<<q1<<" M1 =
"<<M1<<endl;
phi1 = (p1-1)*(q1-1);
do{
cout<<"\n nhap vao so e1:";cin>>e1;
}while(Bezout(e1,phi1)!=1);

d1=inverse(e1,phi1);
cout<<" phi1 = "<<phi1<<" e1 = "<<e1<<" d1 =
"<<d1;
char a[25];
cout<<"\nNHAP VAN BAN CAN MA HOA: "; gets(a);

int n= strlen(a);
cout<<"chieu dai chuoi = "<<n<<endl;
cout<<"\nVAN BAN CAN MA HOA "<<a;
for(int ii=0;ii<n;ii++)
{
if((a[ii]!=' '))
{
xx[ii]= (a[ii] - '0');
}
else
xx[ii]=a[ii];
}
cout<<"\n chuoi sau khi doi thanh so: "<<xx;
for( ii=0;ii<n;ii++)
xx1[ii]=ME(xx[ii],M1,e1);
for( ii=0;ii<n;ii++)
xx2[ii]=ME(xx1[ii],M1,e1);
for( ii=0;ii<n;ii++)
xx3[ii]=ME(xx2[ii],M1,e1);
cout<<"\nKET QUA MA (van ban ma):"<<xx3<<endl;
getch();
//giai ma
for( ii=0;ii<n;ii++)
xx2[ii]=ME(xx3[ii],M1,d1);

for( ii=0;ii<n;ii++)
xx1[ii]=ME(xx2[ii],M1,d1);
for( ii=0;ii<n;ii++)
xx[ii]=ME(xx1[ii],M1,d1);
// cout<<"\n Sau giai ma (chua doi thanh ky tu):"<<xx;
for(ii=0;ii<n;ii++)
{
if(xx[ii]!=32)
a[ii]= char(xx[ii] + '0');
else
a[ii]=' ';
}
cout<<"\nKET QUA DICH (BANG RO): "<<a;
getch();
}
long Bezout (long a, long b)
{
long d,q,r,x,x1,x2,y,y1,y2;
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;
}
return d;

}
int snt(long n)
{
for(int i=2;i<n;i++)
{
if((n%i)==0)
{ n=0;
break;
} }
return n;
}

Bài 9: Mã hóa một file text (thu1.txt) dùng RSA
/* Mã một file text dùng RSA */
#include <stdio.h>
#include <math.h>
#include <conio.h>
#define I 4
#include<time.h>
long Q[40];
FILE *fp1;
int n,N,M;
long q1,p1,M1,x1,phi1,e1,d1,xx,xx1,xx2,xx3,
p2,q2,M2,phi2,e2,d2,yy,yy1,yy2,yy3;
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);/*long codage(long x,long y,long z,long
t,long v);*/
void main()

{/*main*/
long now0,now,user_time,i2=0;
clrscr();
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("");
fp1=fopen("a:\thu1.txt","rb");
printf("KET QUA MA : \n");
while(fread(&xx,sizeof(xx),1,fp1)==1)
{
/*codage(xx,M1,e1,M2,e2);*/
xx1=ME(xx,M1,e1);
xx2=ME(xx1,M1,e1);xx3=ME(xx2,M1,e1);
yy1=ME(xx3,M2,e2);
yy2=ME(yy1,M2,e2);yy3=ME(yy2,M2,e2);
i2++;printf("%lx ",yy3);
if(i2%7==0)printf("");
}
fclose(fp1);


now=time(NULL);user_time=now-now0;
printf("\n user_time = %d seconds
",user_time,"seconds");getchar();
}/*main*/
long x0,m0,p0;
long ME(long x0,long m0,long p0)
{
long A0,p1,Z[40];int i1;
BR(p0);N=n;for (i1=0;i1<=N;i1++) Z[i1]=Q[i1];
/*for (i1=0;i1<=N;i1++)
printf("Q[%d]=%ld \n",i1,Q[i1]);getchar();*/
A0=x0%m0;p1=1;/*printf ("A0=%ld N=%d ",A0,N);getchar();/*
A0=x1modMM B pesentation of x1*/
if(p0>0);
{
if (Z[0]>0) p1=A0;
for(i1=1;i1<=N;i1++)
{
A0=MP(A0,A0,m0);
if(Z[i1]>0) p1=MP(A0,p1,m0);
/*printf("i1=%d A0=%ld p1=%ld
\n",i1,A0,p1);*/
}/*i1*/
}/* printf("p1=%ld ",p1); getchar();*/
return (p1);
}/*phep tinh luy thua modulo*/
long s;
long BR(long s)
{

int ii;
long x,q;
ii=0;x=s;q=(x>>1);Q[ii]=x-(q<<1);
while (q>0)
{
ii++;x=q;q=(x>>1);Q[ii]=x-(q<<1);
}
n=ii;
}/*Bit-Representation,bieu dien bit*/
long x,y,k;
long MP(long x,long y,long k)
{
int ii;
long P,t1,t2,Y[40];
double tem;
t1=x;
t2=y;
if(x<0) {x=-x;x=k-(x%k);
} else x=x%k;
if(y<0) {y=-y;y=k-(y%k);
} else y=y%k;
if ((x==0)||(y==0))
{
P=0;
goto label1;
}
BR(y); M=n;
for(ii=0;ii<=M;ii++) Y[ii]=Q[ii];
if(Y[0]>0) P=x; else P=0;
for(ii=1;ii<=M;ii++)

{
tem=2.0*x;
if(tem>=k) tem-=k;
x=(long)tem;
if(Y[ii]>0)
{
tem+=P;
if(tem>=k) P=(long)(tem-k);
else P=(long)tem;
}
}/*ii*/
x=t1;
y=t2;
label1:
return(P);
}/*phep nhan modulo*/

long b,a;/*0<b<a,Multicative inverse of b mod a*/
long inverse(long b,long a)
{
long A,B,d,q,r,x,x1,x2,y,y1,y2,tem;
A=a;B=b;
if(b==0)
{
d=a;x=1;y=0;
}/*if*/
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;
}/*while*/
d=a;x=x2;y=y2;
}/*else*/
tem=A*x+B*y-d;
if(y<0)
y=A+y;
return(y);
}/*inverse*/
/* long codage(long x, long y ,long z,long t,long
v)
{

}/*codage*/

BÀI 10 :Bài tổng hợp :CHƯƠNG TRÌNH HỆ THỐNG BẢO MẬT – XÁC
THỰC DÙNG RSA
Tạo menu chương trình

Bấm (K) –tạo khóa :
Menu tạo khóa :
1.Tạo bảng SNT ( hoặc đưa SNT từ BF song phải kiểm tra số NT)
2. Chọn p&q trong bảng
3. Tạo khóa :
Chương trình tự động tính N=p*q

Chương trình tính Fi = (p-1)*(q-1)
Chọn công việc:
a. Tạo khóa (K)
b. Xác thực (A)
c. Bảo mật (C)
d. Thoát ra ngoài HT (X)
Yêu cầu chọn e (nhập e từ KB)
Chương trình K/tra e có NTCN với Fi
Nếu e không là số NT cùng nhau với Finhập lại e
Chương trình tính tiếp d = e
ø
mod N (Dùng Beazout)
Xuất cặp khóa (e,N) và (d,N)
Ví dụ p=3,q=5;p*q=15 ; Fi=(p-1)*(q-1)=8
Chọn e = 3 d =3
-1
mod8 = 3
Xuất (3,15) và (3,15)
Thoát về main menu
Bấm A ( Authenticate)
1.Yêu cầu nhập văn bản gốc cần xác thực
- Nhập từ KB số nguyên m
Trong thực tế : Nhập text  chuyển khối dữ liệu thành số nguyên
- Nhập khóa từ bảng khóa (d,N) (của tôi)
- Mã xác thực : c = m
d
mod N  gửi đi
- Bên nhận (cần xác thực) dùng Public Key của “tôi” để giải > giả được “tôi”
đúng là tôi.
Bấm C ( bảo mật thông điệp).

Nhập thông điệp cần bảo mật “m” từ KB
Lấy (e,N) của bên nhận thông điệp từ bang khóa hoặc PK server
Mã bằng (e,N) của bên nhận : c= m
e
modN gửi c đi.
Bên nhận dùng (d,N) của mình để giải mật. Nếu giải được  OK!
Thoát ra menu chính
Hỏi có tiếp tục K
o
???
Yes ! Trở về menu chính
No ! Bye bye!
Thoát ra HĐH
BÀI TẬP THỰC HÀNH AN TOÀN & BẢO MẬT THÔNG TIN
PHẦN 2 : AN TOÀN VÀ BẢO MẬT HỆ THỐNG (12 tiết)

Bài 1 : Sao lưu dự phòng dữ liệu
 B 1: Khởi động máy vào log on Admin , vào thư mục C: tạo thư mục
(Dulieu) và các file h1.txt , h2.txt ; sau đó vào D: tạo thư mục Backup
 B2: Start → Programs →Accessories → Systems Tools → Backup →tại
cửa sổ Welcome →bỏ dấu chọn ô Always start in wizard mode → Click
chọn Advance Mode → tại cửa sổ Backup Utility chọn tab Backup →
click vào dấu “+” tại C: và đánh dấu chọn vào ô Dulieu → tại cửa sổ
Backup media or file name chọn tab Browse … → chỉ đường dẫn tới
D:\Backup và lưu file backup với tên (bk1.bkf). Sau đó chọn Start
Backup →tại cửa sổ Backup Job Information chọn Start Backup. Sau khi
Backup Progress xong → Vào D:\Backup để kiểm tra xem có file
“bk1.bkf” chưa ?
 B3 : vào C:\Dulieu , click nút phải chuột trên h1.txt → Properities → tab
General → chọn Advance →bỏ dấu chọn File is ready for archiving

 B4 : Mở file h1.txt nhập thêm vào nội dung và lưu lại →click chuột phải
trên h1.txt → Properities → tab General → chọn Advanced … → bỏ dấu
chọn File is ready for archiving
 B5 : Mở chương trình Backup, tại cửa sổ Backup Utility chọn tab Backup
→Click dấu ‘+’ tại C: và đánh dấu chọn vào ô dữ liệu →tại mục Backup
media or file name gõ “D:\Backup\dif.bkf” →chọn Start Backup →tại
cửa sổ Backup Job Information chọn Advance … →tại cửa sổ Advanced
Backup Options , tại mục Backup Type chọn Differential → OK →Start
Backup
 B6 : tại cửa sổ Backup Utility chọn tab Restore and Manage Media và
click dấu ‘+’ tại mục dif.bkf →Click dấu ‘+’ tại C: và chọn Dulieu (chỉ
có h1.txt được backup). Sau đó đóng các cửa sổ , kế tiếp vào thư mục
C:\Dulieu → Click chuột phải trên h1.txt →Properities →tab General →
Advanced … (đánh dấu chọn mục File is ready for archiving
 B7 : Mở file h2.txt nhập thêm nội dung và save lại →Click chuột phải
trên h2.txt → Properities →tab General → Advanced … (đánh dấu chọn
mục File is ready for archiving
 B8: Mở chương trình Backup →chọn tab Backup , sau đó click dấu ‘+’
tại C: , đánh dấu chọn vào ô Dulieu, tại mục Backup media or file name
gõ D:\Backup\inc.bkf , sau đó chọn Start Backup
 B9: tại cửa sổ Backup Job Information chọn Advanced … → tại cửa sổ
Advanced Backup Options , click vào mục Backup Type và chọn
Incremental →OK →Start Backup.
 B10: tại cửa sổ Backup Utility chọn tab Restore and Manage Media ,
click dấu ‘+’ tại mục inc.bkf, kế tiếp click dấu ‘+’ tại mục C:\Dulieu

BÀI 2 : RESTORE DATA
 B1 : Xóa thư mục Dulieu
 B2: Mở chương trình Backup →chọn Menu Tools →Options →tab
Restore → đánh dấu chọn vào mục Replace the file on disk only if the

file on disk is older
 B3: tại cửa sổ Backup Utility →vào tab Restore and Manage Media →
click vao file →bk1.bkf → C: và đánh dấu chọn vào Dulieu → Chọn
Start Restore → tại cửa sổ confirm restore chọn OK.

BÀI 3 : Ẩn Control Panel
 B1 : Vào Start → Run →MMC → OK
 B2 : Sẽ xuất hiện màn hình Console1, chọn File →Add/Remove Snap-in
→Add →tại cửa sổ Add Standalone Snap-in →Group Policy Object
Editor →Add →Finish.
 B3 : Tại màn hình Console1, chọn File →Save as (tại cửa sổ save in chọn
Destop và File name : Local Policy)
 B4 : Vào Local Policy →Local Computer Policy → User Configuration
→ Administrator Templates →Control Panel
 B5 : Vào Prohibit access to the Control Panel →Properties (đánh dấu
chọn Enable , sau đó apply và OK)
 B6 : Vào Start → Run →cmd →gpupdate /force



BÀI 4 : Ghi nhận quá trình đăng nhập (Logon)
 B1 : Start →Programs →Administrative Tools →Local Security Policy
→Local Policies → Audit Policy → Click chuột phải lên Audit Account
Logon Events → Properties → đánh dấu chọn Failure → Apply → OK
→cập nhật policy (gpupdate \force)
 B2 : Start →Programs →Administrative Tools →Event viewer → click
chuột phải lên Security → chọn Clear all events → chọn No.

BÀI 5 : NETWORK MONITORING


Chọn 2 máy tính : máy tính 1 có địa chỉ IP :192.168.1.1 và máy tính 2 có địa
chỉ IP : 192.168.1.2.
 B1: Start → Settings →Control Panel → Add/Remove Programs →
Add/Remove Windows Component →chọn mục Management and
Monitoring Tools →Details →đánh dấu chọn vào ô Network
MonitorTools →OK → Next.
 B2 : Start →Administrative Tools →Network Monitor →tại cửa sổ
Microsoft Network Monitor, chọn OK →tại cửa sổ Select a network,
click dấu ‘+’ ở mục Local Computer → chọn card LAN → OK
 B3 : Tại cửa sổ Network Monitor → chọn Capture →Start. (Chú ý để
nguyên màn hình Network Monitor)
 B4 : Start →Run →gõ cmd →ta dùng lệnh ping địa chỉ IP giữa 2 máy
tính : ping 192.168.1.1 và ping 192.168.1.2.
 B5 : Quay lại màn hình Network Monitor → chọn Capture →Stop and
View → double click trên dòng có protocol là ICMP


BÀI 6 : IP SECURITY (IP SEC)

Chọn 2 máy tính : máy tính 1 có địa chỉ IP : 192.168.1.1 và máy tính 2 có địa
chỉ IP : 192.168.1.2.
 B1 : Start →Run →gõ MMC →tại cửa sổ Consol →chọn Menu File →
Add/Remove Snap-in →tại cửa sổ Add/Remove Snap-in , tại mục Snap in
add to : Consol Root, sau đó chọn Add →Kéo thanh trượt chọn mục IP
Security Policy Management →chọn Add → tại cửa sổ Select Computer or
Domain , chọn ô Local Computer →Finish →Close →OK
 B2 : Tại cửa sổ Consol1 →click chuột phải trên IP Security Policies on
Local Computer →chọn Create IP Security Policy→tại cửa sổ Welcome
chọn Next → tại cửa sổ IP Security Policy Name gõ ‘IPSec bang Preshare
Key’ vào ô name →Next → tại cửa sổ Request for Secure Communication,

bỏ dấu chọn tại mục Activate the default → Next → Finish →tại cửa sổ Test
IP Sec Preshare Key Properties → chọn Add →tại cửa sổ Welcome → Next
→ tại cửa sổ Tunnel Endpoint chọn This rule does not specify a tunnel →
Next → tại cửa sổ Network Type , chọn ô Local area network LAN
→Next→tại cửa sổ IP Filter List, chọn mục All IP →Next →tại cửa sổ
Filter Action , chọn mục Require Security → Next →trong cửa sổ
Authentication Method, chọn mục Use this string to protect the key
exchange, trong hộp thoại gõ ‘123’ →Next →Finish.
 B3 : Tại cửa sổ Consol1, click chuột phải lên IPSec bang Preshare Key →
Assign →lưu Consol1 ra màn hình Desktop →đóng các cửa sổ đang có và
cập nhật Policy(gpupdate /force)
 B4 : Start →Progarms →Administrative Tools →Services →click chuột
phải lên IPSEC Service, chọn Restart.
 B5: Mở chương trình Network Monitor →chọn Capture →Start

×