Tải bản đầy đủ (.doc) (16 trang)

VISUAL C ++60.DOC

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 (62.28 KB, 16 trang )

Phụ lục hệ mật RSA
Phụ 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;
Phô lôc hÖ mËt RSA
}
int 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;
Phô lôc hÖ mËt RSA
x[i]-=y[i];
nho=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;
Phô lôc hÖ mËt RSA
for (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;
Phô lôc hÖ mËt RSA
nho+=thap(r);
s--;

}
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)
Phô lôc hÖ mËt RSA
{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);

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×