zoukankan      html  css  js  c++  java
  • C语言异或加解密文件函数

    #include<stdio.h>
    #include <sys/stat.h>
    #include<stdbool.h>
    char *Key="qwert789456-+";
    short EncodeFile(const  char * File_0,const char *File_E/*,char (*p) (void)*/) {
    	FILE *f1=fopen(File_0,"r+b"); //读方式打开一个二进制文件,只允许读数据
    	FILE *f2=fopen(File_E,"w+b") ;//写方式打开一个二进制文件,只允许写数据
    
    	unsigned long StrLong=GetrFileSizeK(File_0);
    
    	if(StrLong==0) {
    		fwrite("", sizeof(char), StrLong, f2);
    		fclose(f1);
    		fclose(f2);
    		return 1;//长度为0
    	}
    	if(StrLong>512*1024*1024) {
    		fclose(f1);
    		fclose(f2);
    		return 2;//文件大小太大
    	}
    
    	if(!StrLong||!f1||!f2) {
    		return -1;//无法打开
    	}
    	char *pDATA = (char *)malloc(StrLong*sizeof(char));
    
    	if(pDATA==NULL) {
    		return -2;//内存申请失败
    	}
    	fread(pDATA, sizeof(char), StrLong, f1);
    
    
    
    	for (unsigned long i = 0; i < StrLong; i++) {
    
    		pDATA[i]^=(GetRanDomKey());
    
    	}
    	fwrite(pDATA, sizeof(char), StrLong, f2);
    	fputc(10^(int)GetRanDomKey(),f2);
    	free(pDATA);
    	fclose(f1);
    	fclose(f2);
    	return 0;
    }
    
    
    
    short DecodeFile(const  char * File_0,const char *File_E/*,char (*p) (void)*/) {
    	FILE *f1=fopen(File_0,"r+b"); //读方式打开一个二进制文件,只允许读数据
    	FILE *f2=fopen(File_E,"w+b") ;//写方式打开一个二进制文件,只允许写数据
    	unsigned long StrLong=GetrFileSizeK(File_0);
    
    	if(!StrLong||!f1||!f2) {
    		remove(File_E);
    		return -1;//无法打开文件
    	}
    
    	if(StrLong==0) {
    
    		return 1;//文件长度为0
    	}
    	if(StrLong>512*1024*1024) {
    		fclose(f1);
    		fclose(f2);
    		remove(File_E);
    		return -3;//文件太大
    	}
    
    	char *pDATA = (char *)malloc(StrLong*sizeof(char));
    
    	if(pDATA==NULL) {
    		fclose(f1);
    		fclose(f2);
    		remove(File_E);
    		return -2;//内存申请失败
    	}
    	fread(pDATA, sizeof(char), StrLong, f1);
    	unsigned long i;
    	for (i = 0; i < StrLong; i++) {
    
    		pDATA[i]^=(GetRanDomKey());
    	}
    
    	short code;
    	if((int)(pDATA[i-1])==10) {
    		fwrite(pDATA, sizeof(char), StrLong-1, f2);
    		code=0;//解密成功
    
    	} else {
    		remove(File_E);
    		code=-4;//解密失败
    	}
    
    	free(pDATA);
    	fclose(f1);
    	fclose(f2);
    	return code;
    }
    
    
    
    unsigned long GetrFileSizeK(const char *path) {
    //获取文件名为path的文件大小。
    	struct stat statbuf;
    	stat(path,&statbuf);
    	unsigned long size=statbuf.st_size;
    	return size;
    
    }
    char GetRanDomKey() {
    	static unsigned short con; //密钥长度不会很长
    	++con;
    	if(con==(strlen(Key)+1)) {
    		con=0;
    	}
    	return	Key[con];
    }
    

    完整工程在github
    https://github.com/znkzz/File-Encryption

  • 相关阅读:
    python 的基础 学习 第六天 基础数据类型的操作方法 字典
    python 的基础 学习 第五天 基础数据类型的操作方法
    python 的基础 学习 第四天 基础数据类型
    ASP.NET MVC 入门8、ModelState与数据验证
    ASP.NET MVC 入门7、Hellper与数据的提交与绑定
    ASP.NET MVC 入门6、TempData
    ASP.NET MVC 入门5、View与ViewData
    ASP.NET MVC 入门4、Controller与Action
    ASP.NET MVC 入门3、Routing
    ASP.NET MVC 入门2、项目的目录结构与核心的DLL
  • 原文地址:https://www.cnblogs.com/obj-a/p/xor.html
Copyright © 2011-2022 走看看