#include<stdio.h> #include<stdlib.h> #include<string.h> int dofile(char *in_fname,char *pwd,char *out_fname);/*对文件进行加密的具体函数*/ //简单文件异或加解密。 //xxx infile pwd outfile int main(int argc,char *argv[])/*定义main()函数的命令行参数*/ { char in_fname[1024];/*用户输入的要加密的文件名*/ char out_fname[1024]; char pwd[64];/*用来保存密码*/ if(argc!=4){/*容错处理*/ printf("\nIn-fname:\n"); gets(in_fname);/*得到要加密的文件名*/ printf("Password:\n"); gets(pwd);/*得到密码*/ printf("Out-file:\n"); gets(out_fname);/*得到加密后你要的文件名*/ }else{/*如果命令行参数正确,便直接运行程序*/ strcpy(in_fname,argv[1]); strcpy(pwd,argv[2]); strcpy(out_fname,argv[3]); } return dofile(in_fname,pwd,out_fname); } /*加密子函数开始*/ int dofile(char *in_fname,char *pwd,char *out_file) { FILE *fp1,*fp2; register char ch; int j=0; int j0=0; fp1=fopen(in_fname,"rb"); if(fp1==NULL){ printf("cannot open in-file.\n"); exit(1);/*如果不能打开要加密的文件,便退出程序*/ } fp2=fopen(out_file,"wb"); if(fp2==NULL){ printf("cannot open or create out-file.\n"); exit(1);/*如果不能建立加密后的文件,便退出*/ } //获取密码长度 while(pwd[++j0]); //printf("pwd_len=%d\n",j0); ch=fgetc(fp1); /*加密算法开始*/ while(!feof(fp1)){ //和密码每一位循环异或 fputc(ch^pwd[j>=j0?j=0:j++],fp2);/*异或后写入fp2文件*/ //printf("j=%d\n",j); ch=fgetc(fp1); } fclose(fp1);/*关闭源文件*/ fclose(fp2);/*关闭目标文件*/ return 0; }
#include <stdio.h> #include <errno.h> #define NORMAL_SIZ BUFSIZ #define CHMSK_KEY 0xa5 char buf[NORMAL_SIZ]; //simpleed.exe hehe1.txt | cat hehe1.txt 运行一次加密或者解密 int main(int argc, char **argv) { FILE *fp; int n; //printf("argc=%d\n",argc); if(argc!=2) { printf("%s file",argv[0]); return -1; } fp= fopen(argv[1], "rb+"); if(fp==NULL) { printf("%s\n",strerror(errno)); return -1; } while ((n = fread(buf, 1, NORMAL_SIZ, fp)) > 0) { int i; for (i = 0; i < n; i++) { buf[i] ^= CHMSK_KEY; } if (fseek(fp, -n, SEEK_CUR) == -1) { perror("fseek"); } n = fwrite(buf, 1, n, fp); fflush(fp);//如果不加这句,那么文件大小如果小于库函数缓冲(默认是BUFSIZ大小)时实际上面的写操作并没写进去,从而导致下一次read还是成功,因此程序不会退出 buf[0] = 0; } fclose(fp); return 0; }
#include <stdio.h> #include <stdlib.h> #define DWORD unsigned long #define BYTE unsigned char #define false 0 #define true 1 int main(int argc, char *argv[]) { FILE *hSource; FILE *hDestination; DWORD dwKey=0xfcba0000; char* pbBuffer; DWORD dwBufferLen=sizeof(DWORD); DWORD dwCount; DWORD dwData; char* szSource; char* szDestination; if(argv[1]==0||argv[2]==0) { printf("missing argument!\n"); return false; } szSource=argv[1]; szDestination=argv[2]; hSource = fopen(szSource,"rb");// 打开源文件. hDestination = fopen(szDestination,"wb"); //打开目标文件 if (hSource==NULL) {printf("open Source File error !"); return false ;} if (hDestination==NULL){ printf("open Destination File error !"); return false ;} //分配缓冲区 pbBuffer=(char* )malloc(dwBufferLen); do { // 从源文件中读出dwBlockLen个字节 dwCount = fread(pbBuffer, 1, dwBufferLen, hSource); //加密数据 dwData = *(DWORD*)pbBuffer; //char* TO dword dwData^=dwKey; //xor operation pbBuffer = (char *) &dwData; // 将加密过的数据写入目标文件 fwrite(pbBuffer, 1, dwCount, hDestination); } while(!feof(hSource)); //关闭文件、释放内存 fclose(hSource); fclose(hDestination); printf("%s is encrypted to %s\n",szSource,szDestination); return 0; }