Search code examples
c++encryptionuppercaselowercasecaesar-cipher

upper and lowercase string encryption and decryption, basic cipher c++


#include<iostream>
#include<string>
#include<array>
#include<locale>
using namespace std;
class endeMachine
{
public:
    int findIndex(char letter)
    {
        int index = 0;
        while (letter != alphabet[index])
        {
            index++;
    }//end while letter test
    return index;
    }//findIndex
    string subEncrypt(string clear)
    {
        string subString = clear;
        for (int i = 0; i < clear.length(); i++)
        {
            subString[i] = substitution[findIndex(clear[i])];
        }//end for
        return subString;
    }//subEncrypt
    string transEncrypt(string clear)
    {
        string subString = clear;
        for (int i = 0; i < clear.length(); i++)
        {
            subString[i] = alphabet[findIndex(clear[i]) + offset];
        }//end for
        return subString;
    }//transEncrypt
private://---------------------------------------------------------
    array<char, 26> alphabet = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
    array<char, 26> substitution = { 'm','l','k','j','i','h','g','f','e','d','c','b','a','z','y','x','w','v','u','t','s','r','q','p','o','n' };
    int offset = 3;
};//end endoMachine
int main()
{
    endeMachine text;
    string clear_text = { "Hello" };
    cout << text.subEncrypt(clear_text) << endl;
    cout << text.transEncrypt(clear_text) << endl;
    cin >> clear_text;
}//end main

So in this program I am trying to eventually get to the point where it can:

  • Encrypt a string entered by the end user
  • Choose between a substitution or transposition method for the encryption
  • Decrypt the string previously encrypted post-entry
  • Choose between either one of the encryption methods to do so
  • Decrypt the encrypted string without knowing the method of encryption, therefore generating all possible results

My problem is:

When the input string contains an uppercase letter, the whole program shut downs. However if I do something in line 12 like:

while (tolower(letter) != alphabet[index])

the encryption methods both work, but return the strictly lowercase version of the originally input word, making the input of "Hello" result in:

fibby
knoor

upon output, rather than:

Fibby
Knoor

This means that in some way, I need to check for the capitalization of each letter in the word individually, so when it comes time for output, the corresponding letter of the ciphered string can be capitalized before it is output to the screen.

PLEASE HELP!!!


Solution

  • Your findIndex function will fail if an UPPER case char is passed.

    The reason is it uses the constant arrays below:

    array<char, 26> alphabet = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
    array<char, 26> substitution = { 'm','l','k','j','i','h','g','f','e','d','c','b','a','z','y','x','w','v','u','t','s','r','q','p','o','n' };
    

    So it will not find a match for an UPPER case letter, and your while loop means your index value returned will be out of bounds....That is 1 more than your array size.

    You can:

    • Check if char is upper case
    • convert to lower case
    • process via your functions as normal
    • if it WAS uppercase, then set your encrypted char to uppercase

    ...And repeat in reverse for decryption I guess.

    There are ugly and elegant ways to check if a char is uppercase...

    Ugly:

    Convert to lower case and compare with original..Are they the same? If not, Original is upper case. This is probably more portable across similar languages. tolower function here.

    Elegant:

    The char values (for normal English) uppercase characters are between and including 65 and 90. You could check for the char value in this range