c ++ file encryption record and resolve problems

(First blog, I hope everyone can support! Beginner, or if the problem better insight, please indicate in the comments area, please share!)

I. Description of the problem

  Write an encryption program that reads from cin (a file)and writes the encoded characters to cout (a file).

  You might use this simple encryption scheme: the encrypted form of a character c is c^key[i], where key is a string passed as a command-line argument. The program uses the characters in key in a cyclic manner until all the input has been read. Re-encrypting encoded text with the same key produces the original text. If no key (or a null string) is passed, then no encrption is done.

Second, the problem is solved

  1.thinking

    Creates three files: in.txt (for reading), out.txt (encryption result memory), original.txt (decrypted result is stored).

    Read the file, for subsequent cryptographic operations; if the read fails, exception handling.

  2.important

    ①. Learn c ++ bit operation (here mainly for ^ exclusive OR operation)

    ②. Some processing on files

  3.process

    ①. About XOR operation

      

      As can be seen, the return value is the XOR operation int, char ^ int and actually carried out on the char implicit type conversion.

      In addition, we need to read a char from in.txt file to an int read from out.txt in.

      Has an exclusive OR operation to some specific applications: 1 or a different number twice per se. 2. a number of exclusive OR preserves the original value 0 (10,011,001 ^ 00000000--> 10011001)

    ②. About file reads and EOF

      Detailed explanation of the EOF: https://www.cnblogs.com/dolphin0520/archive/2011/10/13/2210459.html

      

 

      The file is loaded, each should judge whether the read after reading EOF. Of course, it is written according to the specific circumstances. In this question, the body if the read write cycles to determine conditions while (! F_in.eof ()) will be unpredictable problems occur, even if the file has been read, but the cycle does not end!

 

  4.result

      Share Source

 

 1 #include<iostream>
 2 #include<fstream>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 int main(int argc,char** argv)
 8 {
 9     ifstream fin("in.txt");
10     if (!fin.is_open()) {
11         cout << "Can't open file \"in.txt\"";
12         return -1;
13     }
14 
15      the fstream FOUT ( " out.txt " );
 16      IF (! {Fout.is_open ())  
 . 17          COUT << " Can Not Open File \" out.txt \ " " ;
 18 is          return - . 1 ;
 . 19      }
 20 is  
21 is      IF (strlen (the argv [ . 1 ]) == 0 ) {// if not receive the command-line parameters, encryption and decryption is not performed
 22 is          String TEMP;
 23 is          COUT << " .. no no encryption Key " << endl;
 24          the while (FIN >>temp){
25             fout<<temp;
26         }
27         return 0;
28     }
29 
30     char temp;
31     int i = 0;
32 
33     while (fin.get(temp))    //加密运算
34     {
35         fout << (temp ^ argv[1][strlen(argv[1]) - i % strlen(argv[1])]) << ' ';
36         i++;
37     }
38 
39     fin.close();
40     fout.close();
41 
42     cout << "Encryption has done ." << endl
43         << "Now begin re-encrypting ." << endl;
44 
45     ifstream f_in("out.txt");
46     if (!f_in.is_open()) {
47         cout << "Can't open file \"out.txt\"";
48         return -1;
49     }
50 
51     F_OUT ofstream ( " original.txt " );
 52 is      IF (! {f_out.is_open ())
 53 is          COUT << " Can Not Open File \" original.txt \ " " ;
 54 is          return - . 1 ;
 55      }
 56 is  
57 is      I = 0 ;
 58      int TEMP_1;
 59      the while (f_in >> TEMP_1) {// decryption operation, a cast is required here output character
 60          F_OUT << char ((^ TEMP_1 the argv [ . 1 ] [strlen (the argv [ . 1 ] ) - I% strlen (the argv [ . 1])]));
61         i++;
62     }
63 
64     cout << "Re-encryption has done." << endl;
65     return 0;
66 }

 

 

 

    

    

Guess you like

Origin www.cnblogs.com/cs-weilai/p/12507632.html