文件加解密(二)——使用密码
2015-08-03 10:48
281 查看
#define _CRT_SECURE_NO_WARNINGS #define SRC_PATH "C:\\Users\\michael\\Desktop\\yzh.txt" #define CODE_PATH "C:\\Users\\michael\\Desktop\\yzh_code.txt" #define DECODE_PATH "C:\\Users\\michael\\Desktop\\yzh_decode.txt" #include <stdio.h> #include <stdlib.h> #include <string.h> int getFileSize_C(char * file) { int size = -1; FILE * path; path = fopen(file, "r"); if (NULL == path) { printf("文件打开失败!\n"); return size; //文件大小不可能为负数 } else { //设置流文件指针的位置,以SEEK_END为起点,偏移量是0,亦即SEEK_END fseek(path, 0, SEEK_END); //函数结果:当前文件流指针位置相对于文件起始位置的字节偏移量 size = ftell(path); fclose(path); path = NULL; } return size; } void code_decode_file_with_psw(char* path, char* newpath,char* psw) { FILE* pRead = fopen(path, "r"); FILE* pWrite = fopen(newpath, "w"); if (pRead == NULL || pWrite == NULL) { return; } else { int ch = 0; int fileSize = getFileSize_C(path); int pswLength = strlen(psw); for (int i = 0; i < fileSize / pswLength; i++) { for (int j = 0; j < pswLength; j++) { ch = fgetc(pRead); fputc(ch^psw[j], pWrite); } } for (int k = 0; k <fileSize % pswLength; k++) { ch = fgetc(pRead); fputc(ch^psw[k], pWrite); } } fclose(pRead); fclose(pWrite); pRead = NULL; pWrite = NULL; } void main() { code_decode_file_with_psw(SRC_PATH, CODE_PATH,"yzh"); code_decode_file_with_psw(CODE_PATH, DECODE_PATH,"yzh"); system("pause"); }
说明:文本文件使用密码加密时,不能使用r,w方式打开文件。应该使用rb,wb。
相关文章推荐
- LeetCode Valid Anagram
- Sizeof与Strlen的区别与联系
- LeetCode Valid Anagram
- 快速取模
- POJ 2486-Apple Tree(树状背包)
- 谷歌面经 Tree Serialization
- 如何判断ox系统版本
- 仿QQ侧滑 添加事件下发
- 浅析Ruby的源代码布局及其编程风格
- 程序员几个重要的网站
- java中的基本数据类型一定存储在栈中的吗?
- UVALive 6322 最大匹配...
- java程序设计线程池(newCachedThreadPool())
- 解决 jquery dialog 弹框destroy销毁方法不能把弹出元素设置成初始状态
- 解决cxf+spring发布的webservice,types,portType和message以import方式导入
- spss 数据库配置(mysql)
- 南邮 OJ 1028 Digital Roots
- linux下磁盘管理命令汇总
- 一些设计上的基本常识
- TeXstudio中如何编译eps格式的图片