正在阅读:实现基于IDEA算法的加密工具(8)实现基于IDEA算法的加密工具(8)

2004-02-14 09:34 出处:PConline 作者:吴真 责任编辑:zwg
任何媒体不得转载  3.6.1 加密工具实现源代码 下面列出按照上述操作思路编写的实现源代码 /**********************************************************************/ /*-文件名:idea.c */ /*- */ /*-功能: 利用idea加密算法实现文件的加密 */ /*- */ /*-说明: */ /*- 这是利用IDEA算法实现的文件加密工具可以在法律允许范围内以非商 */ /*-业形式自由使用,该程序的所有权利由作者吴真保留 */ /*- */ /*-版本号:1.0.0(2002.6) */ /*-AUTHOR:吴真(WUZHEN) */ /*- */ /*********************************************************************/ #include #include #include #include #include #include "idea.h" typedef int INT32; typedef char INT8; typedef unsigned char ULONG8; typedef unsigned short ULONG16; typedef unsigned long ULONG32; #define SUCCESS 0 #define FAIL -1 #define WZ_COMMEND_NUM 4 #define WZUSEHELPNUM 7 #define READFILESIZE 512 /*一次从文件中读取多少字节,可以根据内存的大小调节*/ INT32 file_enc( FILE *readfile, FILE *writefile,ULONG8 *key);/*加密文件*/ INT32 file_dec( FILE *readfile, FILE *writefile,ULONG8 *key);/*解密文件*/ INT32 hextofile( ULONG8 *buf ,FILE *writefile, ULONG32 length);/*以16进制写入文件*/ INT32 encodehex(ULONG8 *tobuf,ULONG8 *frombuf,ULONG32 len);/*16进制解码*/ void wz_printhelp();/*打印帮助*/ INT8 *WZ_Commend_Help[] = { "基于IDEA的加密解密工具v1.0 ",/*0*/ "追求卓越,勇于创新 ", "----著者 : 吴真--- ", " " }; INT8 *WZ_USE_HELP[]={ "输入5个参数:", "\t1.可执行文件名 *.exe", "\t2.操作类型 1:加密;2:解密;", "\t3.读出数据的文件名*.txt", "\t4.写入数据的文件名*.txt", "\t5.密钥(32字节)", "******************************" };   void main(INT32 argc,INT8 *argv[]) { INT8 *FILENAME1,*FILENAME2; FILE *fp, *fp2; ULONG8 key[33] = { 0 }; /*密钥容器*/ if ( argc != 5 ) { wz_printhelp(); return; } FILENAME1 = argv[2]; FILENAME2 = argv[3]; if ((fp= fopen(FILENAME1,"r+b")) == NULL || (fp2 = fopen(FILENAME2,"w+b"))==NULL) { printf("Can't open file\n"); return ; } memcpy( key, argv[4] , strlen( argv[4]) );/*取得密钥*/ switch( atoi(argv[1] )) { case 1:/*加密操作*/ file_enc(fp,fp2,key); printf("\n \t IDEA 加密完毕,密文存于%s文件\n",FILENAME2); break; case 2: /*解密*/ file_dec(fp,fp2,key); printf("\n\t IDEA解密完毕,明文存于%s文件\n",FILENAME2); break; default: printf("请选择是加密|解密 plese choose encrypt|deencrypt\n"); break; } fclose(fp); fclose(fp2); } INT32 hextofile( ULONG8 *buf ,FILE *writefile, ULONG32 length) { ULONG32 writelen = 0 ; /*以16进制形式写入文件*/ while( writelen < length) { if(buf[writelen] == 0) { fprintf( writefile, "%x", 0 ); fprintf( writefile, "%x", 0 ); } else if (buf[writelen] < 0x10) { fprintf( writefile, "%x", 0 ); fprintf( writefile, "%x", buf[writelen] ); } else { fprintf( writefile, "%x", buf[writelen] ); } writelen++; } return SUCCESS; } INT32 file_enc( FILE *readfile, FILE *writefile,ULONG8 *key) { INT32 filelen = 0,readlen = 0,writelen = 0; ULONG32 totalfilelen = 0 ;/*统计实际的文件的长度*/ INT32 i; ULONG8 readbuf[READFILESIZE] = { 0 }; idea_makekey( (ULONG32*)key , outkey); filelen = fread( readbuf, sizeof( INT8 ), READFILESIZE, readfile ); while( filelen == READFILESIZE ) { totalfilelen += READFILESIZE; for ( i = 0 ; i < READFILESIZE ; i += 8) { idea_enc( (ULONG16*)&readbuf[i] );/*加密*/ } hextofile( readbuf, writefile, READFILESIZE );/*以16进制形式写入文件*/ memset(readbuf,0,READFILESIZE); filelen = fread( readbuf, sizeof( INT8 ), READFILESIZE, readfile ); } /*这是从文件中读出的最后一批数据,长度可能会等于0,所以要先判断*/ if ( filelen > 0 ) { /*如果从文件中读出的长度不等于0,那么肯定有8个字节以上的空间 文件长度存在最后8个字节中*/ totalfilelen += filelen; memcpy( &readbuf[READFILESIZE-8], (ULONG8*)&totalfilelen,4); for ( i = 0 ; i < READFILESIZE ; i += 8) { idea_enc( (ULONG16*)&readbuf[i]);/*加密*/ } hextofile( readbuf, writefile,READFILESIZE );/*以16进制形式写入文件*/ memset(readbuf,0 ,READFILESIZE); } else /*filelen == 0*/ { memcpy( &readbuf[0], (ULONG8*)&totalfilelen,4); idea_enc( (ULONG16*)&readbuf[0] );/*加密*/ hextofile( readbuf, writefile, 8);/*以16进制形式写入文件*/ } return SUCCESS; } INT32 file_dec( FILE *readfile, FILE *writefile,ULONG8 *key) { INT32 filelen = 0,readlen = 0,writelen = 0; ULONG32 totalfilelen = 0 ;/*统计实际的文件的长度*/ INT32 i,num; ULONG8 readbuf[READFILESIZE] = { 0 }; ULONG8 sendbuf[READFILESIZE*2] = { 0 }; idea_makekey( (ULONG32*)key , outkey); key_decryExp(outkey); fseek(readfile,-16,SEEK_END);/*最后16个字节的表示文件长度的空间*/ filelen = fread( sendbuf, sizeof( INT8 ), 16, readfile ); encodehex( readbuf,sendbuf,8); idea_dec( (ULONG16*)&readbuf[0] );/*解密*/ memcpy((ULONG8*)&totalfilelen, &readbuf[0],4);/*得到文件总长*/ memset(readbuf,0 ,8); memset(sendbuf,0 ,16); num = totalfilelen/READFILESIZE;/*有几个READFILESIZE组*/ totalfilelen %= READFILESIZE; fseek(readfile,0,SEEK_SET);/*跳到文件头*/ while(num--) { filelen = fread( sendbuf, sizeof( INT8 ), READFILESIZE*2, readfile ); encodehex( readbuf,sendbuf,READFILESIZE); for ( i = 0 ; i < READFILESIZE ; i += 8) { idea_dec( (ULONG16*)&readbuf[i] );/*解密*/ } writelen = fwrite(readbuf, sizeof( INT8 ), READFILESIZE, writefile); memset(readbuf,0 ,READFILESIZE); memset(sendbuf,0 ,READFILESIZE*2); } if ( totalfilelen > 0 )/*最后一块有多余的元素*/ { filelen = fread( sendbuf, sizeof( INT8 ), READFILESIZE*2, readfile ); encodehex( readbuf,sendbuf,READFILESIZE); for ( i = 0 ; i < READFILESIZE ; i += 8) { idea_dec( (ULONG16*)&readbuf[i] );/*解密*/ } writelen = fwrite(readbuf, sizeof( INT8 ), totalfilelen, writefile); memset(readbuf,0 ,READFILESIZE); memset(sendbuf,0 ,READFILESIZE*2); } return SUCCESS; } INT32 encodehex(ULONG8 *tobuf,ULONG8 *frombuf,ULONG32 len) { ULONG8 *readfirst = frombuf ; ULONG8 *readend = &frombuf[1] ; INT8 *s; ULONG8 y[2] ; ULONG32 i; for ( i = 0 ; i < len ; i++) { y[0] = *readfirst ; y[1] = *readend ; readfirst += 2 ; readend += 2 ; tobuf[i] = (ULONG8)strtol((INT8*)y, &s, 16); } return SUCCESS; } void wz_printhelp() { INT32 i ; printf("\t"); for ( i = 0 ; i < 22 ; i++) { printf("%c ",5); } printf("\n"); for( i = 0 ; i < WZ_COMMEND_NUM ; i++) { printf("\t%c\t%s %c\n",5,WZ_Commend_Help[i],5); } printf("\t"); for ( i = 0 ; i < 22 ; i++) { printf("%c ",5); } printf("\n"); for( i = 0 ; i < WZUSEHELPNUM ; i++) { printf("\t%s\n",WZ_USE_HELP[i]); } return ; }

相关文章

关注我们

最新资讯离线随时看 聊天吐槽赢奖品