CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2007
    Posts
    13

    encryption code please help

    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.

    Code:
    #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;
    }
    Last edited by cyke1; December 3rd, 2007 at 06:05 AM.

  2. #2
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: encryption code please help

    Quote Originally Posted by cyke1
    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:
    Code:
    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
    Last edited by DoronMoraz; December 3rd, 2007 at 09:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured