原理:异或运算
加密:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define KEY ‘@‘ int main(int argc, char * argv[]){ FILE *fp; FILE *coded; char codedname[40]; char ch; if(argc < 2){ printf("param error!\n"); exit(1); } if((fp = fopen(argv[1], "rb")) == NULL){ printf("error when open file %s.\n", argv[1]); exit(1); } strcpy(codedname, argv[1]); strcat(codedname, ".coded"); if((coded = fopen(codedname, "a+b")) == NULL){ printf("error when open file %s.\n", codedname); exit(1); } while((ch = getc(fp)) != EOF){ char temp = ch^KEY; putc(temp, coded); } if(fclose(fp) != 0){ printf("error when close file %s.\n", argv[1]); exit(1); } if(fclose(coded) != 0){ printf("error when close file %s.\n", codedname); } return 0; }
解密:
#include<stdio.h> #include<stdlib.h> #include<string.h> int main(int argc, char * argv[]){ FILE *fp; FILE *decoded; char decodedname[40]; char ch; char key; const char *dot = "."; if(argc < 3){ printf("param error!\n"); exit(1); } if((fp = fopen(argv[1], "rb")) == NULL){ printf("error when open file %s.\n", argv[1]); exit(1); } strcpy(decodedname, strtok(argv[1], dot)); strcat(decodedname, "."); strcat(decodedname, strtok(NULL, dot)); key = argv[2][0]; if((decoded = fopen(decodedname, "a+b")) == NULL){ printf("error when open file %s.\n", decodedname); exit(1); } while((ch = getc(fp)) != EOF){ char temp = ch^key; putc(temp, decoded); } if(fclose(fp) != 0){ printf("error when close file %s.\n", argv[1]); exit(1); } if(fclose(decoded) != 0){ printf("error when close file %s.\n", decodedname); } return 0; }
时间: 2024-10-10 04:38:22