博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
加密与解密;解压缩
阅读量:4116 次
发布时间:2019-05-25

本文共 6570 字,大约阅读时间需要 21 分钟。

//使用简化RLE压缩方式,输入一个文件名和方式,压缩或解压已压缩的文件。//加密或解密文件,秘钥//加密方案:使用秘钥对文件内容异或处理,然后对单字节,高四位颠倒 低四位取反。//解密方案:按照加密方案相反处理#include"compress.h"#include
#include
#include
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_Hvoid 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
#include
#include
#include
#include
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;}

 

转载地址:http://dlypi.baihongyu.com/

你可能感兴趣的文章
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Java8 HashMap集合解析
查看>>
自定义 select 下拉框 多选插件
查看>>
fastcgi_param 详解
查看>>
poj 1976 A Mini Locomotive (dp 二维01背包)
查看>>
MODULE_DEVICE_TABLE的理解
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
postgresql监控工具pgstatspack的安装及使用
查看>>
【JAVA数据结构】双向链表
查看>>
【JAVA数据结构】先进先出队列
查看>>
乘法逆元
查看>>
Objective-C 基础入门(一)
查看>>
Flutter Boost的router管理
查看>>
iOS开发支付集成之微信支付
查看>>
C++模板
查看>>
【C#】如何实现一个迭代器
查看>>
【C#】利用Conditional属性完成编译忽略
查看>>
DirectX11 光照演示示例Demo
查看>>
VUe+webpack构建单页router应用(一)
查看>>
Node.js-模块和包
查看>>