Click to See Complete Forum and Search --> : encryption code please help


cyke1
December 3rd, 2007, 04:42 AM
Hey guys I need some help again. I need to create a program that will open a file and create a second file. The second file should be encrypted by reading the file by reading each character and adding 10 to the ASCII code. Here is what I have so far the thing I can't figure out is how to add 10 to the ASCII code. please help. The book says something about reinterpret cast but I'm not sure how to use it and if that is right. Thank for any help.


#include <iostream>
#include <fstream>
using namespace std;

int main()
{
const int SIZE = 51;
char fileName[SIZE];
fstream dataFile, outPut, inFile;

ofstream outFile(outPut);

cout << "This program encrypts a text file by addind 10 to the\n";
cout << "ASCII code of each character in the file, then writes\n";
cout << "a new text file.\n";
cout << "Enter the input file name: " << fileName << endl;
cin >> fileName;
cout << "Enter the output file name: " << outPut << endl;
cin >> outPut;

dataFile.open(fileName);
if (fileName)
{
return 0;
}
dataFile.write (reinterpret_cast<char *>(ASCII+10), sizeof(ASCII+10));

cout << "The file has been encrypted." << endl;

dataFile.close();
return 0;
}

Doron Moraz
December 3rd, 2007, 05:26 AM
I can't figure out is how to add 10 to the ASCII code. please help. The book says something about reinterpret cast but I'm not sure how to use it and if that is right.

Hi Cyke,

Look at the following example:

char ch='A';
ch+=10; // Add 10 to the current ASCII value, CH='K'.
cout<<ch<<endl;

int asciiValue=65; // 'A' in ASCII
asciiValue+=10;
cout<<(char)asciiValue<<endl; // Cast it so we print the ASCII code and not the value.
cout<<asciiValue<<endl;



Another thing, I will recommend you to encrypt your information using a symmetric or unsymmetrical cryptography.
Take a look at:
http://www.codeguru.com/forum/showthread.php?t=439683

Regards
Doron Moraz