加密与解密;解压缩

//使用简化RLE压缩方式,输入一个文件名和方式,压缩或解压已压缩的文件。

//加密或解密文件,秘钥
//加密方案:使用秘钥对文件内容异或处理,然后对单字节,高四位颠倒 低四位取反。
//解密方案:按照加密方案相反处理
#include"compress.h"
#include<stdio.h>
#include<string.h>
#include <windows.h>


int main(int argc, char* argv[])
{

	char* buf = NULL;
	char* tem = NULL;
	char* new_buf = NULL;

	FILE* fp = NULL;
	if (argc != 5)
	{
		printf("Usage:%s filemane\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	if (NULL == (fp = fopen(argv[1], "rb")))
	{
		fprintf(stderr, "Can't open %s\n", argv[1]);
		exit(EXIT_FAILURE);
	}
	fseek(fp, 0L, SEEK_END);
	int size = ftell(fp);

	buf = (char*)malloc(size + 1);
	memset(buf, 0, size + 1);
	tem = (char*)malloc(size * 2 + 1);
	memset(tem, 0, size * 2 + 1);
	new_buf = (char*)malloc(size * 2 + 1);
	memset(new_buf, 0, size * 2 + 1);

	char *file_name = argv[1];
	if (NULL == (fp = fopen(file_name, "rb")))
	{
		fprintf(stderr, "can't open %s\n", file_name);
		exit(EXIT_FAILURE);
	}
	fread(buf, sizeof(char), size, fp);

	printf("select the method of compress:%s\n", argv[2]);
	compress(buf, tem, size);
	uncompress(tem, new_buf);


	//加密
	int n = 0;
	char* encrypte_buf = NULL;
	char* decrypte_buf = NULL;
	char* str_key = argv[3];

	encrypte_buf = (char*)malloc(size * 5 + 1);
	memset(encrypte_buf, 0, size * 5 + 1);
	decrypte_buf = (char*)malloc(size * 5 + 1);
	memset(decrypte_buf, 0, size * 5 + 1);

	printf("select the method<1 is encrypte;2 isdecrypte>:");
	scanf_s("%d", &n);
	switch (n)
	{
	case 1:
		printf("select the method is encrypte_code\n");
		encrypte_code(buf, str_key, encrypte_buf, size);
		break;
	case 2:
		printf("select the method is decrypte_code\n");
		char *efile_name = argv[4];
		if (NULL == (fp = fopen(efile_name, "rb")))
		{
			fprintf(stderr, "can't open %s\n", efile_name);
			exit(EXIT_FAILURE);
		}
		fread(encrypte_buf, sizeof(char), size, fp);
		decrypte_code(encrypte_buf, str_key, decrypte_buf);
		break;
	case 3: 
		break;

	}

	if (fp != NULL)
	{
		fclose(fp);
	}
	free(buf);
	free(tem);
	free(new_buf);
	free(encrypte_buf);
	free(decrypte_buf);
	system("pause");
	return 0;
}
#ifndef COMPRESS_H
#define COMPRESS_H


void compress(const char* buf, char* tem, int size);
void uncompress(char* tem, char* new_buf);

void encrypte_code(char* buf, char* str_key, char* encrypte_buf, int size);
void decrypte_code(char* buf, char* str_key, char* decrypte_buf);

#endif
#include"compress.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <windows.h>
#include <ctype.h>

void compress(const char* buf, char* tem, int size)
{
	const char* buffer = buf;
	int count = 1;//统计字符数
	int index = 0;//压缩后存的下标
	int flag = 3;//标志控制字符的次数
	FILE* out = NULL;

	int i = 0;
	while (i < size)
	{
		count = 1;
		if (isalpha(buffer[i]))
		{
			while (buffer[i] == buffer[i + 1])
				//while (*buffer == *(buffer + 1))
			{
				++count;
				++i;

			}
			tem[index++] = buffer[i];
			tem[index++] = count + 48;
		}
		//当字符是数字时,标志控制字符为0,连续打印两次,如果有连续的数字字符就用0标志
		else if (isdigit(buffer[i]))
		{
			while (buffer[i] == buffer[i + 1])
			{
				++count;
				++i;

			}
			if (('0' == buffer[i]) && (1==count))
			{
				while (flag)
				{
					tem[index++] = '0';
					--flag;
				}
			}
			else if (1 == count)
			{
				tem[index++] = buffer[i];

			}
			else
			{
				tem[index++] = '0';
				tem[index++] = buffer[i];
				tem[index++] = count + 48;
			}
		}
		++i;
	}
	char compress_file_name[] = "compress.txt";
	if (NULL == (out = fopen(compress_file_name, "wb+")))
	{
		fprintf(stderr, "Can't open %s\n", compress_file_name);
		exit(EXIT_FAILURE);
	}
	fwrite(tem, sizeof(char), strlen(tem), out);
	fclose(out);
	return;
}

void uncompress(char* tem, char* new_buf)
{
	FILE* out = NULL;
	int len = strlen(tem);
	int index = 0;//解压后new_buf的下标
	char ch = 0;//当前字符
	int i = 0;

	while (i < len)
	{
		if (isalpha(tem[i]))
		{
			ch = tem[i + 1];
			while ('0' != ch)
			{
				new_buf[index++] = tem[i];
				ch = ch - 1;
			}
			++i;
		}
		else if (('0' == tem[i]))
		{

			if (('0' == tem[i + 1]) && ('0' == tem[i + 2]))
			{
				new_buf[index++] = '0';
				i += 2;
			}
			else
			{
				ch = tem[i + 2];
				while ('0' != ch)
				{
					new_buf[index++] = tem[i + 1];
					ch = ch - 1;
				}
				i += 2;
			}
		}
		else
		{
			new_buf[index++] = tem[i];
		}
		++i;
	}
	char uncompress_file_name[] = "uncompress.txt";
	if (NULL == (out = fopen(uncompress_file_name, "wb+")))
	{
		fprintf(stderr, "Can't open %s\n", uncompress_file_name);
		exit(EXIT_FAILURE);
	}
	fwrite(new_buf, sizeof(char), strlen(new_buf), out);
	fclose(out);
	return;
}


char reverse_byte(unsigned char value)
{
	char ret = 0x0;
	int i_byte = 0;
	char high_byte = (value & 0xF0) >> 4;
	while (i_byte < 4)
	{
		char temp = high_byte & 0x01;
		high_byte = high_byte >> 1;
		ret = (ret << 1) | temp;
		++i_byte;
	}
	ret = ret << 4;
	ret |= (~value) & 0x0F;
	return ret;
}

void encrypte_code(char* buf, char* str_key, char* encrypte_buf, int size)
{
	int i = 0;
	int index = 0;
	int index_buf = 0;
	char template[32] = { 0 };//异或后存入空间
	int len = strlen(str_key);
	char high_byte = 0;
	char low_byte = 0;
	int high_byte_buf[32] = { 0 };
	for (i = 0; i < size; ++i)
	{
		memset(high_byte_buf, 0, 32);
		template[index] = str_key[0] ^ (buf[i]);
		for (int j = 1; j < len; ++j)
		{
			template[index] ^= str_key[j];

		}
		//获取高四位颠倒,低四位取反
		encrypte_buf[index_buf] = reverse_byte(template[index]);

#if 0
		//获取高四位,颠倒
		high_byte = (template[index] & 0xf0) >> 4;

		int bit = 0x1;
		high_byte_buf[0] = high_byte&bit;
		for (int i = 1; i < 4; ++i)
		{
			high_byte = high_byte >> 1;
			high_byte_buf[i] =high_byte&bit;
		}
		high_byte = ((high_byte_buf[0] & 0xff) << 7) | ((high_byte_buf[1] & 0xff) << 6)
			| ((high_byte_buf[2] & 0xff) << 5) | ((high_byte_buf[3] & 0xff) << 4);

		//获取低四位,取反
		low_byte = template[index] & 0x0f;
		low_byte = low_byte ^ 0xf;
		encrypte_buf[index_buf] = high_byte | low_byte;
#endif

		++index_buf;
		++index;
	}
	FILE* out = NULL;
	char encryption_file_name[] = "encryption.txt";
	if (NULL == (out = fopen(encryption_file_name, "wb+")))
	{
		fprintf(stderr, "Can't open %s\n", encryption_file_name);
		exit(EXIT_FAILURE);
	}
	fwrite(encrypte_buf, sizeof(char), strlen(encrypte_buf), out);
	fclose(out);
	return;
}


void decrypte_code(char* encrypte_buf, char* str_key, char* decrypte_buf)
{
	int i = 0;
	int index_buf = 0;

	int len = strlen(str_key);
	char high_byte = 0;
	char low_byte = 0;
	int l = strlen(encrypte_buf);
	for (i = 0; i < strlen(encrypte_buf); i++)
	{	
		
		decrypte_buf[index_buf] = reverse_byte(encrypte_buf[i]);
		for (int j = 0; j < len; ++j)
		{
			decrypte_buf[index_buf] ^= str_key[j];

		}
		++index_buf;
	}


	FILE* out = NULL;
	char decryption_file_name[] = "decryption.txt";
	if (NULL == (out = fopen(decryption_file_name, "wb+")))
	{
		fprintf(stderr, "Can't open %s\n", decryption_file_name);
		exit(EXIT_FAILURE);
	}
	fwrite(decrypte_buf, sizeof(char), strlen(decrypte_buf), out);
	fclose(out);
	return;


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值