jiami.c

来自「文件 加密 程序」· C语言 代码 · 共 79 行

C
79
字号
/*文件加密,用文件逐字节与密码异或方式对文件进行加密*/
#include<stdio.h>
#include<stdlib.h>

#include<string.h>

void encryption(char *in,char *password,char *out);/*加密函数*/

void main()
{
    char in[30];
    char out[30];
    char password[10];



    printf("\n input In-file name:\n");
    gets(in);/*得到要加密的文件名*/

    while(1)
	{
		printf(" input Password(length<10):\n");
        gets(password);/*得到密码*/

	    if(strlen(password)<10)break;
		printf("the password is too long,input again");
	}

        printf(" input Out-file name:\n");
        gets(out);/*得到加密后你要的文件名*/
        

     encryption(in,password,out);
     
   

}


/*加密子函数*/
void encryption(char *in,char *password,char *out)
{
    FILE *fp1,*fp2;
    register char ch;
    int i=0,j=0;
   



    fp1=fopen(in,"rb");
    if(fp1==NULL){
    printf("cannot open in-file.\n");
    exit(1);/*打开失败,退出程序*/
    }
    fp2=fopen(out,"wb");
    if(fp2==NULL){
    printf("cannot open or create out-file.\n");
    exit(1);/*打开失败,退出程序*/
    }



    i=strlen(password);
    ch=fgetc(fp1);
   


/*加密算法开始*/
    while(!feof(fp1)){

    fputc(ch^password[j],fp2);/*加密*/
	j>=i?j=0:j++;

    ch=fgetc(fp1);
    }
/*关闭文件*/
    fclose(fp1);
    fclose(fp2);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?