I recently learned about file operations and felt that file encryption is still interesting. So I learned it carefully and made a simple summary. The following describes one of the methods-command encryption.
The command encryption method instructs the user to type a complete file name, including the file path and file name. For example, enter the encryption password to encrypt the specified file.
Principle of encryption: Read the characters in the file and write them to the new file. The decryption process is the same as the encryption principle.
ProgramWrite as follows:
# Include <stdio. h>
# Pragma hdrstop
# Include <tchar. h>
# Pragma argsused
Int _ tmain (INT argc, _ tchar * argv [])
{
File * file1, * file2;
Int I;
Int paslen;
Char ch;
Char source [30], DESTIN [30], password [10];
Printf ("Please input the source file (less than 30 charcters): \ n ");
Gets (source );
Printf ("Please input the destination file (less than 30 charcters): \ n ");
Gets (DESTIN );
Printf ("Please input the password (less than 10 ditigals): \ n ");
Gets (password );
Paslen = strlen (password); // get the password length
If (file1 = fopen (source, "rb "))! = NULL)
{
Printf ("the source file % s opened successfully. \ n", source );
If (file2 = fopen (DESTIN, "WB + "))! = NULL)
{
Printf ("the destination file % s created successfully \ n", DESTIN );
Ch = fgetc (file1 );
I = 0;
While (Ch! = EOF)
{
Ch = CH ^ (password [I ++]); // use the password for encryption
If (I> = paslen)
I = 0;
Fputc (CH, file2 );
Ch = fgetc (file1 );
}
Fclose (file1 );
Fclose (file2 );
}
Else
{
Printf ("the destination file % s created error \ n", DESTIN );
}
}
Else
{
Printf ("the source file % s opened error. \ n", source );
}
Getchar ();
Return 0;
}
the running result cannot be displayed. Sorry. I will send my summary to csdn.net. If necessary, I will download the summary. After all, it takes a long time to learn the summary, so I still need some points. Sorry, if you have any questions, please contact me. Thank you! The file contains three common encryption methods, including principles, algorithms , programs, and running results. More support! My csdn.net username is andamajing and the file name is "C language-based file encryption technology ".