Total members 11797 |It is currently Sat Sep 21, 2019 9:51 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Encrypt/Decrypt (Encryption, Decryption) a file from source file to target file, i want to resolve errors from this prog...plz hep me
cpp code
#include <stdlib.h>
#include <fstream.h>
#include <iostream.h>
#include <string.h>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;

class krypt
{
public:
krypt();

void inputFrom(string);
void outputTo(string);
void modeA();
void modeB();
void userMode();

void displayHelp(int) const;

char myDash;
char mySwitch;

bool isEncrypt;
bool isDecrypt;
bool isStepup;
bool isUserCode;
bool isCompliment;
bool isInputFileAvailable;
bool isOutputFileAvailable;

int userCode;

friend void checkSwitch(char);

private:
string inputFileName;
string outputFileName;
string fileBody;
char tempChar;
}k;

krypt::krypt()
:inputFileName(" "),
myDash(' '),
mySwitch(' '),
isEncrypt(false),
isDecrypt(false),
isCompliment(false),
isStepup(false),
isUserCode(false),
isInputFileAvailable(false),
isOutputFileAvailable(false)
{}

void krypt::displayHelp(int helpCode) const
{
switch(helpCode)
{
case 107:
{
cout << "krypt: cannot encrypt and decrypt at the same time" << endl;
break;
}
case 106:
{
cout << "krypt: user code must be in the range 1-6" << endl;
break;
}
case 105:
{
cout << "krypt: " << k.output << ": Unable to write " << endl;
break;
}
case 104:
{
cout << "krypt: " << k.input << endl;
break;
}
case 103:
{
cout << "krypt: too many non-option arguments" << endl;
break;
}
case 102:
{
cout << "krypt: unrecognized option `" << k.mySwitch << "\'" << endl;
break;
}
case 101:
{
cout << "krypt: too few arguments" << endl;
break;

}
case 100:
{
cout << "Try again" << endl;
break;
}
}
}
void krypt::modeA()
{
int fileLength = (int)k.fileBody.size();

for(int i = 0; i < fileLength; ++i)
{
k.fileBody[i] ^= i;
}
}

void krypt::modeB()
{
int fileLength = (int)k.fileBody.size();

for(int i = 0; i < fileLength; ++i)
{
k.fileBody[i] = ~k.fileBody[i];
}
}

void krypt::userMode()
{

int fileLength = (int)k.fileBody.size();

if(isEncrypt == true && isDecrypt == false)
{
for(int i = 0; i < fileLength; ++i)
{
k.fileBody[i] = k.fileBody[i] + userCode;
}
}
else if(isEncrypt == false && isDecrypt == true)
{
for(int i = 0; i < fileLength; ++i)
{
k.fileBody[i] = k.fileBody[i] - userCode;
}
}
}

void krypt::inputFrom(string value)
{
k.inputFileName = value;

ifstream fin(value.c_str());
if(!fin)
{
k.displayHelp(104);
exit(1);
}

while(fin.get(k.tempChar))
{
k.fileBody += k.tempChar;
}
}
void krypt::outputTo(string value)
{
k.outputFileName = value;

ofstream fout(value.c_str());
if(!fout)
{
k.displayHelp(105);
exit(1);
}

fout << k.fileBody;
}

void checkSwitch(char value)
{
if(value == 's' || value == 'c' || value == 'x' || value == 'd' || value == 'e')
{
if(value == 's')
{
k.isStepup = true;
}

if(value == 'c')
{
k.isCompliment = true;
}

if(value == 'x')
{
k.isUserCode = true;
}

if(value == 'e')
{
k.isEncrypt = true;
}
else if(value == 'd')
{
k.isDecrypt = true;
}

if(k.isEncrypt == true && k.isDecrypt == true)
{
k.displayHelp(107);
k.displayHelp(100);
exit(1);
}
}
else
{
k.displayHelp(102);
k.displayHelp(100);
exit(1);
}
}
int main(int argc, char* argv[])
{
if(argc == 1)
{
k.displayHelp(101);
k.displayHelp(100);
exit(1);
}

string helpOptions = argv[1];

if(helpOptions == "--help")
{
cout << "Usage: krypt OPTION... FILE1 FILE2" << endl;
cout << "Encrypt/Decrypt a file from source file to target file.\n\n";
cout << " -c,modeA encryption" << endl;
cout << " -s,modeB encryption" << endl;
cout << " -x#,modeC encryption (user code required)" << endl;
cout << " -e,encrypt the file" << endl;
cout << " -d,decrypt the file\n\n";
cout << "--help" << endl;

cout << "Use one -e or -d to encrypt or decrpyt. You must use at least one: -c -s -x#.\n";
cout << "" << endl;
exit(1);
}

int numberOfFiles = 0, sourceFileNum = 0, targetFileNum = 0;
string tempSourceFileName = " ", tempTargetFileName = " ";

for(int i = 1; i < argc; ++i)
{
k.myDash = argv[i][0];
k.mySwitch = argv[i][1];
if(k.myDash == '-')
{
checkSwitch(k.mySwitch);
if(k.mySwitch == 'x')
{
string tempUserCode;
int numbers = 0, x = 2;

while(argv[i][x] >= '0' && argv[i][x] <= '9')
{
if(numbers < 0 || numbers > 5)
{
k.displayHelp(106);
k.displayHelp(100);
exit(1);
}

tempUserCode += argv[i][x];

++x;
++numbers;
}

if(numbers == 0)
{
k.displayHelp(106);
k.displayHelp(100);
exit(1);
}

k.userCode = atoi(tempUserCode.c_str());
}
}
else if(k.myDash != '-')
{
++numberOfFiles;

if(numberOfFiles > 2)
{
k.displayHelp(103);
k.displayHelp(100);
exit(1);
}
if(numberOfFiles == 1)
{
sourceFileNum = i;
tempSourceFileName = argv[i];
k.isInputFileAvailable = true;
}
else if(numberOfFiles == 2)
{
targetFileNum = i;
tempTargetFileName = argv[i];
k.isOutputFileAvailable = true;
}
}
}

if(k.isInputFileAvailable == false || k.isOutputFileAvailable == false)
{
k.displayHelp(101);
k.displayHelp(100);
exit(1);
}

if((k.isStepup == true || k.isCompliment == true || k.isUserCode == true)
&& (k.isEncrypt == true || k.isDecrypt == true))
{
k.inputFrom(tempSourceFileName);

if(k.isEncrypt == true && k.isDecrypt == false)
{
if(k.isUserCode == true)
{
k.userMode();
}

if(k.isCompliment == true)
{
k.modeB();
}

if(k.isStepup == true)
{
k.modeA();
}
}
else if(k.isEncrypt == false && k.isDecrypt == true)
{
if(k.isCompliment == true)
{
k.modeB();
}

if(k.isStepup == true)
{
k.modeA();
}

if(k.isUserCode == true)
{
k.userMode();
}
}

k.outputTo(tempTargetFileName);
}
else
{
k.displayHelp(101);
k.displayHelp(100);
exit(1);
}

return( EXIT_SUCCESS );
}





Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

please give us a hint on what this code is above ?


Author:
Moderator
User avatar Posts: 47
Have thanks: 1 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Encrypt/Decrypt a file from source file to target file.
 Reading selected data from a source file     -  
 location of a package statement within a source code file     -  
 Copy file to file in java code- implementation     -  
 getting file name of html input file tag using jsp     -  
 file descriptor vs file pointer     -  
 How to convert xml file to Pdf file using C     -  
 C++ File I/O     -  
 web.xml file     -  
 explode CSV file     -  
 File creation in C++     -  



Topic Tags

C++ Algorithms






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com