C语言之文件的加密与解密

#include
#include

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;
	}
}

void copy(char *oldpath, char *newpath)
{
	FILE *pfr, *pfw;
	pfr = fopen(oldpath, "rb");
	pfw = fopen(newpath, "wb");
	if (pfr == NULL || pfw == NULL)
	{
		fclose(pfr);
		fclose(pfw);
		return;
	}
	else
	{
		int length = getfilesize(oldpath);
		char *p = (char *)malloc(length*sizeof(char));
		fread(p, sizeof(char), length, pfr);
		fwrite(p, sizeof(char), length, pfw);
		fclose(pfr);
		fclose(pfw);
	}
}

void jia(char *oldpath, char *newpath)
{
	FILE *pfr, *pfw;
	pfr = fopen(oldpath, "rb");
	pfw = fopen(newpath, "wb");
	if (pfr == NULL || pfw == NULL)
	{
		fclose(pfr);
		fclose(pfw);
		return;
	}
	else
	{
		int length = getfilesize(oldpath);
		char *p = (char *)malloc(length*sizeof(char));
		fread(p, sizeof(char), length, pfr);
		for (int i = 0; i < length; i++)
		{
			p[i] ^= 'A';
		}
		fwrite(p, sizeof(char), length, pfw);
		fclose(pfr);
		fclose(pfw);
	}
}

void jie(char *oldpath, char *newpath)
{
	FILE *pfr, *pfw;
	pfr = fopen(oldpath, "rb");
	pfw = fopen(newpath, "wb");
	if (pfr == NULL || pfw == NULL)
	{
		fclose(pfr);
		fclose(pfw);
		return;
	}
	else
	{
		int length = getfilesize(oldpath);
		char *p = (char *)malloc(length*sizeof(char));
		fread(p, sizeof(char), length, pfr);
		for (int i = 0; i < length; i++)
		{
			p[i] ^= 'A';
		}
		fwrite(p, sizeof(char), length, pfw);
		fclose(pfr);
		fclose(pfw);
	}
}

void main()
{
	char *oldpath = "C:\\Users\\Administrator\\Desktop\\MathType6.9.exe";
	char *newpath = "C:\\Users\\Administrator\\Desktop\\MathType6.9a.exe";
	char *newjiepath = "C:\\Users\\Administrator\\Desktop\\MathType6.9b.exe";
	jia(oldpath, newpath);
	jie(newpath, newjiepath);
	system("pause");
}

你可能感兴趣的:(C/C++)