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

📄 mars.c

📁 This the mars encryption algorithm test source code.The mars is the IBM company s algorithm,and the
💻 C
字号:
/*********************************************************************/
/*-文件名:mars  													 */
/*-																	 */
/*-功能: 实现mars加密算法dll   									 */
/*-																	 */
/*-说明:															 */
/*- 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.          										 */
/*-																	 */
/*-本程序的所有权利由作者保留							             */
/*-																     */
/*-																     */
/*-版本号: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


/*解密时密钥的换位表*/
ULONG32 outkey[40] = { 0 };

#define WZ_COMMEND_NUM 4  
#define WZUSEHELPNUM 9
#define READFILESIZE 512 /*一次从文件中读取多少字节,可以根据内存的大小调节*/

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进制写入文件*/
INT32 encodehex(ULONG8 *tobuf,ULONG8 *frombuf,ULONG32 len);/*16进制解码*/
void wz_printhelp();/*打印帮助*/

INT8 *WZ_Commend_Help[] =
{
	
	"基于MARS的加密解密工具v1.0	 ",/*0*/	
	"追求卓越,勇于创新		 ",	
	"----著者 :  吴真---		 ",
	"			  	 "	
};

INT8 *WZ_USE_HELP[]={
	"输入5个参数:",
	"\t1.可执行文件名 *.exe",
	"\t2.操作类型 1:加密;2:解密;",
	"\t3.读出数据的文件名*.txt",
	"\t4.写入数据的文件名*.txt",	
	"\t5.密钥(16~32字节)",	
	"\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("请选择是加密|解密 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,ULONG32 keylen)
{
	INT32 filelen = 0,readlen = 0,writelen = 0;
	ULONG32 totalfilelen = 0 ;/*统计实际的文件的长度*/
	
	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进制形式写入文件*/
		memset(readbuf,0,READFILESIZE);	
		filelen = fread( readbuf, sizeof( INT8 ), READFILESIZE, readfile );
	}	
	/*这是从文件中读出的最后一批数据,长度可能会等于0,所以要先判断*/

	if ( (filelen > 0) && (filelen < READFILESIZE - 4) )
	{
		/*如果从文件中读出的长度不等于0,那么肯定有8个字节以上的空间
		文件长度存在最后8个字节中*/
		totalfilelen += filelen;
		memcpy( &readbuf[READFILESIZE-4], (ULONG8*)&totalfilelen,4);	
		mars_enc((ULONG32*)readbuf,READFILESIZE/4,outkey);
		hextofile( readbuf, writefile,READFILESIZE );/*以16进制形式写入文件*/		
		memset(readbuf,0 ,READFILESIZE);
	}
	else if(filelen == 0)
	{
		memcpy( &readbuf[12], (ULONG8*)&totalfilelen,4);			
		mars_enc((ULONG32*)readbuf,4,outkey);/*加密相当于16个字节*/		
		hextofile( readbuf, writefile, 16);/*以16进制形式写入文件*/		
	}
	else 
	{
		totalfilelen += filelen;		
		mars_enc((ULONG32*)readbuf,READFILESIZE/4,outkey);
		hextofile( readbuf, writefile,READFILESIZE );/*以16进制形式写入文件*/		
		memset(readbuf,0 ,READFILESIZE);
		memcpy( &readbuf[12], (ULONG8*)&totalfilelen,4);			
		mars_enc((ULONG32*)readbuf,4,outkey);/*加密相当于16个字节*/		
		hextofile( readbuf, writefile, 16);/*以16进制形式写入文件*/		

	}
	return SUCCESS;
}

INT32 file_dec( FILE *readfile, FILE *writefile,ULONG8 *key,ULONG32 keylen)
{
	INT32 filelen = 0,readlen = 0,writelen = 0;
	ULONG32 totalfilelen = 0 ;/*统计实际的文件的长度*/
	INT32 num;
	ULONG8 readbuf[READFILESIZE] = { 0 };
	ULONG8 sendbuf[READFILESIZE*2] = { 0 };		
	make_enckey( (ULONG32*)key ,keylen, outkey);
		

	fseek(readfile,-32,SEEK_END);/*最后16个字节的表示文件长度的空间*/			
	filelen = fread( sendbuf, sizeof( INT8 ), 32, readfile );	
	encodehex( readbuf,sendbuf,16);		
	mars_dec((ULONG32*) readbuf, 4,outkey);				
	memcpy((ULONG8*)&totalfilelen, &readbuf[12],4);/*得到文件总长*/
	memset(readbuf,0 ,16);
	memset(sendbuf,0 ,32);			
    
	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);
		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 ;
}

⌨️ 快捷键说明

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