Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - File encryption in C language
File encryption in C language
//Write your own encryption. The encryption method is to XOR the file content according to the input password and store it in the encrypted file.

//Reprint please ask Brother Situ Cai to declare the program.

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & ltconio.h & gt//getche () needs

# include & ltstring.h & gt

char ch

//XOR function

int YiHuo(FILE *f0,FILE *fp,int passwordnum,int move,char key)

{

while(fread(& amp; ch, 1, 1,f0),! feof(f0))

{

If (key==49)// encryption

{

ch = ch+move;

(char)(ch^=passwordnum);

fwrite(& amp; ch, 1, 1,FP);

}

If (key==50)// Decryption

{

(char)(ch^=passwordnum);

ch = ch+move;

fwrite(& amp; ch, 1, 1,FP);

}

}

}

int main()

{

//encryption encryption decryption decryption

Files *f0, *fe, * fd

char fname 0[50];

char ef name[50]= { 0 };

char df name[50]= { 0 };

char Password[50]={0},key=0,key 2 = 0;

char sysfname[20]= { 0 };

int p=0,passwordnum,move = 0; //Number of moves after XOR;

System ("CLS");

do

{

System ("CLS");

do

{

Printf(" 1- Encrypted file \ n ");

Printf(" 2- decrypt file \ n ");

printf(" ESC-Exit \ n ");

key = getch(); //Read in the keyboard code

}

And ((key! = ' 1 ')& amp; & (Key! = ' 2 ')& amp; & (Key! =27));

Password [50] = 0;

password num = 0;

if (key==49)

{

System ("CLS");

Printf ("Please enter the absolute path of the file to be encrypted \ n");

scanf("%s ",fname 0);

// while(key=0)

//Open the original file

if ((f0=fopen(fname0," Rb))= = NULL)

{

Printf ("The file can't be opened! \ n ");

key = 0; //When the file cannot be opened, the value of key changes.

Exit (0);

Printf ("Encryption failed \ n");

System ("suspended");

}

//Only when the file fname0 is successfully opened can you continue to open e fname.

if(key==49)

{

System ("CLS");

Printf ("Please enter the file storage path where encryption is completed \ n");

scanf("%s ",ef name);

if ((fe=fopen(efname," w+")= = NULL)

{

Printf ("The file can't be opened! \ n ");

Printf ("Encryption failed \ n");

fclose(F0);

System ("suspended");

Exit (0);

key = 0;

}

}

//If the file is opened successfully, enter the password.

if(key==49)

{

System ("CLS");

Printf ("Please enter password \ n");

Scanf("%s ",password);

//Convert the character password to integer XOR.

for(p = 0; Password [p]! =0; p++)

Password num+=((int)Password[p]);

move =(int)(password num % 9);

while(move & gt; 9)

move =(int)(move % 9);

printf("%d ",move); System ("suspended");

}

if (key==49)

{//XOR encryption

System ("CLS");

A fire (f0, fe, passwordnum, move, key);

Printf ("\ nEncryption succeeded, and your output file path is: \ n");

printf("%s ",ef name);

fclose(F0);

fclose(Fe);

}

}

if (key==50)

{

System ("CLS");

Printf ("Please enter the absolute path of the file to be decrypted \ n");

scanf("%s ",fname 0);

// while(key=0)

//Open the original file

if ((f0=fopen(fname0," Rb))= = NULL)

{

Printf ("The file can't be opened! \ n ");

key = 0; //When the file cannot be opened, the value of key changes.

Exit (0);

Printf ("Decryption failed \ n");

System ("suspended");

}

//Only when the file fname0 is successfully opened can you continue to open dfname.

if(key==50)

{

System ("CLS");

Printf ("Please enter the storage path of the decrypted file \ n");

scanf("%s ",df name);

if ((fd=fopen(dfname," w+")= = NULL)

{

Printf ("decryption failed");

fclose(F0);

System ("suspended");

Printf ("The file can't be opened! \ n ");

Exit (0);

key = 0;

}

}

//If the file is opened successfully, enter the password.

if(key==50)

{

System ("CLS");

Printf ("Please enter password \ n");

Scanf("%s ",password);

//Convert the character password to integer XOR.

for(p = 0; Password [p]! =0; p++)

Password num+=((int)Password[p]);

move =(int)(password num % 9);

while(move & gt; 9)

move =(int)(move % 9);

move =(-move);

}

if (key==50)

{//XOR encryption

System ("CLS");

One fire (f0, fd, passwordnum, move, key);

Printf ("\ nEncryption succeeded, and your output file path is: \n%S", df name);

System ("suspended");

fclose(F0);

fclose(FD);

}

}

}

And (key! =27); //End the loop when the key is 0, that is, both files are opened successfully.

Returns 0;

Printf ("\ nThanks for using! , program by stucaige \ n ");

System ("suspended");

}