最近在看des,便试这用sdk编了个des加密程序,希望各位能够多多指点,谢谢大家了
#include "stdafx.h"
#include <commdlg.h>
#include <math.h>
static OPENFILENAME ofn;
static HANDLE hFile1,hFile2;
char Key[16][48];
char PassWord[8];
BOOL InitKey();
BOOL IPChange(TCHAR *Source,char *L,char *R);
BOOL _IPChange(TCHAR *Dest,char *L,char *R);
BOOL CalcLR(char *L,char *R,BOOL bCodeFlag);
BOOL GetBit(TCHAR *szSource,char *Array);
BOOL f(TCHAR *L,TCHAR *R,int nCount);
BOOL SBox(char *E);
BOOL InitOFN(HWND hWnd) /*初始化OFN*/
{
ofn.Flags =0;
ofn.hInstance =NULL;
ofn.hwndOwner =hWnd;
ofn.lCustData =0;
ofn.lpfnHook =0;
ofn.lpstrCustomFilter =NULL;
ofn.lpstrDefExt ="*";
ofn.lpstrFile =NULL;
ofn.lpstrFileTitle =NULL;
ofn.lpstrFilter =TEXT("所有文件(*.*)\0*.*\0\0");
ofn.lpstrInitialDir =NULL;
ofn.lpstrTitle =NULL;
ofn.lpTemplateName =NULL;
ofn.lStructSize =sizeof(OPENFILENAME);
ofn.nFileExtension =0;
ofn.nFileOffset =0;
ofn.nFilterIndex =0;
ofn.nMaxCustFilter =0;
ofn.nMaxFile =MAX_PATH;
ofn.nMaxFileTitle=MAX_PATH;
return 1;
}
BOOL OpenFileDialog(TCHAR *szFileName) /*打开“打开”通用对话框*/
{
szFileName[0]='\0';
ofn.Flags =OFN_HIDEREADONLY;
ofn.lpstrFile =szFileName;
ofn.lpstrFileTitle =szFileName;
return GetOpenFileName(&ofn);
}
BOOL Des(TCHAR *szSourceFileName,TCHAR *szDestFileName,BOOL bCodeFlag) /*des加/解密主程序*/
{
DWORD dwBytesWritten;
LONG lFileLength;
static TCHAR Source[8],Dest[8],L[32],R[32];
if (INVALID_HANDLE_VALUE==(hFile1=CreateFile(szSourceFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL)))
{
MessageBox(NULL,"无法打开源文件!",NULL,NULL);
return 0;
}
if (INVALID_HANDLE_VALUE==(hFile2=CreateFile(szDestFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL)))
{
MessageBox(NULL,"无法创建目标文件!",NULL,NULL);
CloseHandle(hFile1);
return 0;
}
if (!InitKey()) /*初始化密钥*/
return 0;
lFileLength=GetFileSize(hFile1,0);
while (lFileLength)
{
if (lFileLength<8) /*一次读8个字节,如果不够8个就不加密*/
{
ReadFile(hFile1,Source,lFileLength,&dwBytesWritten,NULL);
WriteFile(hFile2,Source,lFileLength,&dwBytesWritten,NULL);
if (dwBytesWritten!=(DWORD) lFileLength)
{
CloseHandle(hFile1);
CloseHandle(hFile2);
MessageBox(NULL,"写入错误!",NULL,MB_OK);
return 0;
}
break;
}
ReadFile(hFile1,Source,8,&dwBytesWritten,NULL);
IPChange(Source,L,R); /*IP置换*/
CalcLR(L,R,bCodeFlag); /*L与R进行16次迭代*/
_IPChange(Dest,L,R); /*IP_1置换*/
WriteFile(hFile2,Dest,8,&dwBytesWritten,NULL);
if (dwBytesWritten!=8)
{
CloseHandle(hFile1);
CloseHandle(hFile2);
MessageBox(NULL,"写入错误!",NULL,MB_OK);
return 0;
}
lFileLength-=8;
}
CloseHandle(hFile1);
CloseHandle(hFile2);
return 1;
}
BOOL InitKey() /*初始化密钥*/
{
int pc1[]= {57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4};
int pc2[]= {14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32};
int lShift[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
char c[29],d[29],k[64];
int i,j,nCount;
if (strlen(PassWord)!=8)
{
MessageBox(NULL,"密码必须为8位!",NULL,NULL);
return 0;
}
GetBit(PassWord,k); /*将密码转为64位,存在K数组中*/
for (i=0;i<28;i++) /*pc-1置换*/
{
c[i]=k[pc1[i]-1];
d[i]=k[pc1[i+28]-1];
}
for (i=0;i<16;i++) /*16次迭代*/
{
for (j=0;j<lShift[i];j++) /*左移运算*/
{
c[28]=c[0];
d[28]=d[0];
for(nCount=0;nCount<27;nCount++)
{
c[nCount]=c[nCount+1];
d[nCount]=d[nCount+1];
}
c[27]=c[28];
d[27]=d[28];
}
for (j=0;j<48;j++) /*pc-2置换*/
if ((pc2[j]-1)<28)
Key[i][j]=c[pc2[j]-1];
else
Key[i][j]=d[pc2[j]-29];
}
return 1;
}
BOOL IPChange(TCHAR *szSource,char *L,char *R) /*IP置换*/
{
int ip[]= {58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7};
char m[64];
int nCount;
GetBit(szSource,m); /*将要加密的数据转为64位,存在m数组中*/
for (nCount=0;nCount<32;nCount++) /*ip置换*/
{
*(L+nCount)=m[ip[nCount]-1];
*(R+nCount)=m[ip[nCount+32]-1];
}
return 1;
}
BOOL CalcLR(char *L,char *R,BOOL bCodeFlag) /*L、R迭代*/
{
int nCount;
char cTemp[32];
if (!bCodeFlag) /*判断是加密还是解密,0为加密,1为解密*/
for (nCount=0;nCount<16;nCount++) /*进行16次迭代*/
f(L,R,nCount);
else
for (nCount=15;nCount>=0;nCount--) /*进行16次迭代*/
f(L,R,nCount);
for (nCount=0;nCount<32;nCount++) /*交换L、R*/
{
cTemp[nCount]=*(R+nCount);
*(R+nCount)=*(L+nCount);
*(L+nCount)=cTemp[nCount];
}
return 1;
}
BOOL f(TCHAR *L,TCHAR *R,int nCount)
{
int e[]= {32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1};
int p[]= {16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25};
char cTemp[32],E[48];
int i;
for (i=0;i<32;i++) /*保存R*/
cTemp[i]=*(R+i);
for (i=0;i<48;i++) /*将R进行e置换并与密钥异或*/
E[i]=R[e[i]-1]^Key[nCount][i];
SBox(E); /*s盒置换*/
for (i=0;i<32;i++)
R[i]=E[p[i]-1]^L[i]; /*与L异或*/
for (i=0;i<32;i++)
*(L+i)=cTemp[i]; /*将原来的R变为L*/
return 1;
}
BOOL SBox(char *E) /*s盒*/
{
int sBox[8][64]={14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13, /*s1*/
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9, /*s2*/
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12, //s3
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14, /*s4*/
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3, /*s5*/
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13, /*s6*/
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12, /*s7*/
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}; /*s8*/
char s[6],cTemp[32];
int i,j,nCount,iRow,iColumn;
for (nCount=0;nCount<8;nCount++) /*分成8组*/
{
for (i=0;i<6;i++) /*从E中取出6位放入s中*/
s[i]=*(E+nCount*6+i);
iRow=s[0]*2+s[5]; /*计算行与列*/
iColumn=s[1]*8+s[2]*4+s[3]*2+s[4];
j=sBox[nCount][iRow*16+iColumn];
j<<=4;
for (i=0;i<4;i++)
{
cTemp[nCount*4+i]=j&128?1:0; /*将s盒里的结果转为4比特输出*/
j<<=1;
}
}
for (i=0;i<32;i++)
*(E+i)=cTemp[i];
return 1;
}
BOOL _IPChange(TCHAR *Dest,char *L,char *R) /*IP_1置换*/
{
int ip_1[]= {40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25};
char c[64];
int i,j;
for (i=0;i<64;i++) /*进行IP_1置换*/
{
if (i<32)
c[i]=*(L+ip_1[i]-1);
else
c[i]=*(R+ip_1[i]-33);
}
for (i=0;i<8;i++) /*将64位转回字母*/
{
*(Dest+i)='\0';
for (j=0;j<8;j++)
if (c[i*8+j])
*(Dest+i)+=(int) pow(2,7-j);
}
return 1;
}
BOOL GetBit(TCHAR *szSource,char *Array) /*将输入变成64位输出*/
{
int i,j;
int nTemp;
for (i=0;i<8;i++)
{
nTemp=szSource[i];
for (j=0;j<8;j++)
{
*(Array+i*8+j)=(nTemp&128?1:0);
nTemp<<=1;
}
}
return 1;
}
BOOL SetKey(TCHAR *szPassWord) /*设置密码变量*/
{
int i;
for (i=0;i<8;i++)
PassWord[i]=*(szPassWord+i);
return 1;
}