⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cedc.~cpp

📁 一个用DES做的加/解密代码
💻 ~CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include "CEDC.h"
//////////////////////////////////////////////////////////////////////////
char *inname, *outname;
FILE *infile, *outfile;
int outcount=0;
int encrypting;
char buf[512];
char keyx[9],keyy[9];
int bytebit[]={0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};
int nibblebit[]={0x8,0x4,0x2,0x1};
char iperm[16][16][8],fperm[16][16][8];
char s[4][4096];
char p32[4][256][4];
char kn[16][6];				            /* 密钥*/
char pc1m[56];
char pcr[56];

char ip[]				                /* 初始换位表*/
= {	58, 50, 42, 34, 26, 18, 10,  2,     /*输入的第58比特,在输出时被*/
	60, 52, 44, 36, 28, 20, 12,  4,     /*置换到第1比特的位置*/
	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 fp[]			                    /* 最后换位表*/
= {	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 pc1[]				/* 密鈅的压缩型换位表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	};

char totrot[]			   /* 密钥生成中的循环左移位的累计次数*/
= {	1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28};

char pc2[]				/* 密鈅的压缩型换位表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	};

char si[8][64]			  /* 48->32 bit 压缩表*/
= {					/* S[1]			 */
	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,
					/* S[2]			 */
	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,
					/* S[3]			 */
	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,
					/* S[4]			 */
	 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,
					/* S[5]			 */
	 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,
					/* S[6]			 */
	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,
					/* S[7]			 */
	 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,
					/* S[8]			 */
	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	};

char p32i[]				/* 32-bit 单纯换位表*/
= {	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	};
//////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TEDCForm *EDCForm;
//---------------------------------------------------------------------------
__fastcall TEDCForm::TEDCForm(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TEDCForm::BitBtn1Click(TObject *Sender)
{
    register char *u;
	char *filename;
    BitBtn1->Enabled=false;
	inname=new char[40];// malloc(strlen(inname) + 5);
    strcpy(inname,Edit1->Text.c_str()); //*argv;
	outname=new char[strlen(inname)+5];// malloc(strlen(inname) + 5);
    filename=new char[strlen(inname)+5];//malloc(strlen(inname) + 5);
	strcpy(filename,inname);
	u=&filename[strlen(filename)-4];	 /* 检查最后4个字符*/
	encrypting=(strcmp(".des",u)!=0);
	if(!encrypting)*u=0;			     /* 解密时输出文件去掉.des*/
	else strcat(filename,".des");		 /* 加密时给输出文件加上.des*/
    strcpy(outname,filename);
	if((infile=fopen(inname, "rb")) == NULL)
	{
        Application->MessageBoxA("不能打开源文件!","错误",0);
        return;
	}
	if((outfile=fopen(outname,"rb"))!=NULL)
	{
        if(Application->MessageBox("文件存在,要覆盖吗?",NULL,1)==IDOK)
            goto A;
        else
            return;
	}
A:; if((outfile=fopen(outname,"wb"))==NULL)
	{
        Application->MessageBoxA("不能打开输出文件!","错误",0);
        return;
	}
	key_get();                       /*取得八字节密钥*/
    strcpy(keyx,keyy);
	desinit(keyx);	     /*建立用于des运算的矩阵*/
	if(pfile()==0)
        unlink(inname);
	else
        Application->MessageBoxA("输入/输出错误!","错误",0);
	fclose(outfile);
	fclose(infile);
    Application->MessageBoxA("   加/脱密完毕!  ","消息",0);
    delete inname;
    delete outname;
    delete filename;
}
//---------------------------------------------------------------------------

void __fastcall TEDCForm::key_get()	/*输入密码*/
{
    register int i, j;
	char linebuf[256];
	int count;
	count=Edit2->Text.Length();     /*密钥长度*/
    strcpy(linebuf,Edit2->Text.c_str());
	linebuf[count]=0;               /* 添加一个0作为字符串的结束*/
	if(linebuf[count-1]=='\n')
	{
        linebuf[count-1]=0;	        /*把"\n"变成0*/
	    count--;
	}
	if(count>16)count=16;	            /* 只取16个字符*/
    for(i=0;i<16;i++)
    {
        switch(linebuf[i])
        {
            case 'A':
                linebuf[i]=0x0A;break;
            case 'B':
                linebuf[i]=0x0B;break;
            case 'C':
                linebuf[i]=0x0C;break;
            case 'D':
                linebuf[i]=0x0D;break;
            case 'E':
                linebuf[i]=0x0E;break;
            case 'F':
                linebuf[i]=0x0F;break;
        }
        if(!(i%2))linebuf[i]=linebuf[i]<<4;
    }
    for(count=0,i=0;i<16;i+=2)
        linebuf[count++]=linebuf[i]^linebuf[i+1];
	count=8;	            /* 只取 8 字节 */
    for(i=j=0; count--;)
	   keyy[i++]=linebuf[j++];
}
//---------------------------------------------------------------------------
void __fastcall TEDCForm::desinit(char *key)/* 初始化各种变换矩阵*/
{
    #ifdef DEBUG
    /*deb*/ Label5->Caption="Initial perm init.";
    #endif
	    perminit(iperm,ip);
    #ifdef DEBUG
    /*deb*/ Label5->Caption="Final perm init.";
    #endif
    	perminit(fperm,fp);
    #ifdef DEBUG
    /*deb*/ Label5->Caption="Key sched init.";
    #endif
	    kinit(key);
    #ifdef DEBUG
    /*deb*/ Label5->Caption="Compression init.";
    #endif
	    sinit();          /* 初始化s1-s8矩阵*/

    #ifdef DEBUG
    /*deb*/ Label5->Caption="32-bit perm init.";
    #endif
	    p32init();
    #ifdef DEBUG
    /*deb*/ Label5->Caption="End init.";
    #endif
}
//---------------------------------------------------------------------------

void __fastcall TEDCForm::sinit()/* 初始化s1-s8矩阵*/
{
    register int i,j;
	for(i=0;i<4;i++)	 /* 48位按四个12位(一个半字节)来输入*/
    	for(j=0;j<4096;j++)      /*4095=1111 1111 1111B*/
            s[i][j]=(getcomp(i*2,j>>6)<<4)|(0x0F&getcomp(i*2+1,j&0x3F));
                                 /*每个s项为一个字节,存有两个体代数*/
}
//---------------------------------------------------------------------------

int __fastcall TEDCForm::getcomp(int k,int v)
                    		     /*用s[k]盒对v进行6bit~4bit的数据压缩*/
//int k,v;                       /*只取6bits的中间4bits*/
{
    register int i,j;
	i=((v&0x20)>>4)|(v&1);  /*i为每个s盒的行号改行号由v的第一位和最后一位组成*/
	j=(v&0x1F)>>1;		     /*j位s盒的列号,该列号由v的中间四位组成*/
	return(int)si[k][(i<<4)+j];  /*返回对v压缩后的值*/
/*
    FILE *fg;
    if((fg=fopen("e:\\ajx\\perm.txt","w+"))==NULL)
	{
        Application->MessageBoxA("不能打开输出文件!","错误",0);
        return;
	}
    fprintf(fg,"%d,",perm[i][j][k]);
    fclose(fg);
*/
}
//---------------------------------------------------------------------------

void __fastcall TEDCForm::perminit(char perm[16][16][8],char p[64])
                                /* 初始化换位表,用IP表对输入的64bit进行换位*/
//char perm[16][16][8];	    	/* 64-bit, 完成换位的中间的过渡矩阵*/
//char p[64];
{
    register int L, j, k;
	int i,m;
	for(i=0;i<16;i++)		    /* 每一个输入的4 bit(4*16)*/
		for(j=0;j<16;j++)	    /* 每一个4 bit的可能值~[0,16]*/
		    for(k=0;k<8;k++)			/* 每一个屏蔽字*/
			    perm[i][j][k]=0;		/* 过渡矩阵清零*/
	for(i=0;i<16;i++)		     /* 每一个输入的4 bit*/
		for(j=0;j<16;j++)	     /* 每一个4 bit的可能值~[0,16]*/
		    for (k=0;k<64;k++)	     /* 每一个输出bit的位置*/
		    {
                L=p[k]-1;		/* p[k]最小值为l,l从0开始*/
	            if((L>>2)!=i)		 /* L~[0,63],每四位一组,共十六组,因此要L>>2*/
            		continue;           /*确定L的组数*/
            	if(!(j&nibblebit[L&3]))/*确定第L~[0,63]位是一个四位组中的第几位跑k*/
            		continue;           /*nibblebit[]={8,4,2,1}*/
            	m=k&0x07;		/*确定第k~[0,63]位是一个字节中的第几位*/
                               /*k&07->取k的后3bits->m是一个字节中的第0~7位*/
        	perm[i][j][k>>3]|=bytebit[m];
	        }                           /*bytebit[]={128,64,32,16,8,4,2,1}*/
}
//---------------------------------------------------------------------------

void __fastcall TEDCForm::iter(int num,char *inblock,char *outblock)
                                    /* 完成第num层的变换*/
//int num;
//char *inblock, *outblock;			/* 64 bits	*/
{
    char fret[4];		        	/* 放置f(R[i-1],key)的返回值*/
	register char *ib,*ob,*fb;
	ob=outblock;
	ib=&inblock[4];			        /*右边32位*/
	f(ib,num,fret);
	*ob++=*ib++;			/* L[i] = R[i-1]		*/
	*ob++=*ib++;
	*ob++=*ib++;
	*ob++=*ib++;
	ib=inblock; fb=fret;	        /* R[i]=L[i] XOR f(R[i-1],key)  */
	*ob++=*ib++^*fb++;
	*ob++=*ib++^*fb++;
	*ob++=*ib++^*fb++;
	*ob++=*ib++^*fb++;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -