An example of document encryption technology
There are many technologies encrypted to files, which are divided into different levels to fit the needs of different occasions. The simplest file encryption technology is given, that is, encrypts files with passwords or methods, when decrypted Simply run again, you can run again.
Here is an example program that encrypts any file, the password requires the user to enter, limited to 8 digits (of course you can change). The program has a good fault tolerance, which is what we should learn.
/ * Turbo 2.0 Pass. Give file a password! * /
#include
Void Dofile (char * in_fname, char * pwd, char * out_fname); / * Connected to files Documents * /
Void Main (int Argc, char * argv []) / * Defines the command line parameter of the main () function * / {char in_fname [30]; / * User input to encrypted file name * / char output_fname [30]; CHAR PWD [8]; / * Used to save password * /
IF (argc! = 4) {/ * fault tolerance * / printf ("/ nin-fname: / n"); gets (in_fname); / * Get file name to encrypted * /
Printf ("password: / n"); gets (pwd); / * Get your password * /
Printf ("out-file: / n"); gets (out_fname); / * Get encrypted file name * /
DOFILE (IN_FNAME, PWD, OUT_FNAME);} else {/ * If the command line parameter is correct, run the program directly * / strcpy (in_fname, argv [1]); strcpy (PWD, Argv [2]); strcpy (out_fname, Argv [3]); DOFILE (in_fname, pwd, out_fname);
}
/ * Encrypted sub-function start * / void Dofile (char * in_fname, char * pwd, char * out_file) {file * fp1, * fp2; register char ch; int J = 0; int J0 = 0;
FP1 = FOPEN (IN_FNAME, "RB"); if (fp1 == null) {printf ("cannot open in-file./n"), if you can't open the file to encrypted, Program * /} fp2 = fopen (OUT_FILE, "WB"); if (fp2 == null) {Printf ("Cannot OR CREATE OUT-FILE./N "; eXIT (1) After the file, you will exit * /} while (PWD [ J0]); CH = FGETC (FP1);
/ * Encryption algorithm start * / while (! Feof (fp1)) {fputc (ch ^ PWD [J> = J0? J = 0: J ], FP2); / * Differences, write FP2 file * / ch = FGETC (FP1);} fclose (fp1); / * Close source file * / fclose (fp2); / * Close target file * /}
/ * Problem end * /