[Help Me]Very Simply Encrypt/Decrypt

This is a discussion on [Help Me]Very Simply Encrypt/Decrypt within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream> #include <string> using namespace std; int Menu(void); void Encrypt(void); void Decrypt(void); string EncryptLine(string,int); string DecryptLine(string,int); ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    Exclamation [Help Me]Very Simply Encrypt/Decrypt

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int Menu(void);
    void Encrypt(void);
    void Decrypt(void);
    string EncryptLine(string,int);
    string DecryptLine(string,int);
    
    int main(void)
    {
            int c = Menu();
            while( c != 3 )
            {
                    switch(c)
                    {
                            case 1:
                                    Encrypt();
                            case 2:
                                    Decrypt();
                    }
    
            }
    }
    
    int Menu(void)
    {
            int c = 0;
            cout << "Press [1] to Encrypt" << endl'
            cout << "Press [2] to Decrypt" << endl;
            cin << c;
    }
    
    void Encrypt(void)
    {
            string encryptedLine;
            string line;
                    //Stuff to get a line and the key
            EncryptLine(line,key);
            //stuff to write out lines
    }
    
    void Decrypt(void)
    {
            string decryptedLine;
            string line;
            // Stuff to get a line
            DecryptLine(line,key);
            // stuff to write out lines
    }
    this is the example i was given to go of off and i have no idea where to even start, we are also given 2 files, Number.dat, the only thing in that file is the number 3, and then we are given Unencrypted.txt, which contains 3 lines of text.

    I am not asking for this to be done for me i am merely asking if anyone can give me any tips or hints on where to start, i am extreamly confused

    Edit: We have to pull in the information from Unencrypted.txt, use the Number.dat as the key, and output it to a new file names Encrypted.txt

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    For a "Very" simple encryption/decryption, you would make it symetric so that you only need one function to encrypt and decrypt.

    As far, as file I/O google for std::fstream tutorials, and you will find many helpful sites.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Quote Originally Posted by Raigne View Post
    For a "Very" simple encryption/decryption, you would make it symetric so that you only need one function to encrypt and decrypt.

    As far, as file I/O google for std::fstream tutorials, and you will find many helpful sites.
    thanks! ill check it out

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Code:
    cin << c;
    this should be:

    Code:
    cin >> c;
    We have to pull in the information from Unencrypted.txt, use the Number.dat as the key, and output it to a new file names Encrypted.txt
    The idea that you have to use Number.dat as the key suggests that a specific encryption scheme has been given to you. We can't help you very much if you don't tell us what that is. Remember there are many many ways to do encryption, the specific way you've been asked to do it will have a big impact on the code you need to write.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    Instructions

    these are the instructions we were given


    Create a program that reads in a number from a file named "Number.dat". This number will be the "key" for your encryption and decryption.

    Use this number to "encrypt" all of the characters in a file named "Unencrypted.txt" by adding the number to each character and saving the new text as "Encrypted.txt".

    Write a decrypter that takes in the number from "Number.dat" and the encrypted contents of "Encrypted.txt" and decrypt the contents into "Decrypted.txt"

    Do this in one program, with a menu to encrypt or decrypt.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    ShiroAzure,

    In addition to Mozza's correction, it may be desirable to tell the user that the choice three will cause the program to exit. Otherwise it appears that you presentation layer is done for the given design.

    What sorts of ideas do you have for the activities associated with getting the key? That seems like a good place to start. I might consider using a separate function to extract the key from your 'Number.dat' file, because it is used by both the Encrypt and Decrypt functions....

    Perhaps, because of the algorithm purposed by the assignment, one could use the same function (e.g. Transform) that accepts the key and applies it to the Unencrypted.txt/Encrypted.txt file depending on the menu choice made by the user.... Somgetething like:

    Code:
    void Transform( fstream &in, fstream &out, int key );
    I looked on the wikipedia for the keyword fstream, and followed the external reference to cplusplus.com: fstream - C++ Reference. Check out the constructor link....

    Best Regards,
    Last edited by new_ink2001; 04-08-2011 at 12:21 AM. Reason: semantic error
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    Okay so...

    Im breaking this down trying to get it 1 piece at a time.
    Right now im trying to get the reading in and math functions down in this code

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(void)
    {
      ifstream fReader("test.txt");
    
       int i;
       char str[255];
    
    
       string line;
       string previousLine;
      while(!fReader.eof())
      {
          if(line.length() > 0)
             previousLine = line;
          getline(fReader,line);
          cout << line << endl;
    
      }
    
      fReader.close();
    
    
       for( i = 0; i < previousLine.length(); i++)
       {
          cout << "previousLine[" << i << "] = " << previousLine[i] << endl;
       }
    
       for(i = 0; i < previousLine.length(); i++)
       {
          cout << previousLine[i] << " + 2 = " << char(previousLine[i]+2) <<
    endl;
       }
    
    
       return 0;
    }
    this is just me trying to test it but for some reason its not reading both lines of test.txt, in test.txt i have written
    THIS IS DUMB
    AND OVERLY DIFFICULT

    but when i run through this the only output i get it

    THIS IS DUMB
    AND OVERLY DIFFICULT


    previousLine[0] = A
    previousLine[1] = N
    previousLine[2] = D
    previousLine[3] =
    previousLine[4] = O
    previousLine[5] = V
    previousLine[6] = E
    previousLine[7] = R
    previousLine[8] = L
    previousLine[9] = Y
    previousLine[10] =
    previousLine[11] = D
    previousLine[12] = I
    previousLine[13] = F
    previousLine[14] = F
    previousLine[15] = I
    previousLine[16] = C
    previousLine[17] = U
    previousLine[18] = L
    previousLine[19] = T
    A + 2 = C
    N + 2 = P
    D + 2 = F
    + 2 = "
    O + 2 = Q
    V + 2 = X
    E + 2 = G
    R + 2 = T
    L + 2 = N
    Y + 2 = [
    + 2 = "
    D + 2 = F
    I + 2 = K
    F + 2 = H
    F + 2 = H
    I + 2 = K
    C + 2 = E
    U + 2 = W
    L + 2 = N
    T + 2 = V




    so its only displaying the last line and converting it, how do i fix it so it reads out and prints out both?
    Last edited by ShiroAzure; 04-12-2011 at 08:45 PM.

  8. #8
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    It involves putting something like this:

    Code:
       for( i = 0; i < previousLine.length(); i++)
       {
          cout << "previousLine[" << i << "] = " << previousLine[i] << endl;
       }
    
       for(i = 0; i < previousLine.length(); i++)
       {
          cout << previousLine[i] << " + 2 = " << char(previousLine[i]+2) <<
    endl;
       }
    inside the loop where you're reading line by line:

    Code:
      while(!fReader.eof())
      {
          if(line.length() > 0)
             previousLine = line;
          getline(fReader,line);
          cout << line << endl;
    
      }

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Code:
       string line;
       string previousLine;
      while(fReader.good())  //tried (!fReader.eof()) as well
      {
          if(line.length() > 0)
             previousLine = line;
          getline(fReader,line);
          cout << line << endl;
    
       for( i = 0; i < previousLine.length(); i++)
       {
          cout << "previousLine[" << i << "] = " << previousLine[i] << endl;
       }
    
       for(i = 0; i < previousLine.length(); i++)
       {
          cout << previousLine[i] << " + 2 = " << char(previousLine[i]+2) <<
    endl;
       }
    
    
      }
    Like this?
    Mozza thanks for putting up with me lol your being a huge help! Okay so now the problem im running into is that its running through and outputting the last line twice
    Code:
    THIS IS DUMB
    AND OVERLY DIFFICULT
    previousLine[0] = T
    previousLine[1] = H
    previousLine[2] = I
    previousLine[3] = S
    previousLine[4] =
    previousLine[5] = I
    previousLine[6] = S
    previousLine[7] =
    previousLine[8] = D
    previousLine[9] = U
    previousLine[10] = M
    previousLine[11] = B
    T + 2 = V
    H + 2 = J
    I + 2 = K
    S + 2 = U
      + 2 = "
    I + 2 = K
    S + 2 = U
      + 2 = "
    D + 2 = F
    U + 2 = W
    M + 2 = O
    B + 2 = D
    
    previousLine[0] = A
    previousLine[1] = N
    previousLine[2] = D
    previousLine[3] =
    previousLine[4] = O
    previousLine[5] = V
    previousLine[6] = E
    previousLine[7] = R
    previousLine[8] = L
    previousLine[9] = Y
    previousLine[10] =
    previousLine[11] = D
    previousLine[12] = I
    previousLine[13] = F
    previousLine[14] = F
    previousLine[15] = I
    previousLine[16] = C
    previousLine[17] = U
    previousLine[18] = L
    previousLine[19] = T
    A + 2 = C
    N + 2 = P
    D + 2 = F
      + 2 = "
    O + 2 = Q
    V + 2 = X
    E + 2 = G
    R + 2 = T
    L + 2 = N
    Y + 2 = [
      + 2 = "
    D + 2 = F
    I + 2 = K
    F + 2 = H
    F + 2 = H
    I + 2 = K
    C + 2 = E
    U + 2 = W
    L + 2 = N
    T + 2 = V
    
    previousLine[0] = A
    previousLine[1] = N
    previousLine[2] = D
    previousLine[3] =
    previousLine[4] = O
    previousLine[5] = V
    previousLine[6] = E
    previousLine[7] = R
    previousLine[8] = L
    previousLine[9] = Y
    previousLine[10] =
    previousLine[11] = D
    previousLine[12] = I
    previousLine[13] = F
    previousLine[14] = F
    previousLine[15] = I
    previousLine[16] = C
    previousLine[17] = U
    previousLine[18] = L
    previousLine[19] = T
    A + 2 = C
    N + 2 = P
    D + 2 = F
      + 2 = "
    O + 2 = Q
    V + 2 = X
    E + 2 = G
    R + 2 = T
    L + 2 = N
    Y + 2 = [
      + 2 = "
    D + 2 = F
    I + 2 = K
    F + 2 = H
    F + 2 = H
    I + 2 = K
    C + 2 = E
    U + 2 = W
    L + 2 = N
    T + 2 = V
    Thats what my output is, ive been trying to tweak it some but i just cant make it work.
    Last edited by ShiroAzure; 04-13-2011 at 07:13 PM.

  10. #10
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    ShiroAzure,

    Your test on fReader.good(), on line 3, returns true after the stream has reached the end of the file. It isn't necessarily an error to have reached the end of file, but it is considered an error to attempt to read past it. Since your assignment isn't concerned with paging or memory contention, something like this should be effective:

    Code:
    ...
       ifstream fReader("test.txt");
    
       if( !fReader.good() )
       {
          cout << "Error reading test.txt...exiting." << endl;
    
          return 1;
       }
    
       while( !fReader.eof() )
       {
          getline( fReader,currentline );
          lines.push_back( currentline );
       }
    
       fReader.close();
    ...
    fReader.good() is more like a sanity check, e.g. does the file exist, can it be opened for reading, etc.... Then, the loop which loads the lines of the file into a list is controlled by whether or not the fReader has encountered an EOF during it's operations. After the file's entries, lines in this case, are loading into memory where they may be manipulated, the file is closed and processing continues.

    After a walk through, it looks like the code you had would initially read the first line, but not initialize the previousLine variable, so the loops that print would not execute. Then the second time through the loop the line variable is non-empty, so previousLine is initialized, and the line variable is overwritten. At this point fReader encounters the EOF, but has not been asked to read past it so fReader is still good. The loops which print then execute, because previousLine is non-empty. (<== first line of file is coded/decoded) Finally, the outer loop's condition tests true again, because the file is still in a "good" state, line is non-empty so previousLine is overwritten, and the fReader is asked to read another line, but fails. The loops which print again execute. (<== second line of file is coded/decoded) The outer loop's condition fails because the file is no longer good, and execution continues after the loop body given.

    I can't say why it is printing twice for you from the code you've given. Maybe one-too-many copy/pastes? I look forward to seeing your next try....

    Best Regards,
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  11. #11
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    This is a good option for reading a line and checking for eof at the same time.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Given these instructions, wouldn't it make more sense to simply read and write a character at a time?

    Code:
    #include <iostream>
    
    int main ( int argc, const char * argv[] )
    {
        const int magicNumber = 2;
        char c;
    
        while ( ( c = std::cin.get() ) != EOF ) {
            std::cout.put( c + magicNumber );
        }
    
        return 0;
    }
    Quote Originally Posted by ShiroAzure View Post
    these are the instructions we were given


    Create a program that reads in a number from a file named "Number.dat". This number will be the "key" for your encryption and decryption.

    Use this number to "encrypt" all of the characters in a file named "Unencrypted.txt" by adding the number to each character and saving the new text as "Encrypted.txt".

    Write a decrypter that takes in the number from "Number.dat" and the encrypted contents of "Encrypted.txt" and decrypt the contents into "Decrypted.txt"

    Do this in one program, with a menu to encrypt or decrypt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Good Discussion - Stay on Topic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 71
    Last Post: 04-28-2004, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21