#include <stdio.h> #include <stdlib.h>
int main(int argc,char *argv[]) { FILE *fp1,*fp2; char c,ch; long j; if(argc != 3) exit(0); fp1 = fopen(argv[1],"rb");/*打开文件供读*/ fp2 = fopen(argv[2],"wb");/*打开文件供写*/ if((fp1 == NULL)||(fp2 == NULL)) { printf("cannot open file\n"); exit(1); } printf("Pls input your password:"); scanf("%i",&j); srand(j); /*初始化随机数发生器*/ ch = fgetc(fp1); /*ch为文件字符流*/ while(!feof(fp1)) { c = rand(); /*c为随机字符流*/ ch = ch ^ c; /*异或加密*/ fputc(ch,fp2); ch = fgetc(fp1); } fclose(fp1); fclose(fp2); return 0; }
程序操作流程如下: Microsoft(R) Windows 98 (C)Copyright Microsoft Corp 1981-1999.
C:\WINDOWS>cd desktop
C:\WINDOWS\Desktop>bb.exe in.txt out.txt Pls input your password:123456
C:\WINDOWS\Desktop>bb.exe out.txt in.txt Pls input your password:123456
原理: 文件的加密解密进行的都是异或运算 解密的过程是加密的逆操作(见流程和如下公式) (A xor B) xor B = A