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

📄 main.c

📁 简单的vigenere解密及其解密功能实现代码
💻 C
字号:
#include <stdio.h>#include <string.h>#include "dvigenere.h"#include "dcaesar.h"#define BASE 26#define TOP 3#define LEN 24#define PRO_NAME "dvigenere-cracker"#define PRO_VER "0.1"static void display_usage( void );static void display_logo( void );static int getkey( unsigned long start, int len, FILE *in );extern void encrypt( char c, int key, FILE *out );extern void decrypt( char c, int key, FILE *out );int main( int argc, char **argv ){	FILE *in, *out;	char *infile, *outfile;	char c;	int i, j, len;	int key[LEN];	/**	 * check whether enough parameters	 **/	if( 3!=argc && 2!=argc )	{		display_usage();		return -1;	}	/**	 * intput and output	 **/	infile = argv[1];	in = fopen( infile, "rb" );	if( NULL==in )	{		fprintf( stderr, "error: can not open file %s\n", infile );		return -3;	}	if( 3==argc )	{		outfile = argv[2];		out = fopen( outfile, "wb" );		if( NULL==out )		{			fclose( in );			fprintf( stderr, "error: can not create file %\n", outfile );			return -4;		}	}	else		out = stdout;		/**	 * crack the length of the key	 **/	len = key[0] = 0;	while( !(key[0]=getkey(0,len,in)) && len<=LEN )	{		len++;	};	if( LEN==len )	{		fprintf( stderr, "error: the length of the key is larger then %d\n", LEN  );		return -5;	}	len++;	if( 3==argc ) printf( "the length of the key : %d\n", len ); 	/**	 * crack all the keys	 **/	for( i=1; i<len; i++ )	{		key[i] = getkey( i, len-1, in ); // because auto-add-offset 1 when read a char	}	if( 3==argc ) 	{		printf( "the key : " ); 		for( i=0; i<len; i++ )			printf( "%c", key[i]+'a' );		printf( "\n" );	}	/**	 * output the result	 **/	fseek( in, 0l, SEEK_SET );	i = 0;	while( fread(&c,sizeof(char),1,in) )	{		decrypt(c,key[i%len],out);		i++;	}		if( 3==argc ) fclose( out );	fclose( in );	return 0;};static void display_usage( void ){	printf( "%s v%s by dorainm, dorainm@gmail.com\n", PRO_NAME, PRO_VER );	printf( "this is a free program; it used to encrypt or decrypt the string or file\n" );	printf( "with the vigenere cipher, and current version only support all the letters\n" );	printf( "\n" );	printf( "usage :\n" );	printf( "    %s infile <outfile> | -h\n", PRO_NAME);	printf( "\n" );	printf( "options :\n" );	printf( "    infile               point out the file would be cracked\n" );	printf( "    outfile              the file to save the result; if it hasn't be set,\n" );        printf( "                            the cracked plain text will displayed on <stdout>\n" );	printf( "    -h | --help          display this help\n" );	printf( "\n" );	printf( "report bugs to <dorainm@gmail.com>\n" );	return;};static int getkey( unsigned long start, int len, FILE *in ){	int i, j, key = 0;	int num[BASE], top[TOP];	char c;	fseek( in, start, SEEK_SET );	/**	 * count all the letter's displayed times	 **/	for( i=0; i<BASE; i++ ) num[i] = 0;		while( fread(&c,sizeof(char),1,in) )	{		if( blowerletter(c) )			num[c-'a']++;		else if( bpowerletter(c) )			num[c-'A']++;		fseek( in, len, SEEK_CUR );	}		/**	 * compute the top TOP letters	 **/	for( i=0; i<TOP; i++ )	{		top[i] = 0;		for( j=0; j<BASE; j++ )		{			if( num[top[i]]<num[j] ) top[i] = j;		}		num[top[i]] = 0;	}			/**	 * crack out the key's value	 **/	for( i=0; i<TOP-1; i++ )	{		for( j=1; j<TOP; j++ )		{			if( ((top[i]+'a'-'e'+BASE)%BASE)==((top[j]+'a'-'t'+BASE)%BASE) )			{				key = ((top[i]+'a'-'e'+BASE)%BASE);			}		}	}	return key;}extern void encrypt( char p, int key, FILE *out ){	if( blowerletter(p) )		fputc( dcaesar_encrypt(p-'a',key,BASE,'A'), out );	else if( bpowerletter(p) )		fputc( dcaesar_encrypt(p-'A',key,BASE,'a'), out );	else		fputc( p, out );	return;};extern void decrypt( char c, int key, FILE *out ){	if( blowerletter(c) )		fputc( dcaesar_decrypt(c-'a',key,BASE,'A'), out );	else if( bpowerletter(c) )		fputc( dcaesar_decrypt(c-'A',key,BASE,'a'), out );	else		fputc( c, out );	return;};

⌨️ 快捷键说明

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