文件加密解密小程序
2013-10-23 19:20
344 查看
#include <stdio.h> #include <stdlib.h> #include <string.h> int process(char *filename, char *opration); int read_file(char *filename, char **buf); int write_file(char *buf, long file_size); int decode(char *buf, int file_size, int key); int encrypt(char *buf, int file_size, int key); int main(int argc, char **argv) { char flag; if (argc != 3) { printf("error!the type is :%s <filename> <opration>\n", argv[0]); exit(-1); } if ((*argv[2] == 'e') || (*argv[2] == 'd')) process(argv[1], argv[2]); else { printf("error:opration only be e(encrypt) or d(decode)\n"); exit(-1); } return 0; } int process(char *filename, char *opration) { FILE *myfile_dst; char *buf; int key = 123; long file_size; file_size = read_file(filename, &buf); if (*opration == 'e') encrypt(buf, file_size, key); else if(*opration == 'd') decode(buf, file_size, key); write_file(buf, file_size); return 0; } int read_file(char *filename, char **buf) { FILE *myfile_src; long file_size; if (!(myfile_src = fopen(filename, "r"))) { perror("fopen"); } fseek(myfile_src, 0, SEEK_END); file_size = ftell(myfile_src); fseek(myfile_src, 0, SEEK_SET); *buf = (char *)malloc(file_size); fread(*buf, 1, file_size, myfile_src); fclose(myfile_src); return file_size; } int write_file(char *buf, long file_size) { FILE *myfile_dst; char filename[20]; printf("please input the file name that you want to save:\n"); scanf("%s", filename); if (!(myfile_dst = fopen(filename, "wb"))) { perror("fopen"); } fwrite(buf, 1, file_size, myfile_dst); printf(">>>>>>>>>>>>>>%s is save<<<<<<<<<<<<<<<\n", filename); fclose(myfile_dst); return 0; } int encrypt(char buf[], int file_size, int key) { int i; for(i = 0; i < file_size; i ++) buf[i] = buf[i] + key; return 0; } int decode(char *buf, int file_size, int key) { int i; for(i = 0; i < file_size; i ++) buf[i] = buf[i] - key; return 0; }
非常简单的文件加密解密,有很多缺陷需要修改,以后慢慢增加
相关文章推荐
- 实现对文件进行加密,解密程序代码
- Java批量加密解密文件简单小程序
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- 文件加密解密程序
- 『原创』c#实现文件加密、解密及文件拖拽至程序图标直接打开
- c#实现文件加密、解密及文件拖拽至程序图标直接打开(转)
- c#实现文件加密、解密及文件拖拽至程序图标直接打开
- linux 加密解密文件小程序
- 一个简单的C++加密,解密文件程序
- 一个简单的加密和解密程序——凯撒密码
- C# 文件加密解密 SHA256
- java base64加密文件 及解密到文件
- 某壳对.Net程序加密的原理及解密探讨二
- Java 加密解密zip文件(亲测有效,附源码和jar包)
- vc++网络安全编程范例(17)-open ssl 实现文件加密与解密
- VS2010下Unicode的Base64加密解密程序 [
- 使用 OpenSSL 对文件进行加密和解密
- 如何使用 Visual C# 加密和解密文件
- 模拟文件的加密与解密
- Java 加密解密zip文件