?? mars.c
字號(hào):
/*********************************************************************/
/*-文件名:mars */
/*- */
/*-功能: 實(shí)現(xiàn)mars加密算法dll */
/*- */
/*-說(shuō)明: */
/*- The MARS algorithm is covered by a pending patent application */
/*- owned by IBM, who intend to offer a royalty free license under */
/*- any issued patent that results from such application if MARS is */
/*- selected as the AES algorithm. In the interim, you may evaluate */
/*- the MARS algorithm for your personal, lawful, non-profit purposes*/
/*- as an end user. */
/*- */
/*-本程序的所有權(quán)利由作者保留 */
/*- */
/*- */
/*-版本號(hào):1.0.0(2002.6) */
/*- */
/*- */
/*-AUTHOR:吳真(WUZHEN) */
/*- */
/*********************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <malloc.h>
#include "mars.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
/*解密時(shí)密鑰的換位表*/
ULONG32 outkey[40] = { 0 };
#define WZ_COMMEND_NUM 4
#define WZUSEHELPNUM 9
#define READFILESIZE 512 /*一次從文件中讀取多少字節(jié),可以根據(jù)內(nèi)存的大小調(diào)節(jié)*/
INT32 file_enc( FILE *readfile, FILE *writefile,ULONG8 *key,ULONG32 keylen);/*加密文件*/
INT32 file_dec( FILE *readfile, FILE *writefile,ULONG8 *key,ULONG32 keylen);/*解密文件*/
INT32 hextofile( ULONG8 *buf ,FILE *writefile, ULONG32 length);/*以16進(jìn)制寫入文件*/
INT32 encodehex(ULONG8 *tobuf,ULONG8 *frombuf,ULONG32 len);/*16進(jìn)制解碼*/
void wz_printhelp();/*打印幫助*/
INT8 *WZ_Commend_Help[] =
{
"基于MARS的加密解密工具v1.0 ",/*0*/
"追求卓越,勇于創(chuàng)新 ",
"----著者 : 吳真--- ",
" "
};
INT8 *WZ_USE_HELP[]={
"輸入5個(gè)參數(shù):",
"\t1.可執(zhí)行文件名 *.exe",
"\t2.操作類型 1:加密;2:解密;",
"\t3.讀出數(shù)據(jù)的文件名*.txt",
"\t4.寫入數(shù)據(jù)的文件名*.txt",
"\t5.密鑰(16~32字節(jié))",
"\t 例: 1 1.txt 2.txt 1234567812345678",
"\t : 2 2.txt 3.txt 1234567812345678",
"******************************"
};
void main(INT32 argc,INT8 *argv[])
{
INT8 *FILENAME1,*FILENAME2;
FILE *fp, *fp2;
ULONG8 key[33] = { 0 }; /*密鑰容器*/
ULONG32 keylen = 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 ;
}
keylen = ( strlen(argv[4]) < 32 )? strlen( argv[4]):32 ;
memcpy( key, argv[4] , keylen );/*取得密鑰*/
switch( atoi(argv[1] ))
{
case 1:/*加密操作*/
file_enc(fp,fp2,key,keylen);
printf("\n \t MARS 加密完畢,密文存于%s文件\n",FILENAME2);
break;
case 2:
/*解密*/
file_dec(fp,fp2,key,keylen);
printf("\n \t MARS 解密完畢,明文存于%s文件\n",FILENAME2);
break;
default:
printf("請(qǐng)選擇是加密|解密 plese choose encrypt|deencrypt\n");
break;
}
fclose(fp);
fclose(fp2);
}
INT32 hextofile( ULONG8 *buf ,FILE *writefile, ULONG32 length)
{
ULONG32 writelen = 0 ;
/*以16進(jìn)制形式寫入文件*/
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,ULONG32 keylen)
{
INT32 filelen = 0,readlen = 0,writelen = 0;
ULONG32 totalfilelen = 0 ;/*統(tǒng)計(jì)實(shí)際的文件的長(zhǎng)度*/
ULONG8 readbuf[READFILESIZE] = { 0 };
make_enckey((ULONG32*)key,keylen,outkey);
filelen = fread( readbuf, sizeof( INT8 ), READFILESIZE, readfile );
while( filelen == READFILESIZE )
{
totalfilelen += READFILESIZE;
mars_enc((ULONG32*)readbuf,READFILESIZE/4 , outkey);
hextofile( readbuf, writefile, READFILESIZE );/*以16進(jìn)制形式寫入文件*/
memset(readbuf,0,READFILESIZE);
filelen = fread( readbuf, sizeof( INT8 ), READFILESIZE, readfile );
}
/*這是從文件中讀出的最后一批數(shù)據(jù),長(zhǎng)度可能會(huì)等于0,所以要先判斷*/
if ( (filelen > 0) && (filelen < READFILESIZE - 4) )
{
/*如果從文件中讀出的長(zhǎng)度不等于0,那么肯定有8個(gè)字節(jié)以上的空間
文件長(zhǎng)度存在最后8個(gè)字節(jié)中*/
totalfilelen += filelen;
memcpy( &readbuf[READFILESIZE-4], (ULONG8*)&totalfilelen,4);
mars_enc((ULONG32*)readbuf,READFILESIZE/4,outkey);
hextofile( readbuf, writefile,READFILESIZE );/*以16進(jìn)制形式寫入文件*/
memset(readbuf,0 ,READFILESIZE);
}
else if(filelen == 0)
{
memcpy( &readbuf[12], (ULONG8*)&totalfilelen,4);
mars_enc((ULONG32*)readbuf,4,outkey);/*加密相當(dāng)于16個(gè)字節(jié)*/
hextofile( readbuf, writefile, 16);/*以16進(jìn)制形式寫入文件*/
}
else
{
totalfilelen += filelen;
mars_enc((ULONG32*)readbuf,READFILESIZE/4,outkey);
hextofile( readbuf, writefile,READFILESIZE );/*以16進(jìn)制形式寫入文件*/
memset(readbuf,0 ,READFILESIZE);
memcpy( &readbuf[12], (ULONG8*)&totalfilelen,4);
mars_enc((ULONG32*)readbuf,4,outkey);/*加密相當(dāng)于16個(gè)字節(jié)*/
hextofile( readbuf, writefile, 16);/*以16進(jìn)制形式寫入文件*/
}
return SUCCESS;
}
INT32 file_dec( FILE *readfile, FILE *writefile,ULONG8 *key,ULONG32 keylen)
{
INT32 filelen = 0,readlen = 0,writelen = 0;
ULONG32 totalfilelen = 0 ;/*統(tǒng)計(jì)實(shí)際的文件的長(zhǎng)度*/
INT32 num;
ULONG8 readbuf[READFILESIZE] = { 0 };
ULONG8 sendbuf[READFILESIZE*2] = { 0 };
make_enckey( (ULONG32*)key ,keylen, outkey);
fseek(readfile,-32,SEEK_END);/*最后16個(gè)字節(jié)的表示文件長(zhǎng)度的空間*/
filelen = fread( sendbuf, sizeof( INT8 ), 32, readfile );
encodehex( readbuf,sendbuf,16);
mars_dec((ULONG32*) readbuf, 4,outkey);
memcpy((ULONG8*)&totalfilelen, &readbuf[12],4);/*得到文件總長(zhǎng)*/
memset(readbuf,0 ,16);
memset(sendbuf,0 ,32);
num = totalfilelen/READFILESIZE;/*有幾個(gè)READFILESIZE組*/
totalfilelen %= READFILESIZE;
fseek(readfile,0,SEEK_SET);/*跳到文件頭*/
while(num--)
{
filelen = fread( sendbuf, sizeof( INT8 ), READFILESIZE*2, readfile );
encodehex( readbuf,sendbuf,READFILESIZE);
mars_dec((ULONG32*) readbuf, READFILESIZE/4,outkey);
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);
mars_dec((ULONG32*) readbuf, READFILESIZE/4,outkey);
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 ;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -