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

    Simple Encryption Program

    I need to encrypt a paragraph out of a txt file.

    I can get the info from the txt file into a vector with no problem.

    But then I need to encrypt the data using this key "wyijkcuvdpqlzhtgabmxefonrs"

    Where each letter represents a letter in the alphabet.

    w = a
    y = b
    i = c and so on.

    My problem is I have never done anything like this before and have no idea where to start or how this will work.

    Any help is appreciated!!!!

    Thank you



    Eclipse / WinXP

  2. #2
    Join Date
    Aug 2005
    Posts
    132

    Re: Simple Encryption Program

    Hi!

    You could approach this problem many ways, I think the most simple is to create a hash map of associations matching each letter with its corresponding replacement. I.E. "a"="w", "b"=y" and so on and so forth. Using this you could load each letter from your file you're trying to encrypt and just look up its replacement before writing it back to the encrypted file.

    If you need help with hash maps I would suggest here: http://www.cprogramming.com/tutorial/stl/stlmap.html
    Last edited by GuOddian; September 25th, 2007 at 10:34 PM.

  3. #3
    Join Date
    Sep 2007
    Posts
    1

    Re: Simple Encryption Program

    A simpler solution is to store the encryption into a character array or string.
    i.e. string mappings = "wyijkcuvdpqlzhtgabmxefonrs";
    Then when you loop through your text simply concatenate the i-th character of mappings like:
    Code:
    for (int i = 0; i < vec.size(); i++) {
       for (int j = 0; j < vec[i].size(); j++) {
           cout << mappings[vec[i][j]-'a'];
       }
       cout << "\n";
    }
    This just prints to stdout but it's easy to modify depending on your requirements.
    EDIT: Seems like I had the reversed direction of the mappings, but you can just assign the mappings string in reverse and it should do the same job.
    Last edited by wilan; September 26th, 2007 at 03:48 AM.

  4. #4
    Join Date
    Jul 2007
    Posts
    61

    Re: Simple Encryption Program

    here's a little bit of code i was writing for pleasure. it doesnt use a vector, but it works perfectly fine.

    Code:
    void encryptor(string filename)
    {
      filename += ".txt";
    
      string ofilename, line;
      ofilename = "o" + filename;
    
      ifstream file (filename.c_str());
      ofstream ofile (ofilename.c_str());  
    
      if (file.is_open() && ofile.is_open())
      {
        while (!file.eof())
        {
          getline (file, line);
          ofile << encryptKey(line) << '\n';
        }
      file.close();
      ofile.close();
      }
      else
      {
        cout << "Unable to open one of the files";
      }
    }
    but i couldnt get it to overwirte the original text file, so it outputs it to the filename plu 'o'. also, the parameter filename is inputted in the main function using getline, and encryptkey is just a switch statement with the encryption key.

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