Search code examples
c++encryption

Simple Encryption program array


Building a simple program that multiplies the ASCII value of chars in a string by 3 to encrypt and then divide by 3 to decrypt. So far I got the encryption part down but whenever I enter what the encryption gave and try to decrypt it doesn't work. I think it has something to do with the buffer stream but I could be wrong if anyone could help.

#include <iostream>

using namespace std;

int main()
{
    string message;
    int charValue;
    int counter;
    int encrypt;
    char choice;
    char quit = 'N';

    while (quit == 'N')
    {
        cout << "Enter E to encrypt or D to Decrypt\n";
        cin >> choice;
        toupper(choice);

        cout << "Enter text no spaces: ";
        cin >> message;

        int messagelen = message.length();
        string stringArray[255];

        if (choice == 'E')
        {
            for (counter = 0; counter < messagelen; counter++) //*3 to ascii val
            {
                stringArray[counter] = message[counter] * 3;
            }

            for (counter = 0; counter < messagelen; counter++)
            {
                cout << stringArray[counter];
            }
        }
        else
        {
            for (counter = 0; counter < messagelen; counter++) // divide 3 to ascii val
            {
                stringArray[counter] = message[counter] / 3;
            }

            for (counter = 0; counter < messagelen; counter++)
            {
                cout << stringArray[counter];
            }
        }

        cout << "\nY to go again N to quit";
        cin >> quit;
    }
    return 0;
}

Solution

  • This is a working implementation, although I agree with the other answer that you should use encrypt and decrypt functions. I found quite a few other bugs with your code working through it. You should enable all warnings with -Wall -Werror and fix them:

    #include <iostream>
    #include <vector>
    #include <sstream>
    
    int main()
    {
      // removed some unused variables
      std::string message;
      size_t counter;
      char choice;
      char quit;
      // use vector of int instead of array of strings
      std::vector<int> encryptArray;
    
      // change to do while loop. Not particularly necessary, but I think
      // it makes more sense in this case. Your condition is broken. If the
      // user enters 'Y' at the end to go again, then the quit == 'N'
      // condition is false and the program terminates.
      do
      {
        std::cout << "Enter E to encrypt or D to Decrypt\n";
        std::cin >> choice;
        // toupper returns a value, you need to assign it to choice.
        // Not capturing the return value makes this a noop.
        choice = toupper(choice);
    
        if (choice == 'E')
        {
          std::cout << "Enter text no spaces: ";
          std::cin >> message;
    
          size_t messagelen = message.length();
          // initialize vector to input message length size
          encryptArray = std::vector<int>(messagelen);
    
          for (counter = 0; counter < messagelen; counter++) //*3 to ascii val
          {
            encryptArray[counter] = message[counter] * 3;
          }
    
          // Note, this 2nd loop is more work than you need, you could
          // simply put the std::cout line in the loop above below the
          // assignment
          for (counter = 0; counter < messagelen; counter++)
          {
            // added the separator just for clarity. You could also print
            // hex bytes
            std::cout << encryptArray[counter] << "|";
          }
        }
        else
        {
          // all the data we care about is in the vector now
          for (counter = 0; counter < encryptArray.size(); counter++) // divide 3 to ascii val
          {
            // you don't want to /3 what's in the message here, you want
            // to /3 the encrypted values, which are in the vector
            encryptArray[counter] = encryptArray[counter] / 3;
          }
          // plenty of ways to convert the vector to a string, this is not
          // a "modern" way.
          // Note, you could skip this loop entirely, and in the one
          // above, simply do ss << (char)(encryptArray[i] / 3);
          // Not necessary to write the data back to encryptArray first.
          std::stringstream ss;
          for (size_t i=0; i<encryptArray.size(); ++i)
          {
            ss << (char)encryptArray[i];
          }
          std::cout << "decrypted string: " << ss.str() << std::endl;
        }
    
        std::cout << "\nY to go again N to quit: ";
        std::cin >> quit;
      } while(quit != 'N'); // loop until quit == N
    
      return 0;
    }
    

    Finally, I removed using namespace std;, here's why

    Things get squirrely working with stdin on godbolt, but here's a working demonstration, at least initially.