文件加密——可执行,较复杂的抽取(限文本)

一个简单的文件加密程序、、、还蛮有意思的用C语言写的。

最后一个参数 是当前字符加上一个输入者指定的数字,构成一个新的字符。

效果图:

参数输入方式:

#include <stdio.h>

#pragma warning(disable:4996)
#define MYKEY 6

void readFile(const FILE *);
void writeFile(FILE *,const char *);
//void codeFile(const FILE *, FILE *);
void codeFile(const FILE *, FILE *,const int);
//void decodeFile(const FILE *, FILE *);
void decodeFile(const FILE *, FILE *,const int);
//void code(char *);
void code(char *,const int );
//void decode(char *);
void decode(char *,const int);
void workflow(const int ,const char **);

typedef void myCodingFunction(char *, const int);

void myFileCoding(const FILE *,FILE *,const int,void *(char *,const int));
void myFileCoding(const FILE *, FILE *, const int, myCodingFunction);


int main(int argc,char ** argv){

    workflow(argc,argv);

    //FILE *pf = fopen("code1.txt", "r");
    //FILE *pfw = fopen("decode1.txt", "w");

    //FILE *pfw = fopen("a.txt", "a+");
    //readFile(pf);
    
    //decodeFile(pf, pfw);
    //writeFile(pfw,"lifei
");
    //writeFile(pfw, "come on!
");
    //fclose(pf);
    //fclose(pfw);

    //fclose(pfw);
    getchar();

    return 0;

}

void myFileCoding(const FILE *src, FILE *dest, const int key, myCodingFunction func){

    char buf[1024] = { 0 };
    while (!(feof(src))){
        fgets(buf, sizeof(buf), src);
        func(buf, key);
        fputs(buf, dest);
    }

}

void decode(char *src, const int mykey){
    int len = strlen(src);
    int i = 0;
    for (; i < len; i++){
        src[i] -= mykey;
    }
}

//有了myFileCoding 这个 函数就没有用了
void decodeFile(const FILE *src, FILE *dest, const int mykey){
    char buf[1024] = { 0 };
    while (!(feof(src))){
        fgets(buf, sizeof(buf), src);
        decode(buf, mykey);
        fputs(buf, dest);
    }

}
void code(char *src, const int mykey){
    int len = strlen(src);
    int i = 0;
    for (; i < len; i++){
        src[i] += mykey;
    }
}

//有了myFileCoding 这个 函数就没有用了
void codeFile(const FILE *src, FILE *dest, const int mykey){
    char buf[1024] = { 0 };
    while (!(feof(src))){
        fgets(buf, sizeof(buf), src);
        code(buf,mykey);
        fputs(buf, dest);
    }
}

void workflow(const int argc,const char ** argv){
    if (argc < 5){
        printf("参数过少,需要五个参数
");
    }
    else if (argc>5){
        printf("参数过多,需要五个参数
");
    }
    else {
        if (strcmp(argv[1], "code") == 0){
            FILE *src = fopen(argv[2], "r");
            FILE *dest = fopen(argv[3], "w");
            int key = atoi(argv[4]);
            //codeFile(src, dest, key);

            myCodingFunction *function = &code;
            myFileCoding(src, dest, key, function);

            printf("加密成功,秘钥是:%d", key);


            fclose(src);
            fclose(dest);
        }
        else if (strcmp(argv[1], "decode") == 0){
            FILE *src = fopen(argv[2], "r");
            FILE *dest = fopen(argv[3], "w");
            int key = atoi(argv[4]);
            //decodeFile(src, dest, key);

            myCodingFunction *function = &decode;
            myFileCoding(src, dest, key, function);

            printf("反解码完成(请确保输入了正确的秘钥)");

            fclose(src);
            fclose(dest);
        }
        else{
            printf("请输入参数 code 或者 decode
");
        }

    }


}


//竟然不允许重载。。。笑哭。。。
//传个函数进来吧!!!编码也自己传好不好!!!明天实现一下!!!
//void decodeFile(const FILE *src, FILE *dest){
//
//    char buf[1024] = { 0 };
//    while (!(feof(src))){
//        fgets(buf, sizeof(buf), src);
//        decode(buf);
//        fputs(buf, dest);
//    }
//}

//void decode(char *src){
//
//    int len = strlen(src);
//    int i = 0;
//    for (; i < len; i++){
//        src[i] -= MYKEY;
//    }
//}

//void code(char *src){
//    
//    int len = strlen(src);
//    int i = 0;
//    for (; i < len; i++){
//        src[i] += MYKEY;
//    }
//}

//void codeFile(const FILE *src, FILE *dest){
//    char buf[1024] = { 0 };
//    while (!(feof(src))){
//        fgets(buf, sizeof(buf), src);
//        code(buf);
//        fputs(buf,dest);
//    }
//}
void readFile(const FILE *src){
    char buf[1024] = { 0 };
    while (!(feof(src))){
        fgets(buf, sizeof(buf), src);
        printf(buf);
    }
}

//追加功能,有问题,不要使用,还是要自己写一个文本文件,然后再进行相关操作。
void writeFile(FILE *file,const char * str){ fputs(str, file); char buf[50] = { 0 }; sprintf(buf, "字符串 : %s 追加成功 ", str); printf(buf); }
原文地址:https://www.cnblogs.com/letben/p/5246111.html