国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

實現基于IDEA算法的加密工具(8)

2019-11-17 04:39:02
字體:
來源:轉載
供稿:網友


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 encryptdeencrypt/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 ;

}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延寿县| 普陀区| 漯河市| 咸阳市| 玉林市| 宜都市| 大英县| 南康市| 晋城| 班玛县| 乐昌市| 尚志市| 钟祥市| 安顺市| 秀山| 新绛县| 济宁市| 且末县| 古蔺县| 双辽市| 丰原市| 山东省| 沧源| 普格县| 阜城县| 浦县| 余姚市| 桐梓县| 四子王旗| 监利县| 城步| 锦州市| 仙桃市| 鄂温| 彝良县| 南澳县| 府谷县| 怀集县| 类乌齐县| 广丰县| 宣威市|