zoukankan      html  css  js  c++  java
  • linux 加密解密文件小程序

    代码见下面,编译之后就可以用;建议放在bash下,或者添加环境变量。

    使用方法:encrypt <文件名>。两次输入密码。加密密码与解密密码不一致解码后就不是原文件了!

    #include <stdio.h>   
    #include <stdlib.h>   
    #include <string.h>   
    
    int decrypt(FILE *in,FILE *out);  
    int encrypt(FILE *in,FILE *out);  
    int passlen;
    char pass[105],passa[105];
    
    int main(int argc,char **argv)  
    {  
        if(argc != 2)  
        {  
            printf("
    请输入文件名进行加密或者解密!
    ");  
            printf("    encrypt <inputfile>
    
    ");  
            exit(0);  
        }  
        while(1)
        {
            printf("Enter password:");
            system("stty -icanon");
            system("stty -echo");   
            scanf("%s",pass);
            system("stty icanon");
            system("stty echo");
            printf("
    Enter password again:");
            system("stty -icanon");
            system("stty -echo");   
            scanf("%s",passa);
            system("stty icanon");
            system("stty echo");
            if(strcmp(pass,passa)==0) break;
            printf("
    Password is inconsistent
    ");
        }
        printf("
    ");
        FILE *in = fopen(argv[1],"rb");  
        FILE *out = fopen("encrypt-tmp","w");  
        passlen=strlen(pass);
        char code=0xAA;
        for(int i=0;i<passlen;i++) pass[i]=pass[i]^code;
        if(in == NULL || out == NULL)  
        {  
            fprintf(stderr,"%s
    ","open file error!");  
            exit(1);  
        }  
        encrypt(in,out);  
        fclose(in);  
        fclose(out);
        remove(argv[1]);
        rename("encrypt-tmp",argv[1]);
        return 0;  
    }  
    int encrypt(FILE *in,FILE *out)  
    {  
        if(in == NULL || out == NULL)  
        {  
            fprintf(stderr,"%s
    ","file error!
    ");  
            return -1;  
        }  
      
        char hex;  
        int i=0;
        while(fread(&hex,1,1,in))  
        {  
            char s=pass[i];
            hex = hex^s;  
            fwrite(&hex,1,1,out);  
            i++;
            i%=passlen;
        }  
        return 0;  
    }  
      
    
    
  • 相关阅读:
    Springboot操作域对象
    Thymeleaf的条件判断和迭代遍历
    Thymeleaf的字符串与变量输出的操作
    window 2003 实现多用户远程登录
    非常漂亮js动态球型云标签特效代码
    Echarts运用
    sass、less和stylus的安装使用和入门实践
    java,js 解unicode
    Java上传下载excel、解析Excel、生成Excel
    小知识点应用
  • 原文地址:https://www.cnblogs.com/coded-ream/p/7489263.html
Copyright © 2011-2022 走看看