C语言:对 *.txt 文件的加密和破译(最为简单的加密方法)

实现对 *.txt 文件的加密和破译(最为简单的加密方法),你肯定 get 到了最为简单的含义 \笑哭

附上源码

C语言,

笔记本键值,

Windows 下编译运行,

代码性质:混沌属性,光明与黑暗,

给输入文档命名时,必须取名:“test.txt”。或者改动源码。

#include <stdio.h> 
#include <conio.h> 
#include <stdlib.h>
#include <time.h>
#define Up 72
#define Down 80 

/**************************************************
英文字母使用频率表:(%) 
A 8.19 B 1.47 C 3.83 D 3.91 E 12.25 F 2.26 G 1.71 
H 4.57 I 7.10 J 0.14 K 0.41 L 3.77 M 3.34 N 7.06 
O 7.26 P 2.89 Q 0.09 R 6.85 S 6.36 T 9.41 
U 2.58 V 1.09 W 1.59 X 0.21 Y 1.58 Z 0.08
前面十位是:E T A O I N R S D C
**************************************************/

typedef struct
{
	int type;
	int count;
}save[256];

save T;
int target;
/************************************
The functions below are able to return
the ascil value of the highest frequen
-cy character.
*************************************/
int In_or_not(int ch)
{
	int i=0;
	
	while(T[i].type!=0)
	{
		if(ch==T[i].type)
		{
			target=i;
			return 1;
		}
		i++;
	}
	target=i;
	return 0;
}

int ReturnTXT_highestC(FILE* fp)
{
	int ch;
	int i,search=0;
	//统计文本,计入T(save[256]) 
	while((ch=fgetc(fp))!=EOF)    //读取到文件末尾或读取失败时返回 EOF   
	{
		if(In_or_not(ch))//If the ch is in the T(save)
		{
			T[target].count++;
		}
		else//The ch is out of T(save)
		{
			T[target].type=ch;
			T[target].count++;
		}
   	}
	 
	//统计结束,找出T.count的最大值对应的T.type,把type值返回给main里的maxc
	i=0;
	while(T[i].type!=0)
	{
		if(T[i].count>search)
		{
			search=T[i].count;
			target=i;
		}
		i++;
	}  //最大值的i坐标已储存于target
	return T[target].type; 
}

void initialT(void)
{
	int i;
	
	for(i=0;i<256;i++)
		T[i].type=0;
	for(i=0;i<256;i++)
		T[i].count=0;
}

int main(void)  
{   	
	int i,j,key,maxc,offset,Offset[4];
	int ch;
	FILE *fp1,*fp2,*fp3,*fp4,*fp5;
	
	initialT();
	printf("Down=加密\nUp=破译\n");
	key=getch();
	key=getch();
	switch(key)
	{
		case Down://加密
			printf("Down\n");
			fp1=fopen("test.txt","r");
			if(fp1==NULL)
			{
				printf("\n打开失败\n");
				getch(); 
				return 0;
			}	
			fp2=fopen("output.txt","w");
			
			srand((unsigned)time(NULL));   /*随机种子*/
			offset=rand()%(20-0+1)-16;     /*-16~4之间的随机数*/
			printf("offset:%d\n",offset);  //visible
			while((ch=fgetc(fp1))!=EOF)    //读取到文件末尾或读取失败时返回 EOF   
			{
				fputc(ch+offset,fp2);
    		}       
			fclose(fp2);				
			break;
		case Up://破译
			printf("Up");
			fp1=fopen("output.txt","r");
			if(fp1==NULL)
			{
				printf("\n打开失败\n"); 
				getch();
				return 0;
			}
			fp2=fopen("probability1.txt","w");
			fp3=fopen("probability2.txt","w");
			fp4=fopen("probability3.txt","w");	
			fp5=fopen("probability4.txt","w");
			maxc=ReturnTXT_highestC(fp1);//用maxc接收频率最高的value of char
			Offset[0]=' '-maxc;   //偏移量
			Offset[1]='e'-maxc;   //偏移量
			Offset[2]='t'-maxc;   //偏移量
			Offset[3]='a'-maxc;   //偏移量
			printf("\nMaxChar:%d→",maxc);//visible	
			for(i=0;i<4;i++)
			{
				printf("\t第%d种可能 ",i+1);
				putchar(maxc+Offset[i]);
			}
			puts("");
			printf("offsets:%d\t%d\t%d\t%d\n",Offset[0],Offset[1],Offset[2],Offset[3]);//visible
			fp1=fopen("output.txt","r");//不加这行fp1就被ReturnTXT_highestC玩坏了?搞不懂,加了就好了。 
			while((ch=fgetc(fp1))!=EOF)    //读取到文件末尾或读取失败时返回 EOF   
			{
				fputc(ch+Offset[0],fp2);
				fputc(ch+Offset[1],fp3);
				fputc(ch+Offset[2],fp4);
				fputc(ch+Offset[3],fp5);
    		}       
			fclose(fp2);
			fclose(fp3);
			fclose(fp4);
			fclose(fp5);
			//Visible quick check (begin)
			for(j=0;j<4;j++)
			{
				printf("\n\n>---------------------------probability%d---------------------------<",j+1);
				printf("\ntype:\n");
				i=0;
				while(T[i].type!=0)
				{
					putchar(T[i].type+Offset[j]);
					printf("\t");
					i++;
				}
				puts("");
				puts("");
				printf("count:\n");
				i=0;
				while(T[i].type!=0)
				{
					printf("%d",T[i].count);
					printf("\t");
					i++;
				}
			}
			//Visible quick check (end)		
			break;
		default:
			break;
	} 
	getch(); 
	return 0;  
}  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值