Linux下简单加密程序实现

实现文件加密功能的C语言程序
本文介绍了一个使用C语言实现的文件加密程序,该程序可以将文件中字符进行循环加密,即a变为b,b变为c,直到z变为a,并且能够处理大写字母。包括读取文件、加密过程和写入文件的功能。

1.程序

#include
#include
#include

//功能:实现加密功能,将文件中的a变为b,b变成c,到最后z再变为a,大写字母也一样

void encrypt(char *path);
int ReadFile(char *path,char *temp);
int WriteFile(char *path,char *temp);

int main(int argc,char *argv[])
{
	int i = 0;

	if(argc == 1)													

							//当文件参数不存在时,提示
	{
		printf("加密文件不存在!\n");
		//return -1;
	}
	else
	{
		for(i=0;i= 'a' && temp[i] < 'z') || (temp[i] >= 'A' && temp[i] < 'Z'))
			temp[i]++;
		else if(temp[i] == 'z')
			temp[i] = 'a';
		else if(temp[i] == 'Z')
			temp[i] = 'A';
	}

	if(WriteFile(path,temp) == 1)			//调用写文件函数
		return;

	return;
}

int ReadFile(char *path,char *temp)
{
	FILE *fin = NULL;
	char ch = ' ';
	int i=0;

	//读文件
	fin = fopen(path,"r");
	if(!fin)					//文件读取失败
	{
		printf("文件%s读取失败!\n",path);
		return 1;
	}
															

							//每次读取文件中单个字符,并放到一个数组中
	ch = fgetc(fin);
	while(ch != EOF)
	{
		temp[i] = ch;
		i++;
		ch = fgetc(fin);
	}
	temp[i] = '\0';					//设置字符串的末尾

	fclose(fin);					//文件读取结束;

	return 0;
}

int WriteFile(char *path,char *temp)
{
	FILE *fout = NULL;

	//写文件
	fout = fopen(path,"w");				//以写操作的形式打开文件,原文件被删除
	if(!fout)					//文件写失败
	{
		printf("文件%s写失败!\n",path);
		return 1;
	}

	fprintf(fout,"%s",temp);			//将数组中的数据写入文件

	fclose(fout);					//文件写操作结束

	return 0;
}
2.编译
gcc encrypt.c -o a.out
3.实现及查看


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值