VISUAL C ++60
Trang 1Phụ lục
Trong phần này tôi xin giới thiệu một số thủ tục chính đợc viết trong môi tr-ờng Visual C++6.0 Nó gồm hai tệp chính là Dll.cpp ( chứa các hàm và thủ tục) và Dll.def (các hàm xuất khẩu):
File Dll.cpp
// Chơng trình mã hoá DL bằng RSA
// Dịch file này ra file Dll.dll
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#include <malloc.h>
#include <stdio.h>
#include <conio.h>
#include <stddef.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define C_max 200
#define M16 65535
#define Mmax 65536
#define thap(x) ((x)&M16)
#define cao(x) ((x)>>16)
typedef unsigned short WORD;
typedef unsigned long LONG1;
typedef WORD So_Lon[C_max];
WORD C=23,KEP[2*C_max];
LONG1 dau_mod;
So_Lon khoama,khoagiai,MOD;
int tongso;
int far pascal LibMain(HANDLE hIndtance,WORD wDataseg,WORD wHeapSize,LPSTR lpCmdline)
{
if (wHeapSize!=0)
return 1;
else
return 0;
}
int far pascal WED(int nParameter)
{
return 1;
Trang 2int do_dai_SL(So_Lon x)
{int i=C-1;
while ((x[i]==0)&&(i>0)) i ;
return i;
}
int be_hon_SL(So_Lon x,So_Lon y)
{int i=C-1;
while (x[i]==y[i] && (i>0)) i ;
return (x[i]<y[i]);
}
//KiÓm tra xem hai sè lín b»ng nhau
int bang_nhau_SL(So_Lon x,So_Lon y)
{return (!be_hon_SL(x,y)&&!be_hon_SL(y,x));
}
// Thñ tcô céng hai sè lín
void cong_SL(So_Lon x, So_Lon y)
{int i,nho=0,d=do_dai_SL(x),c=do_dai_SL(y);
if (c>d) d=c;
for (i=0;i<=d;i++)
{x[i]+=y[i];
x[i]+=nho;
if (x[i]<y[i]) nho=1;
else
if (x[i]>y[i]) nho=0;
}
x[d+1]=nho;
}
//Thñ tôc trõ hai sè lín
void tru_SL(So_Lon x, So_Lon y)
{int i,nho=0,d=do_dai_SL(x);
for (i=0;i<=d;i++)
{if (x[i]>y[i])
{x[i]-=y[i];
x[i]-=nho;
nho=0;
}
else
{if (x[i]==y[i]) x[i]=0-nho;
else
{x[i]-=nho;
Trang 3nho=1;
}
}
}
}
// Thñ tôc nh©n hai WORD kiÓu SL
void nhan_word_SL(So_Lon x,So_Lon y,WORD k)
{int i;
LONG1 t,nho=0;
if (k==0)
{memset(y,0,2*C);return;}
if (k==1)
{memcpy(y,x,2*C);return;}
memset(y,0,2*C);
int d=do_dai_SL(x);
for (i=0;i<=d;i++)
{t=x[i];
t*=k;
nho+=t;
y[i]=(WORD) nho;
nho>>=16;
}
y[d+1]=(WORD) nho;
}
// Thñ Tôc nh©n hai sè lín
void nhan_SL(So_Lon x,So_Lon y)
{int i,j,k,d=do_dai_SL(x);
LONG1 r,t1=0,nho=0;
memset(KEP,0,4*C);
for (i=0;i<=d;i++)
{j=i;
for (k=0;k<=i;k++)
{r=(LONG1) x[k];
r*=y[j ];
t1+=cao(r);
nho+=thap(r);
}
KEP[i]=(WORD) nho;
nho>>=16;
nho+=t1;
t1=0;
}
for (i=d+1;i<C;i++)
{j=i;
Trang 4for (k=0;k<=d;k++)
{r=(LONG1) x[k];
r*=y[j ];
t1+=cao(r);
nho+=thap(r);
}
KEP[i]=(WORD) nho;
nho>>=16;
nho+=t1;
t1=0;
}
for (i=C;i<=d+C-1;i++)
{j=i-C+1;
int s=C-1;
for (k=j;k<=d;k++)
{r=(LONG1) x[k];
r*=y[s ];
t1+=cao(r);
nho+=thap(r);
}
KEP[i]=(WORD) nho;
nho>>=16;
nho+=t1;
t1=0;
}
while (nho)
{KEP[i++]=(WORD) nho;
nho>>=16;
}
}
//Thñ tôc b×nh ph¬ng SL
void binh_phuong_SL(So_Lon x)
{int i,j,k,d,s;
LONG1 r,t1=0,nho=0;
memset(KEP,0,4*C);
nho=(LONG1) x[0];
nho*=x[0];
KEP[0]=(WORD) nho;
nho>>=16;
d=do_dai_SL(x);
for (i=1;i<=d;i++)
{s=i>>1;
if ((i&1)==0)
{r=(LONG1) x[s];
r*=x[s];
t1+=r>>16;
Trang 5s ;
}
j=i;
for (k=0;k<=s;k++)
{r=(LONG1) x[k];
r*=x[j ];
t1+=(r>>16)<<1;
nho+=(thap(r)<<1);
}
KEP[i]=(WORD) nho;
nho>>=16;
nho+=t1;
t1=0;
}
for (i=d+1;i<=d*2;i++)
{s=i>>1;
if ((i&1)==0)
{r=(LONG1) x[s];
r*=x[s];
nho+=thap(r);
t1+=cao(r);
s ;
}
j=d;
for (k=i-d;k<=s;k++)
{r=(LONG1) x[k];
r*=x[j ];
t1+=(r>>16)<<1;
nho+=thap(r)<<1;
}
KEP[i]=(WORD) nho;
nho>>=16;
nho+=t1;
t1=0;
}
while (nho)
{KEP[i++]=(WORD) nho;
nho>>=16;
}
}
// Thñ tôc MODULO SL
void modulo_SL(So_Lon r)
{int d=2*C-1;
while ((KEP[d]==0)&&(d>0)) d ;
if (d<C-3)
Trang 6{memcpy(r,KEP,2*C);
return;
}
double dd;
LONG1 m;So_Lon z;
WORD q;
for (int i=d;i>=C-3;i )
{int j=i-C+3;
m=KEP[i+1];
m<<=16;
m+=KEP[i];
dd=(double) m;
dd*=Mmax;
dd+=KEP[i-1];
dd/=dau_mod;
if ((LONG1)dd==Mmax) q=M16;else q=(WORD) dd;
if (q)
{nhan_word_SL(MOD,z,q);
WORD nho=0;
for (int k=0;k<C-1;k++)
{if ((KEP+j)[k]>z[k])
{(KEP+j)[k]-=z[k];
(KEP+j)[k]-=nho;
nho=0;
}
else
{if ((KEP+j)[k]==z[k]) (KEP+j)[k]=0-nho;
else
{(KEP+j)[k]-=nho;
(KEP+j)[k]-=z[k];
nho=1;
}
}
}
if (nho)
{q-=1;
nho=0;
for (k=0;k<C-1;k++)
{(KEP+j)[k]+=MOD[k];
(KEP+j)[k]+=nho;
if ((KEP+j)[k]<MOD[k]) nho=1;
else if ((KEP+j)[k]>MOD[k]) nho=0;
}
}
}
}
memcpy(r,KEP,2*C);
Trang 7}
// Thñ tôc Nh©n Modulo SL
void nhan_SL_mod(So_Lon x,So_Lon y)
{nhan_SL(x,y);
modulo_SL(x);
}
// Thñ tôc b×nh ph¬ng SL
void binh_phuong_SL_mod(So_Lon x)
{binh_phuong_SL(x);
modulo_SL(x);
}
// Thñ tôc Lòy thõa SL
void luy_thua_SL(So_Lon x,So_Lon z)
{int b,i,j,dem=0,dk=0,d=do_dai_SL(z);
So_Lon y,X[32];
dau_mod=MOD[C-3];
dau_mod*=Mmax;
dau_mod^=MOD[C-4];
memcpy(X[0],x,2*C);
for (i=0;i<5;i++) binh_phuong_SL_mod(X[0]);
for (i=1;i<32;i++)
{memcpy(X[i],X[i-1],2*C);
nhan_SL_mod(X[i],x);
}
memset(y,0,2*C);
y[0]=1;
for (i=d;i>=0;i )
{for (j=15;j>=0;j )
{binh_phuong_SL_mod(y);
b=(z[i]>>j)&1;
dem<<=1;
dem+=b;
if (dk)
{if (dem>=32)
{nhan_SL_mod(y,X[dem&31]);
dem=0;
dk=0;
}
}
else
{dem=b;
dk=b;
}
Trang 8}
}
if (dk)
{if (dem>=32) nhan_SL_mod(y,X[dem&31]);
else
while (dem)
{nhan_SL_mod(y,x);
dem ;
}
}
memcpy(x,y,2*C);
}
// Thủ tục tạo số Lớn ngẫu nhiên
void tao_random_SL(So_Lon x)
{memset(x,0,2*C);
for (int i=0;i<C-3;i++)
{
x[i]= (rand()%256);
x[i]<<=8;
x[i] += (rand()%256);
}
}
// Thủ tục Kiểm tra xem ngời giải mã có đợc phép giải mã File
DL hay không
int far pascal XacThucChukiDTNhan(char far *FileMa,char far *FileMatNhan)
{
FILE *fma,*fmat;
So_Lon chukiDT,KT;
fma=fopen(FileMa,"rb+");
fmat=fopen(FileMatNhan,"rb+");
if (fma==NULL)
{
fcloseall();
return -2;
}
if (fmat==NULL)
{
fcloseall();
return -3;
}
fseek(fmat,0,SEEK_SET);
Trang 9fread(&chukiDT,46,1,fmat);
fseek(fma,46,SEEK_SET);
fread(&KT,46,1,fma);
if (bang_nhau_SL(chukiDT,KT))
{
fcloseall();
return 1;
}
fcloseall();
return -1;
}
//Kiem tra thong tin chu ki cua nguoi gui( xem ai gui)
int far pascal ThongTinNguoiGui(char far *FileMa,char far
*FileKhoaChung )
{
FILE *fma,*fkhoachung;
int j;//
So_Lon chukiDT,nguoigui;
long len;
int handle;
handle=open(FileKhoaChung,1);
len=filelength(handle);
tongso=len/92;
fma=fopen(FileMa,"rb+");
fkhoachung=fopen(FileKhoaChung,"rb+");
if (fma==NULL)
{
fcloseall();
return -1;
}
if (fkhoachung==NULL)
{
fcloseall();
return -2;
}
fseek(fma,92,SEEK_SET);
fread(&nguoigui,46,1,fma);
for (j=0;j<tongso;j++)
{
fseek(fkhoachung,92*j ,SEEK_SET);
fread(&chukiDT,46,1,fkhoachung);
if (bang_nhau_SL(chukiDT,nguoigui))
{
Trang 10fcloseall();
return j;
break ;
}
}
fcloseall();
}
//Thu tuc ma hoa mam khoa va thong tin nguoi nhan, thong tin nguoi gui
int far pascal MaKhoaRSA(char far *FileKhoaChung, char far
*FileOut, char far *FileNguoiGui, int far NguoiNhan)
{
FILE *fkhoachung,*fout,*fgui;
So_Lon Ek,k1,chukigui;
fkhoachung=fopen(FileKhoaChung,"rb+");
fgui=fopen(FileNguoiGui,"rb+");
fout=fopen(FileOut,"wb+");
if (fkhoachung==NULL)
{
fcloseall();
return -1;
}
if (fout==NULL)
{
fcloseall();
return -2;
}
if (fgui==NULL)
{
fcloseall();
return -3;
}
{
tao_random_SL(k1);
memcpy(khoama,k1,46);
}
// ma khoa vong mot
rewind(fkhoachung);
fseek(fkhoachung,92*NguoiNhan,SEEK_SET);
fread(&MOD,46,1,fkhoachung);
fread(&Ek,46,1,fkhoachung);
luy_thua_SL(k1,Ek);
fwrite(&k1,46,1,fout);
fwrite(&MOD,46,1,fout);
// thong tin ve chu ki gui
Trang 11rewind(fgui);
fread(&chukigui,46,1,fgui);
fwrite(&chukigui,46,1,fout);
fcloseall();
return 1;
}
// Thñ tôc Gi¶i m· kho¸ RSA
int far pascal GiaiKhoaRSA( char far *FileKhoaMat, char far *FileMa)
{
FILE *fkhoamat,*fma;
So_Lon Dr,mam;
fkhoamat=fopen(FileKhoaMat,"rb+");
fma=fopen(FileMa,"rb+");
if (fkhoamat==NULL)
{
fcloseall();
return -1;
}
if (fma==NULL)
{
fcloseall();
return -2;
}
// Doc mam khoa
fseek(fma,0,SEEK_SET);
fread(&mam,46,1,fma);
//Doc khoa mat
rewind(fkhoamat);
fread(&MOD,46,1,fkhoamat);
fread(&Dr,46,1,fkhoamat);
// Lay lai khoa ban dau
luy_thua_SL(mam,Dr);
memcpy(khoagiai,mam,46);
fcloseall();
return 1;
}
// Thñ tôc M· File DL
int far pascal MaFile( char far *FileRo, char far *FileMa) {
FILE *fin,*fout;
WORD ra[40],ta[40];
long len,t;
int handle,l;
Trang 12handle=open(FileRo,1);
len=filelength(handle);
// cho biet tong so cac user
tongso=len/92;
fout=fopen(FileMa,"a+b+");
fin= fopen(FileRo,"rb+");
if (fin==NULL)
{
fcloseall();
return -1;
}
if (fout==NULL)
{
fcloseall();
return -2;
}
for (t=0;t<(len/40);t++)
{
fseek(fin,40*t,SEEK_SET);
fread(&ta,40,1,fin);
for (l=0;l<40;l++)
{
ra[l]=ta[l]^khoama[l] ; }
fwrite(&ra,40,1,fout);
//Ma hoa du lieu phan con lai
}
fseek(fin,t*40,SEEK_SET);
if(len %40)
{
fread(&ta,len%40,1,fin);
for (l=0;l<(len%40);l++) {
ra[l]=ta[l]^khoama[l] ; }
fwrite(&ra,(len%40),1,fout);
}
fcloseall();
return 1;
}
// Thñ tôc dÞch M· File DL
int far pascal GiaiMaFile(char far *FileMa, char far
*FileRo)
{
FILE *fin,*fout;
Trang 13long len,t;
LONG filemadu;
WORD tam[40],ra[40];
int handle,l;
handle=open(FileMa,1);
len=filelength(handle);
filemadu=(len-138)%40;
fout=fopen(FileRo,"wb+");
fin=fopen(FileMa,"rb+");
if (fin==NULL)
{
fcloseall();
return -1;
}
if (fout==NULL)
{
fcloseall();
return -2;
}
for (t=0;t<(len-138)/40;t++)
{
fseek(fin,138+t*40,SEEK_SET);
fread(&tam,40,1,fin);
for (l=0; l<40;l++)
{
ra[l]= tam[l] ^ khoagiai[l];
}
fwrite(&ra,40,1,fout);
}
//giai ma phan con lai
if(filemadu)
{
fseek(fin,138+t*40,SEEK_SET);
fread(&tam,filemadu,1,fin);
for (l=0;l<filemadu;l++) {
ra[l]=tam[l]^khoagiai[l] ; }
fwrite(&ra,filemadu,1,fout);
}
fcloseall();
return 1;
}
// Thu tuc tao khoa cho nguoi su dung moi
Trang 14int far pascal TaoUser(char far *FileKhoaChung,char far
*FileKhoaMat, char far *FileUser, int STT)
{
So_Lon k,E,D,khoa;
FILE *fkhoachung,*fKmat,*fuser;
fuser=fopen(FileUser,"wb+");
fkhoachung=fopen(FileKhoaChung,"rb+");
fKmat=fopen(FileKhoaMat,"rb+");
if (fkhoachung==NULL)
{
fcloseall();
return -2;
}
if (fuser==NULL)
{
fcloseall();
return -3;
}
if (fuser==NULL)
{
fcloseall();
return -4;
}
fseek(fkhoachung,92*STT,SEEK_SET);
fseek(fKmat,92*STT,SEEK_SET);
fread(MOD,46,1,fkhoachung);
fread(E,46,1,fkhoachung);
fread(MOD,46,1,fKmat);
fread(D,46,1,fKmat);
tao_random_SL(k);
memcpy(khoa,k,46);
luy_thua_SL(k,E);
luy_thua_SL(k,D);
if (bang_nhau_SL(k,khoa))
{
fwrite(&MOD,46,1,fuser);
fwrite(&D,46,1,fuser);
fcloseall();
}
else
{
return -1;
}
}
Trang 15// Thñ tôc kiÓm tra ID cña ngêi sö dông
int far pascal QuyenTruyNhap(char far *FileKhoa, char far
*FileKhoaChung)
{
FILE *fk,*fCK;
So_Lon ID,IDCK;
int i;
long len;
int handle;
handle=open(FileKhoaChung,1);
len=filelength(handle);
tongso=len/92;
if ((fk=fopen(FileKhoa,"rb+"))==NULL)
{
fcloseall();
return -2;
}
if ((fCK=fopen(FileKhoaChung,"rb+"))==NULL)
{
fcloseall();
return -3;
} rewind(fk);
fread(&ID,46,1,fk);
for(i=0;i<tongso;i++)
{
fseek(fCK,92*i,SEEK_SET);
fread(&IDCK,46,1,fCK);
if (bang_nhau_SL(IDCK,ID))
{
fcloseall();
return i;
break ; }
}
return -1;
}
Trang 16File Dll.def :
LIBRARY DLL.DLL
DESCRIPTION 'DLL.DLL'
HEAPSIZE 4096
EXPORTS
MaKhoaRSA
GiaiKhoaRSA
MaFile
GiaiMaFile
ThongTinNguoiGui
XacThucChukiDTNhan
TaoUser
QuyenTruyNhap