文件加密解密

文件加密和解密

要求:写成菜单形式

例:选项:加密,解密,退出

①加密,解密之前对文件打开的判断;

②存在,打开成功,那么就进行加密,解密操作;

③异或原理进行加密,解密;

加密:读取原文件中中内容与密钥进行异或处理,达到对文件加密的效果,将加密后的数据保存在加密文件中;

a.txt  内容:ab

密钥:12

异或处理:PP

加密后文件b.txt:pp

(1)

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#define KEY 12

FILE *fp;

char str[100]= {0};

char str2[100]= {0};

int MenuShow()

{

    printf("*************欢迎使用 万邦易嵌 文件管理系统**************** ");

    printf("  1. 新建文件 ");

    printf("  2. 录入文件信息 ");

    printf("  3. 加密文件 ");

    printf("  4. 解密文件 ");

    printf("  5. 退出系统 ");

    printf(" 请选择:");

    int data;

    scanf("%d", &data);

    return data;

}

void CreateFile()

{

    char filename[20];

    printf("请输入需要新建的文件名: ");

    fflush(stdin);

    gets(filename);

    printf(" ");

    fp=fopen(filename,"w+");

    if(fp==EOF)

    {

        printf("新建文件打开失败!");

        exit(1);

    }

    else printf("文件新建成功! ");

    if(fclose(fp)==0)printf("文件关闭成功! ");

    printf(" ");

}

void write_text()//录入要加密的文本

{

    printf("请输入文本: ");

    fflush(stdin);

    gets(str);

    fputs(str,fp);

    char str2[100];

    rewind(fp);

    fgets(str2,100,fp);

    printf("%s ",str2);

    if(fclose(fp)==0)

        printf("文件关闭成功! ");

}

void EncrypFile()

{

    fp=fopen("a.txt","w+");

    if(fp==0)

    {

        printf("Error! ");

        exit(1);

    }

    int i;

    for(i=0; i<strlen(str); i++)

    {

        str2[i]=str[i]^KEY;

        fputs(str2,fp);

    }

    printf("encrypted_text:%s ",str2);

    if(fclose(fp)==0) printf("文件关闭成功! ");

}

void DecrypFile()

{

    fp=fopen("text.c","w+");

    if(fp==NULL)

    {

        printf("文件打开失败! ");

        exit(1);

    }

    else

        printf("文件打开成功! ");

    int k;

    char str3[100]= {0};

    int i,tot=0;

    printf("请输入密钥:");

LOOP:

    scanf("%d",&k);

    if(k!=KEY)

    {

        printf("密钥输入错误请重新输入! ");

        tot++;

        if(tot>3)

        {

            printf("密钥输入错误次数过多! ");

            exit(0);

        }

        goto LOOP;

    }

    else

    {

        for(i=0; i<strlen(str2); i++)

            str3[i]=str2[i]^k;

        printf("文件解密成功! ");

        printf("Decode_text:%s ",str3);

        fputs(str3,fp);

    }

}

int main()

{

    int cnt=0;

    while(1)

    {

        char userinfo[100]="wyt123456";

        char account[100]= {0};

        char password[100]= {0};

        char user[100]= {0};

        printf("*****欢迎登入绝密档案维护系统!****** ");

        printf("请输入账号:");

        gets(account);

        printf("请输入密码:");

        gets(password);

        sprintf(user,"%s%s",account,password);

        if(strcmp(userinfo,user)==0)

        {

            printf("恭喜你,密码正确! ");

            break;

        }

        else

        {

            printf("密码错误! ");

        }

        ++cnt;

        if(cnt==5)

            exit(cnt);

    }

    int data;

    fp=fopen("a.txt","w+");

    if(fp==NULL)

    {

        printf("文件打开失败! ");

        exit(1);

    }

    else

        printf("文件打开成功! ");

    while(1)

    {

        data = MenuShow();

        switch (data)

        {

        case 1:

            CreateFile();

            break;

        case 2:

            write_text();

            break;

        case 3:

            EncrypFile();

            break;

        case 4:

            DecrypFile();

            break;

        case 5:

            printf("系统成功退出! ");

            return 0;

        }

    }

}

(2)

#include <stdio.h>  // 标准输入输出函数

#include <stdlib.h>  // 标准库函数

#include <string.h>  // 字符串处理函数

#include <ctype.h>   // 字符操作函数

/**

 * 在文件开头声明函数原型,将函数定义放在main()函数后面

**/

void printMenu(void);

int checkPwd(int type);

int encryptFile(char *sourcefile, char *secretKey,char *targetFile);

char redo(void);

char action;

char sourcefile[30];

char secretKey[30];

char targetFile[30];

int main()

{

      

       while(1)

       {

              printMenu();  // 显示菜单

              scanf("%c",&action);  // 输入要执行的操作

              fflush(stdin);  // 清空stdin缓冲区

              switch(action)

              {

                     // 退出系统

                     case 'z':

                            exit(0);

                            break;

                     // 文件加密

                     case 'a':

                            while(1)

                            {

                                   printf("输入要加密的文件名(含路径):");

                                   scanf("%s", sourcefile);

                                   printf("输入密钥:");  //密钥是用户自己定义的,可以随意给需要加密的文件添加密钥

                                   scanf("%s", secretKey);

                                   printf("加密后的文件名(含路径):");  //给加密后的文件命名,并保存

                                   scanf("%s",targetFile);

                                   if( encryptFile(sourcefile, secretKey, targetFile) )

                                   {

                                          printf("恭喜你,文件[%s]加密成功,保存在[%s]。 ", sourcefile, targetFile);

                                   }

                                   if(redo() == 'a')

                                   {  // 继续加密文件

                                   }

                                   else

                                   {  // 回到主菜单

                                          break;

                                   }

                            }

                            break;

                     // 文件解密

                     case 'b':

                            while(1)

                            {

                                   printf("输入要解密的文件名(含路径):");

                                   scanf("%s",sourcefile);

                                   printf("输入密钥:");

                                   scanf("%s",secretKey);

                                   printf("解密后的文件名(含路径):");  //对解密的文件系统又可以提供保存路径

                                   scanf("%s", targetFile);

                                   if( encryptFile(sourcefile, secretKey, targetFile) ){

                                          printf("恭喜你,文件[%s]解密成功,保存在[%s]。 ", sourcefile, targetFile);

                                   }

                                   if(redo() == 'a')

                                   {  // 继续解密文件

                                   }

                                   else

                                   {

                                          break;  // 回到主菜单

                                   }

                            }

                            break;

                     default:

                            printf("没有相应的菜单!按任意键回到主菜单...");

                            getchar();

              }

       }

       return 0;

}

/**

 * 显示程序菜单

**/

void printMenu()

{

       printf("***************** 文本加密解密软件 ***************** ");

       printf("*                                                  * ");

       printf("*      **************************************      * ");

       printf("*                                                  * ");

       printf("*                                                  * ");

       printf("*      请从下面的菜单中选择你要进行的操作:        * ");

       printf("*      a. 文件加密                                 * ");

       printf("*      b. 文件解密                                 * ");

       printf("*      z. 退出系统                                 * ");

       printf("*                                                  * ");

       printf("**************************************************** ");

}

/**

 * 加密/解密文件

 *

 * @param   sourcefile    要加密/解密的文件名

 * @param   secretKey     密钥

 * @param   targetFile    加密/解密后要保存的文件名

 *

 * @return  加密成功或失败的数字表示

     0:加密失败

     1:加密成功

**/

int encryptFile(char *sourcefile, char *secretKey, char *targetFile)

{

       FILE *fpSource, *fpTarget;  // 要打开的文件的指针

       char buffer[21];  // 缓冲区,用于存放从文件读取的数据

       int readCount,  // 每次从文件中读取的字节数

              keyLen = strlen(secretKey),  // 密钥的长度

              i;  // 循环次数

       // 以二进制方式读取/写入文件

       fpSource = fopen(sourcefile, "r");

       if(fpSource==NULL)

       {

              printf("文件[%s]打开失败,请检查文件路径和名称是否输入正确! ", sourcefile);

              return 0;

       }

       fpTarget = fopen(targetFile, "w");

       if(fpTarget==NULL)

       {

              printf("文件[%s]创建/写入失败!请检查文件路径和名称是否输入正确! ", fpTarget);

              return 0;

       }

       // 不断地从文件中读取 keyLen 长度的数据,保存到buffer,直到文件结束

       while( (readCount=fread(buffer, 1, keyLen, fpSource)) > 0 )

       {

              // 对buffer中的数据逐字节进行异或运算

              for(i=0; i<readCount; i++)

              {

                     buffer[i] ^= secretKey[i];

              }

              // 将buffer中的数据写入文件

              fwrite(buffer, 1, readCount, fpTarget);

       }

       fclose(fpSource);

       fclose(fpTarget);

       return 1;

}

/**

 * 当前操作完成,要进行的下一次操作(次要菜单)

 *

 * @return  下一次操作的字母表示

     a:回到主菜单

     b:继续当前操作

**/

char redo()

{

       char action;

       printf(" 接下来你希望: ");

       printf("a. 继续当前操作 ");

       printf("b. 回到主菜单 ");

       // 不停的等待用户输入,直到输入正确

       while(1)

       {

              fflush(stdin);

              scanf("%c", &action);

              fflush(stdin);

              if(action!='a' && action!='b'){

                     printf("没有相应的菜单,请重新选择。 ");

              }

              else

              {

                     return action;

                     break;

              }

       }

}

原文地址:https://www.cnblogs.com/eastofeden/p/7375971.html