c++ - Encryption/ Decryption of a file? -


i trying encrypt, decrypt file. when try decryption file, display content on screen make sure process of decryption done without problems. but, don't have display of decryption of file. not sure missing in code. using dev_c++. appreciated. code below.

#include <iostream> #include <fstream> #include <cstdlib> #include <string>    using namespace std;   int main() {      string line;      string file, encrfile;     int i, key_length, longueur;     unsigned int key=0;     char ch[100];      cout<<"enter secret key: ";     cin.getline(ch, 100);      (i=0;ch[i];i++)     key=(key+3)*ch[i];      cout<<"password generated: "<<key;      cout<<"\n\nenter name of input file: ";     getline(cin,file);      cout<<"\nenter name of output file: ";     getline(cin,encrfile);      ifstream is;     is.open(file.c_str() );       ofstream os;     os.open(encrfile.c_str());      while(is>>line);     {          //encrypting each character         (i=0;i<line.length();i++)         {             line[i]^=rand()>>8;             os<<line[i];  //writing character in output file                     }     }      is.close();     os.close();       cout<<"file "<<encrfile<<" has been encrypted"<<endl;      cout<<"\nenter name of file decrypt: ";     getline(cin,encrfile);        cout<<"\n\ndecryption of file:  "<<endl;     ifstream is2;     is2.open(encrfile.c_str());       while(is2>>line);     {          (i=0;i<line.length();i++)         {             line[i]^=rand()>>8;             cout<<line[i];        }     }     is2.close();    return 0; 

}

the ; means loop has empty body. read whole file word word here.

while(is>>line); 

so correcting above to:
reading word @ time. dropping spaces between words.

while(is>>line) 

this should work more expect it.

while(std::getline(is, line)) 

but here discarding new line character. again not want. point of encryption preserve characters.

to characters easiest read them 1 one:

char c; while(is >> std::noskipws >> c) 

use std::noskipws (so don't loose characters).

you encrypting using rand number.
fine: may want seed random number generator key make sure same sequence of rands each time. but work specific os/lib combination.

        line[i]^=rand()>>8; 

alternatively can replace rand() key.

        line[i]^=key>>8; 

same problem above

while(is2>>line); 

same problem above

        line[i]^=rand()>>8; 

using rand() encryption key:

not tested: should starting point:

#include <iostream> #include <fstream> #include <cstdlib> #include <string>  int main() {      std::cout<<"enter secret key: ";     std::string ch;      std::getline(std::cin,ch);     unsigned int key = 0;      (int i=0;i < ch.size();i++)         key=(key+3)*ch[i];      std::cout << "password generated: "<<key << "\n"               << "\nenter name of input file:\n";      std::string file;     std::getline(std::cin,file);     std::ifstream is(file.c_str());        std::cout<<"enter name of output file:\n";     std::string encrfile;     std::getline(std::cin,encrfile);     std::ofstream os(encrfile.c_str());      std::string line;      char c;      srand(key);  // reset random number sequence.     while(is >> std::noskipws >> c)     {            c ^= (rand() >> 8);          os << c;     }        is.close();     os.close();      std::cout << "file " << encrfile << " has been encrypted\n"               << "enter name of file decrypt:\n";      std::getline(std::cin,encrfile);     std::cout<<"\ndecryption of file:\n";      std::ifstream is2(encrfile.c_str());      srand(key);  // reset random number sequence.     while(is >> std::noskipws >> c)     {         c ^= (rand()>>8);         std::cout << c;     }     is2.close(); } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -