Thread: Help with decoding program...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75

    Help with decoding program...

    I have a problem with my decoding program that I'm trying to figure out. It's supposed to read in a encrypted line of code from a .txt file and then by using ASCII values, convert that encrypted code to a decoded version. Example:

    Encrypted code:

    Fcjjm}rfcpc,

    Decoded code: (after adding 2 to every ASCII value - or a key of 2, within ASCII # 32 - 127)

    Hello there.

    Here is the outline:

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<vector>
    
    using namespace std;
    
    vector<char> read_file(string file_name);
    string decode(vector<char> encrypted, int key);
    void display(string decrypted, int key);
    
    int main ()
    {
    	vector<char> encrypted = read_file("intercepted.txt");
    
    	//Function calls
    	
    	return 0;
    }
    
    vector<char> read_file(string file_name)
    {
    	ifstream input_file;
    	input_file.open(file_name.c_str());
    	vector<char> encrypted;
    
    	// Code to read file
    
    	input_file.close();
    	return encrypted;
    }
    
    string decode(vector<char> encrypted, int key)
    {
    	// Code to decode
    }
    
    void display(string decrypted, int key){
    	cout << "Key: " << key << " Text: " << decrypted << endl;
    }
    If anyone can kinda walk me through this, I would be grateful for all the help! Thanks a lot!

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    loop through the string, and add 2 to every char.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    i see, however, when I have this in my code, it isn't outputting anything on the display:

    Code:
    vector <char> read_file(string file_name)
    {
    	ifstream input_file;
    	input_file.open(file_name.c_str());
    	if (input_file.fail())
    	   cout << "Fail";
    	vector <char> encrypted;
        for (int i = 0; i < encrypted.size(); i++)
            cout << encrypted[i];
        return encrypted;
    }
    Why is that? and can I have some suggestions to fix this? Thanks

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    vector <char> encrypted;
        for (int i = 0; i < encrypted.size(); i++)
            cout << encrypted[i];
    And what is the size of an empty vector?

    I don't really see why you wouldn't store the string in a std::string (which basically is a special vector<char>) and read it with getline (or whatever reads the entire file at a single go).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    how do I read in the .txt file then to vector <char> encrypted? Thanks for the help!

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you need a line of input from a file, you use the getline function (with std::string).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    anon, how does that look (i mean what do you mean by getline, i am not familiar, can you give me an example?)

    this is what I have so far in my problem function:

    Code:
    vector <char> read_file(string file_name) 
    {
           ifstream input_file; 
           input_file.open(file_name.c_str()); 
           if (input_file.fail()) 
           {
              cout << "Failed to open file!" << endl; 
              exit(1); 
           }
           vector <char> encrypted; 
           int next(0);
           while (input_file >> encrypted[next])
           {
                 cout << encrypted[next];
                 next++;
                 if (next == MAX_SIZE)
                    break;
           }
           return encrypted; 
    }

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    An empty vector has size 0. It is illegal to access any items in it with operator[], since there are no items.

    This is getline (works only with string).

    Another way to read the entire file into a string or vector<char> looks like this:

    Code:
    #include <iterator>
    ...
    ifstream fin(filename);
    vector<char> encrypted((istream_iterator<char>(fin)), istream_iterator<char>());
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    I figured out the reading in the string from the .txt file. Sorry, I didn't know that you would've responded at 5am or whatever it was, haha. New question, below in my decode() function, what is wrong, and how should I fix it? I envision the function in a for loop (which I have) cycling through every key up to 100, adding an ASCII value of 1 to every character in encrypted, and then cout << encrypted.


    Code:
    #include <iostream>
    #include <vector>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    
    using namespace std; 
    
    vector <char> read_file(string file_name); 
    string decode(vector <char> encrypted, int key); 
    void display(string decrypted, int key); 
    
    int main () 
    {
        int number = 0;  
        vector <char> encrypted = read_file("intercepted.txt"); 
        string decrypted = decode(encrypted, number); 
        display(decrypted, number); 
        
        int start; 
        cin >> start; 
        
        return 0; 
    }
    //-------------------------------------------- 
    vector <char> read_file(string file_name) 
    {
           ifstream input_file; 
           input_file.open(file_name.c_str()); 
           if (input_file.fail()) 
           {
              cout << "Failed to open file!" << endl; 
              exit(1); 
           }
           vector <char> encrypted; 
           char next(0);
           while(input_file >> next)
    	   {
               encrypted.push_back(next);
    		   cout << next;
           }
           cout << endl;
           input_file.close();
         
           return encrypted; 
    } 
    //-------------------------------------------
    string decode(vector <char> encrypted, int key) 
    {
           string decode; 
           int size = encrypted.size(), i = 0;
           for (i; i < size; i++)       {
               for (key = 0; key < 100; key++)
               {
                   
                   if (int(encrypted[i]) + key > 126)
                   {
                      encrypted.push_back(char(32 + ((int(encrypted[i]) + key) - 127)));                }
                   else
                   {
                       encrypted.push_back(char(int(encrypted[i]) + key));                }
               }
           }
    } 
    //------------------------------------------
    void display(string decrypted, int key)
    {
         cout << "Key: " << key << " Text: " << decrypted << endl; 
    }

  10. #10
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Code:
    string decode(vector <char> encrypted, int key) 
    {
           string decode; 
           int size = encrypted.size(), i = 0;
           for (i; i < size; i++)       {
               for (key = 0; key < 100; key++)
               {
                   
                   if (int(encrypted[i]) + key > 126)
                   {
                      encrypted.push_back(char(32 + ((int(encrypted[i]) + key) - 127)));                }
                   else
                   {
                       encrypted.push_back(char(int(encrypted[i]) + key));                }
               }
           }
    }
    I'm a little confused on what you are trying to do here.
    -You declare "number" in main as 0, which is passed to your decode function as "key", which is then set to 0 in a loop in your decode function.
    -What exactly is key suppose to be? In your first post, you say it is the decoding ascii value you are adding. But, why would you be cycling through 100 keys in your decode function?
    -What exactly are you trying to accomplish by the following:
    Code:
    for (key = 0; key < 100; key++)
               {
                   
                   if (int(encrypted[i]) + key > 126)
                   {
                      encrypted.push_back(char(32 + ((int(encrypted[i]) + key) - 127)));                }
                   else
                   {
                       encrypted.push_back(char(int(encrypted[i]) + key));                }
               }
    If the ascii value (for decoding) is going to be the same, then just cycle through encrypted changing each character based on that ascii value.

    Code:
    cycle through encrypted
    {
         decryptedCharacter = encrypted character + ascii value
         feed into returned string
    }
    IDE - Visual Studio 2005
    Windows XP Pro

  11. #11
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    to ascii values are only supposed to go through character 32 - 127. so, when it hits 127, it is supposed to go back to 27. with the whole key/number thing, I don't know why I did that either, just a mess up. but the key is just supposed to the value that you add to the ascii character value. does that change what you said before?!

  12. #12
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Ah, I think I understand now. So, when it hits 127, it is suppose to go back to 32 and add what was left over. In that case, your if statement is justified:
    Code:
    if (int(encrypted[i]) + key > 126)
                   {
                      encrypted.push_back(char(32 + ((int(encrypted[i]) + key) - 127)));       
                   }
    So, your function pretty much works without the key loop:
    Code:
    string decode(vector <char> encrypted, int key) 
    {
           string decode; 
           int size = encrypted.size(), i = 0;
           for (i; i < size; i++)       {
             
                   if (int(encrypted[i]) + key > 126)
                   {
                      encrypted.push_back(char(32 + ((int(encrypted[i]) + key) - 127)));                }
                   else
                   {
                       encrypted.push_back(char(int(encrypted[i]) + key));                }
               
           }
    print
    }
    The only problem is you are adding the decoded message to the end of the coded message in the vector. So, printing the results (where I marked red) with:
    Code:
    for(int j = 0; j < encrypted.size(); j++)
    		{
    			cout << encrypted[j];
    		}
    will print "Fcjjm}rfcpc,Hello there.". If you changed the initialization of j to "size" in the for loop, then it will print "Hello there.". However, this probably isn't the best way to handle the problem, unless you are wanting to keep the coded message stored for some reason. Even then, I would think it would be best to keep them in separate vectors. If you don't need the old coded message stored, then just change:
    Code:
    encrypted.push_back(char(int(encrypted[i]) + key));  
    
    to
    
    encrypted[i] = (char(int(encrypted[i]) + key));
    IDE - Visual Studio 2005
    Windows XP Pro

  13. #13
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    ok, the last thing that you posted didn't really work out the way that I wanted unfortunately, but I understand what you mean and that makes sense. here's an easier way to look at it. I manipulated this in a different way. Could you help me go through the key loop now, please?! Thanks a lot for your help, its helping out a lot!!!

    Code:
    string decode(vector <char> encrypted, int key)
    {
    	//Creates new vector
    	string decrypted = "";
    	//encrypted[i] = (char(int(encrypted[i]) + key));
        for (int i = 0; i < encrypted.size(); i++)
        {		
        	if (int(encrypted[i]) - key > 126)
    		{
    			decrypted += char(((int(encrypted[i]) - key) + 127) - 32);
    		}
    		else
    		{
                decrypted += char(int(encrypted[i]) - key);
    		}
        }
    	
    	return decrypted;
    }

  14. #14
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    What exactly are you trying to do with the key loop? The program should be working now.

    A couple things:
    -Some of your plus & minus symbols in the for loop are switched around.
    -You have no if statement to take care of values less than 32. This is fine assuming all your characters read in are not less than 32 and your key is never negative.
    IDE - Visual Studio 2005
    Windows XP Pro

  15. #15
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    The program does not decode correctly. It compiles, and when I run it, I get the same thing that the original .txt file has inside it. So, basically, the program is doing nothing, just outputting what it read from the .txt file. That is what I'm asking about. The key loop, should add 1 to each character in the string everytime it completes the loop. For example:

    Fcjjmumpjb --> key = 0 (if this was the original string from the .txt file)
    Gdkknvnqkc --> key = 1
    Helloworld --> key = 2

    Does that make sense?! So, thats what my problem is. I need the decode() function to do, but I am braindead right now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program quits unexpected
    By skelesp in forum C Programming
    Replies: 34
    Last Post: 12-10-2008, 09:10 AM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  3. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  4. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM