C ++ simple file encryption and decryption instance

Source: Internet
Author: User

A file encrypted from the InternetProgramAfter reading it, I understood the so-called file encryption. In fact, the principle is quite simple:

 

That is, read the data in the file to be encrypted by byte or bit, and then do some work with the password we entered.AlgorithmOperation to write new data to the new file. The new file is our encrypted file.

 

So I designed a simple encryption algorithm to implement file encryption and write a decryption algorithm. For your reference

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> </P> <p> void encfile (char * in_filename, char * Pwd, char * out_filename ); /* specific function for file encryption */<br/> void decryptfile (char * in_filename, char * Pwd, char * out_filename ); /* specific function for file decryption */</P> <p> int main (INT argc, char * argv [])/* defines main () function Command Line Parameter */<br/> {<br/> int option;/* function Selection */<br/> char in_filename [30]; /* the name of the file to be encrypted or the file to be decrypted entered by the user */<br/> char out_filena Me [30];/* enter the encrypted file name or decrypted file name */<br/> char PWD [8]; /* used to save the password */</P> <p> printf ("Thank you for using this program ...... /n "); <br/> printf (" 1. encrypt a file 2. decrypt a file/N "); <br/> printf (" chose your option ..... "); </P> <p> scanf (" % d ", & option); <br/> getchar (); <br/> If (argc! = 4) {/* fault tolerance Processing */<br/> printf ("/nplease input in-filename:/N"); <br/> gets (in_filename ); /* get the file name to be encrypted */<br/> printf ("Please input your password:/N"); <br/> gets (PWD ); /* get the password */<br/> printf ("Please input out-filename:/N"); <br/> gets (out_filename ); /* Get the encrypted file name */</P> <p >}< br/> else {/* If the command line parameter is correct, directly run the program */<br/> strcpy (in_filename, argv [1]); <br/> strcpy (PWD, argv [2]); <br/> strcpy (out_filename, argv [3]); <br/>}</P> <p> Switch (option) {<br/> case 1: // encryption <br/> encfile (in_filename, PWD, out_filename ); /* call the encryption function */<br/> break; <br/> case 2: // decrypt <br/> decryptfile (in_filename, PWD, out_filename ); /* call the decryption function */<br/> break; <br/> default: <br/> break; <br/>}</P> <p> system ("pause"); <br/> return 0; <br/>}</P> <p>/* encryption subfunction start */<br/> void encfile (char * in_filename, char * Pwd, char * out_file) <br/>{< br/> file * FP1, * fp2; <br/> Register char ch; <br /> Int J = 0; <br/> int J0 = 0; <br/> FP1 = fopen (in_filename, "R "); /* open the file to be encrypted */<br/> If (FP1 = NULL) {<br/> printf ("cannot open in-file. /n "); <br/> exit (1);/* If the file to be encrypted cannot be opened, exit the Program */<br/>}< br/> fp2 = fopen (out_file, "W"); <br/> If (fp2 = NULL) {<br/> printf ("cannot open or create out-file. /n "); <br/> exit (1);/* If an encrypted file cannot be created, exit */<br/>}</P> <p> while (PWD [++ J0]); </P> <p> CH = fgetc (FP1 ); </P> <p>/* encryption algorithm start */<br/> while (! Feof (FP1) {<br/> If (J0> 7) <br/> J0 = 0; <br/> CH + = PWD [J0 ++]; <br/> fputc (CH, fp2); <br/> CH = fgetc (FP1); <br/>}< br/> fclose (FP1 ); /* close the source file */<br/> fclose (fp2 ); /* close the target file */<br/>}</P> <p>/* Start the decryption sub-function */<br/> void decryptfile (char * in_filename, char * Pwd, char * out_file) <br/>{< br/> file * FP1, * fp2; <br/> Register char ch; <br/> Int J = 0; <br/> int J0 = 0; <br/> FP1 = fopen (in_filename, "R "); /* open the file to be decrypted */<br/> If (FP1 = NULL ){ <Br/> printf ("cannot open in-file. /n "); <br/> exit (1);/* If the file to be decrypted cannot be opened, exit the Program */<br/>}< br/> fp2 = fopen (out_file, "W"); <br/> If (fp2 = NULL) {<br/> printf ("cannot open or create out-file. /n "); <br/> exit (1);/* If the decrypted file cannot be created, exit */<br/>}</P> <p> while (PWD [++ J0]); <br/> CH = fgetc (FP1 ); <br/>/* decryption algorithm start */<br/> while (! Feof (FP1) {<br/> If (J0> 7) <br/> J0 = 0; <br/> CH-= PWD [J0 ++]; <br/> fputc (CH, fp2);/* My decryption algorithm */<br/> CH = fgetc (FP1 ); <br/>}< br/> fclose (FP1);/* close the source file */<br/> fclose (fp2 ); /* close the target file */<br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.