NDK05_C语言文件简单加密

NDK开发汇总

一 文本文件加解密

void encode(char normal_path[], char encode_path[]) {
	FILE * normal_fp = fopen(normal_path, "r");
	FILE * encode_fp = fopen(encode_path, "w");

	int ch;
	while ((ch = fgetc(normal_fp)) != EOF) {
		//简单的加密方式 ^7
		fputc(ch ^ 7, encode_fp);
	}
	fclose(normal_fp);
	fclose(encode_fp);
}

int test5_1() {
	//自定义的文件路径
	char * normalPath = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\friends.txt";
	char * encodePath = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\friends_encode.txt";
	encode(normalPath, encodePath);
	system("pause");
	return 0;
}

运行前 friends.txt的文本内容:

我是要被读取然后加密的内容

运行后friends_encode.txt的文本内容:

烧屠窄都逼夕霞紧辉嫩裁幂馅

二 文件解密

注: ^7 加密 然后7解密(运算两次,结果为原值),加密解密要对应

void decode(char encode_path[], char decode_path[]) {
	FILE * encodeFp = fopen(encode_path, "r");
	FILE * decodeFp = fopen(decode_path, "w");

	int ch;
	while ((ch = fgetc(encodeFp)) != EOF)
	{
		fputc(ch ^ 7, decodeFp);
	}

	fclose(encodeFp);
	fclose(decodeFp);
}

int test5_1() {
	char * encodePath = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\friends_encode.txt";
	char * decodePath = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\friends_decode.txt";
	
	decode(encodePath, decodePath);
	
	return 0;
}

运行后的解密文件friends_decode.txt内容还原了

我是要被读取然后加密的内容

三 二进制文件的加解

//相比文本文件,FILE * 在获取的时候MODE后加b:"rb"与“wb”

/////////////////////////////////////////二进制文件的加解///////////////////////////////////////
void byteEncode(char normal_path[], char encode_path[], char * password) {
	FILE * normal_fp = fopen(normal_path, "rb");
	FILE * encode_fp = fopen(encode_path, "wb");

	int ch;
	int pwd_length = strlen(password);
	int i = 0;
	while ((ch = fgetc(normal_fp)) != EOF  )
	{
		//加密方式可以自己定义
		fputc(ch ^ password[i % pwd_length], encode_fp);
		// % 10001 防止 i 溢出
		i = (i++) % 10001;
	}

	fclose(normal_fp);
	fclose(encode_fp);

}

int test5_1() {
	char * normal_path = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\liuyan.png";
	char * encode_path = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\liuyan_encode.png";
	

	byteEncode(normal_path, encode_path, (char *)"123");
	return 0;
}

运行后图片内容不可见,图片大小不变

四 二进制文件的解密

注意:密码和三中一致,加解密方式对应

void byteDecode(char encode_path[], char decode_path[], char * password) {
	FILE * encode_fp = fopen(encode_path, "rb");
	FILE * decode_fp = fopen(decode_path, "wb");

	int ch;
	int pwd_legth = strlen(password);
	int i = 0;
	while ((ch = fgetc(encode_fp)) != EOF) {
		fputc(ch ^ password[i % pwd_legth], decode_fp);
		i = (i++) % 10001;
	}
	fclose(encode_fp);
	fclose(decode_fp);
}


int test5_1() {
	char * encode_path = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\liuyan_encode.png";
	char * decode_path = (char *)"C:\\Users\\PF0ZYBAJ\\Desktop\\files\\liuyan_decode.png";


	byteDecode(encode_path, decode_path, (char *)"123");
	return 0;
}

解密后图片正常显示

五 Demo

lsn05_1.cpp

发布了269 篇原创文章 · 获赞 123 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/baopengjian/article/details/104784495