按照密码加密文件

本文介绍了如何利用C语言编写程序来对文件进行加密处理,确保数据的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//字符串按照密码加密
char *stringJiami(char *password, char *string);//字符串加密
char *stringJiemi(char *password, char *string);//字符串解密

void fileJiami(char *path, char *pathjia, char *password);
void fileJiami(char *pathjia, char *pathjie, char *password);

源文件

#include"密码加密.h"

int getfilesize(char *path)
{
	FILE *pf = fopen(path, "r");
	if (pf == NULL)
	{
		return -1;
	}
	else
	{
		fseek(pf, 0, SEEK_END);//把指针移动到文件末尾
		int length = ftell(pf);//指针到开头的长度
		return length;
	}
}

char *stringJiami(char *password, char *string)
{
	int passLength = strlen(password);//密码长度
	int stringLength = strlen(string);//字符串长度
	if (stringLength%passLength == 0)
	{
		int ci = stringLength / passLength;//要循环的次数
		for (int i = 0; i < ci; i++)//循环次
		{
			for (int j = 0; j < passLength; j++)//循环密码
			{
				string[passLength*i + j] ^= password[j];//异或加密
			}
		}
	}
	else
	{
		int ci = stringLength / passLength;//要循环的次数
		for (int i = 0; i < ci; i++)
		{
			for (int j = 0; j < passLength; j++)
			{
				string[passLength*i + j] ^= password[j];
			}
		}
		int lastLength = stringLength%passLength;//剩下的长度
		for (int i = 0; i < lastLength; i++)
		{
			string[passLength*ci + i] ^= password[i];//剩下的字节进行加密
		}
	}
	return string;
}

char *stringJiemi(char *password, char *jiastring)
{
	int passLength = strlen(password);
	int stringLength = strlen(jiastring);
	if (stringLength%passLength == 0)
	{
		int ci = stringLength / passLength;
		for (int i = 0; i < ci; i++)
		{
			for (int j = 0; j < passLength; j++)
			{
				jiastring[passLength*i + j] ^= password[j];
			}
		}
	}
	else
	{
		int ci = stringLength / passLength;
		for (int i = 0; i < ci; i++)
		{
			for (int j = 0; j < passLength; j++)
			{
				jiastring[passLength*i + j] ^= password[j];
			}
		}
		int lastLength = stringLength%passLength;
		for (int i = 0; i < lastLength; i++)
		{
			jiastring[passLength*ci + i] ^= password[i];
		}
	}
	return jiastring;
}

void fileJiami(char *path, char *pathjia, char *password)
{
	FILE *pfr, *pfw;
	pfr = fopen(path, "r");
	pfw = fopen(pathjia, "w");
	if(pfr==NULL||pfw==NULL)
	{
		fclose(pfr);
		fclose(pfw);
		return;
	}
	else
	{
		int stringLength = getfilesize(path);
		char *newstr = (char*)malloc(sizeof(char)*(stringLength + 1));
		for (int i = 0; i < stringLength; i++)
		{
			char ch = fgetc(pfr);
			newstr[i] = ch;
		}
		newstr[stringLength] = '\0';
		printf("%s", newstr);
		stringJiami(password, newstr);
		for (int i = 0; i < stringLength; i++)
		{
			fputc(newstr[i], pfw);
		}
		fclose(pfr);
		fclose(pfw);
	}
}

void fileJjiemi(char *pathjia, char *pathjie, char *password)
{
	FILE *pfr, *pfw;
	pfr = fopen(pathjia, "rb");
	pfw = fopen(pathjie, "wb");
	if (pfr == NULL || pfw == NULL)
	{
		fclose(pfr);
		fclose(pfw);
		return;
	}
	else
	{
		//不对
		/*int stringlength = strlen(pathjia);
		char *newstr = (char*)malloc(sizeof(char)*(stringlength + 1));
		for (int i = 0; i < stringlength; i++)
		{
			char ch = fgetc(pfr);
			newstr[i] = ch;
		}
		newstr[stringlength] = '\0';
		printf("\n\n%s", newstr);
		stringJiemi(password, newstr);
		for (int i = 0; i < stringlength; i++)
		{
			fputc(newstr[i], pfw);
		}
		fclose(pfr);
		fclose(pfw);*/


		while (!feof(pfr))
		{
			char string[256] = { 0 };
			fgets(string, 256, pfr);
			stringJiemi(password, string);//加密啊
			fputs(string, pfw);//写入jie密文件

		}
		fclose(pfr);
		fclose(pfw);//关闭文件
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值