3种简单加解密程序,用于让文本文档不轻易被看出来是什么内容

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<stddef.h>

#define SRC_FILE "myfile"
#define ENCRYPT_FILE "enc_file"
#define DECRYPT_FILE "dec_file"
#define PASSWD  0x26


void t8(char* src,char* dst){
    printf("%s %d\n",__func__,__LINE__);
    FILE* fp=fopen(src,"r");
    if (!fp){
        perror("open src_file fail");
        return ;
    }

    FILE* fp2=fopen(dst,"w");
    if (!fp2){
        perror("open dst_file fail");
        return ;
    }

    char ch;
    while((ch=fgetc(fp))!=EOF){
        printf("%c",ch);
        //fputc(ch,fp2);
        fputc(ch^PASSWD,fp2);
    }

    fclose(fp);
    fp=NULL;
    fclose(fp2);
    fp2=NULL;
}


void t7(){
    printf("%d %d %d %d\n",2^3,9^3,2^3^3,9^3^3);
    printf("%d %d %d %d\n",2^9,8^9,2^9^9,8^9^9);
    printf("%d %d\n",1^3,10^3);
    //1 10 2 9
    //2 9
    char b='c';
    char passwd=0x26;
    printf("%d %d\n",b^passwd,b^passwd^passwd);
    char c=b^passwd;
    // 69 99
    printf("%c %d %d %d\n",c^passwd, c^passwd,69^passwd,b^passwd^passwd);
}

void t6(char* src,char* dst){
    FILE* fp=fopen(src,"r");
    if (!fp){
        perror("open src_file fail");
        return ;
    }

    FILE* fp2=fopen(dst,"w");
    if (!fp2){
        perror("open dst_file fail");
        return ;
    }

    char ch='b';
    printf("%c %x %8x",ch,ch,ch);
    fprintf(fp2,"%c %x %8x",ch,ch,ch);

    fclose(fp);
    fp=NULL;

    fclose(fp2);
    fp2=NULL;
}


void decrypt(char* src,char* dst){
    printf("%s %d\n",__func__,__LINE__);
    FILE* fp=fopen(src,"r");
    if (!fp){
        perror("open src_file fail");
        return ;
    }

    FILE* fp2=fopen(dst,"w");
    if (!fp2){
        perror("open dst_file fail");
        return ;
    }

    char ch;
    short b;
    while(!feof(fp)){
        fscanf(fp,"%hd",&b);
        if(feof(fp))
            break;
#if 0
        b<<=1;
        b>>=5;
#endif
        b>>=4;
        ch=(char)b;
        fprintf(fp2,"%c",ch);
        printf("b=%hd\n",b);
        printf("c=%c\n",ch);
    }

    fclose(fp);
    fp=NULL;
    fclose(fp2);
    fp2=NULL;
}


void encrypt(char* src,char* dst){
    printf("%s %d\n",__func__,__LINE__);
    FILE* fp=fopen(src,"r");
    if (!fp){
        perror("open src_file fail");
        return ;
    }

    FILE* fp2=fopen(dst,"w");
    if (!fp2){
        perror("open dst_file fail");
        return ;
    }

    char ch;
    short b;
    srand((unsigned int)time(NULL));
    while((ch=fgetc(fp))!=EOF){
        printf("ch=%c\n",ch);
        //fputc(ch,fp2);
        //fputc(~ch,fp2);
        b=(short)ch;
        b<<=4;
        b=b|0x8000;
        b+=rand()%16;
        fprintf(fp2,"%hd",b);
        printf("b=%hd\n",b);
    }

    fclose(fp);
    fp=NULL;
    fclose(fp2);
    fp2=NULL;
}


void t5(){
    char ch='b';
    printf("%c %x\n",ch,ch);
    short b=(short)ch;
    printf("%hd %x\n",b,b);
    short c=b<<4;
    printf("%hd %x\n",c,c);
    short d=c | 0x8000;
    printf("%hd %x\n",d,d);
    short e=d|0xc;
    printf("%hd %x\n",e,e);

    char* p0=&ch;
    short* p1=&b;
    short* p2=&c;
    short* p3=&d;
    short* p4=&e;

    short f=e<<1;
    printf("%hd %x\n",f,f);
    short g=f>>5;
    printf("%hd %x\n",g,g);
    char h=(char)g;
    printf("%c %x\n",h,h);
    short* p5=&f;
    short* p6=&g;
    char* p7=&h;
    printf("\n");
}


void t4(char* src,char* dst){
    printf("%s %d\n",__func__,__LINE__);
    FILE* fp=fopen(src,"r");
    if (!fp){
        perror("open src_file fail");
        return ;
    }

    FILE* fp2=fopen(dst,"w");
    if (!fp2){
        perror("open dst_file fail");
        return ;
    }

    char ch;
    while((ch=fgetc(fp))!=EOF){
        printf("%c",ch);
        //fputc(ch,fp2);
        fputc(~ch,fp2);
    }

    fclose(fp);
    fp=NULL;
    fclose(fp2);
    fp2=NULL;
}


void fgetc_test(FILE* fp){
    int ch;
#if 0
    while((ch=fgetc(fp))!=EOF){
        //while((ch=fgetc(fp))!='\n')
        printf("%c\n",ch);
    }
#else
    while(!feof(fp)){
        ch=fgetc(fp);

        if (feof(fp))
            break;

        printf("%c\n",ch);
    }
#endif 
}


void fputc_test(char* p,FILE* fp){
    int i=0;
    int ret;
    for(;i<strlen(p);i++){
        ret=fputc(p[i],fp);
        printf("%c\n",ret);
    }
}


void t3(){
    char ch='a';
    printf("%c",ch);
    printf("%c",~ch);
    char ch2=~ch;
    printf("%c",ch2);
    printf("%c",~ch2);
    char ch3=~ch2;
    printf("%c",ch3);
    printf("%c",~ch3);
}


void t2(){
    FILE* fp=fopen("a","w");
    fputc_test("abi % & *cd",fp);

#if 0
    fputs("ab="\
            "12k",fp);
#endif

    fclose(fp);
    fp=NULL;
}


void t1(){
    char* p="acb"
        "we";
    printf("%s\n",p);

    perror("ad");
    int ret=fputc('a',stdout);
    ret=fputs("abc",stdout);
    fprintf(stdout,"%d\n",ret);
    fputs("de\n",stdout);
    fprintf(stdout,"%d\n",ret);

    FILE* fp=fopen("a","r");
    fgetc_test(fp);
    fclose(fp);
    fp=NULL;
}


int main(){
    //t1();
    //t2();
    //t3();
#if 0
    t4(SRC_FILE,ENCRYPT_FILE);
    t4(ENCRYPT_FILE,DECRYPT_FILE);
#endif
    //t5();
    //t6(ENCRYPT_FILE,DECRYPT_FILE);
    //t7();
#if 0
    t8(SRC_FILE,ENCRYPT_FILE);
    t8(ENCRYPT_FILE,DECRYPT_FILE);
#endif

#if 0
    encrypt(SRC_FILE,ENCRYPT_FILE);
    decrypt(ENCRYPT_FILE,DECRYPT_FILE);
#endif

#if 1
    encrypt(SRC_FILE,"1st_enc");
    t4("1st_enc","2st_enc");
    t8("2st_enc",ENCRYPT_FILE);
    t8(ENCRYPT_FILE,"1st_dec");
    t4("1st_dec","2st_dec");
    decrypt("2st_dec",DECRYPT_FILE);
#endif
    return 0;
}

  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值