C++


C++ Source code to encrypt and decrypt the Ceasar cipher

The following code accepts one line from the standard input and uses the functions encrypt and decrypt to break the Ceasar cipher when the key is known. In this example, the key was the number 12, the day of birth of Julius Ceasar.

[download id=”11738″]

#include <iostream>
#include <string> 

using namespace std;

#define OFFSET_SMALL (97)
#define OFFSET_BIG (65)
#define LATIN_CHARACTERS (26)

// Text and offset for the shift are sent into this function, which returns the text's encrypted version.
string encrypt(string text, int offset)
{
    string result = "";

    for (long unsigned int i = 0; i < text.length(); i++)
    {
        if (std::isalpha(text[i]))
        {
            if (std::islower(text[i]))
            {
                result += char (int(text[i] + offset - OFFSET_SMALL) % LATIN_CHARACTERS + OFFSET_SMALL);
            }
            else
            {
                result += char (int(text[i] + offset - OFFSET_BIG) % LATIN_CHARACTERS + OFFSET_BIG);
            }
        }

        else
        {
            result += text[i];
        }
    }

    return result;
}

// Text and offset for the shift are sent into this function, which returns the text's decrypted version.
string decrypt(string text, int offset)
{
    return encrypt(text, LATIN_CHARACTERS - offset);
}

int main() {
    string encrypted;
    getline(cin, encrypted);

    // Sample execution showing how to decrypt an already encrypted message.
    // Julius Ceasar was born on July 12, 100BC
    cout << decrypt(encrypted, 12);

    return 0;
}

[download id=”11738″]

.

Side note:

While authoring this post, we found a peculiar bug in the plugin named “SyntaxHlighlighter Evolved.”

Specifically, when we added the string char( in our C++ source code, we would get the following error back:

Updating failed. The response is not a valid JSON response.

To mitigate the problem, we added a space character between the word char and the open parenthesis as follows char (.


Problem with compiling C# code on Ubuntu using the mono project while the source path contains Unicode characters

To avoid having the same issues as we did, avoid saving your code in folders that contain spaces and non-ASCII characters as it seems that the mono compiler on Ubuntu GNU/Linux is having some issues dealing with those.

We installed the mono compiler as follows:

sudo apt install mono-complete;

Then we tried to compile our C# code as follows:

mcs -out:program.exe Program.cs;

Which produced the following error:

error CS0016: Could not write to file `program'. Could not find a part of the path "윐뛶單/윐뛶單/program.exe".

By moving our code out of that folder, we were able to compile and execute our application as follows:

mono program.exe;


Converting a (void*) buffer to a std::vector

On a project we were recently working on, some legacy C code was producing a (void*) voidBuffer accompanied by its size.
The rest of the project was in C++ and we needed to convert the (void*) voidBuffer to a std::vector<unsigned char> vector.

To do so, we used the following code:


//First cast the (void *) voidBuffer to an (unsigned char *) to implicitly get the element size (1 Byte each)
const unsigned char *charBuffer = (unsigned char *) voidBuffer;
//Then we create the vector (named vectorBuffer) by copying the contents of charBuffer to the vector
std::vector<unsigned char> vectorBuffer(charBuffer, charBuffer + length);

[download id=”4084″]


Enable C++11 standard for GCC on Eclipse CDT

When using Eclipse CDT to write C++, we noticed that it did not enable by default the C++11 standard. Following the steps below, we added the -std=c++11 flag on the GCC C++ Compiler command line arguments enabling the standard for our use.

  1. From the main window of Eclipse, on the list on the left, where your projects are listed, right click on your project and then click Properties from the new menu
  2. In the new window, navigate from the list on the left and expand the C/C++ Build option to view its children and then click the Settings item
  3. In the middle of the window, you will see a new list, expand (if needed) the item GCC C++ Compiler and click on the Miscellaneous child
  4. On the right, a text box named Other Flags will appear, append -std=c++11 to the list of tokens in the box as seen in the image below
  5. Click on the Apply button for the effects to take place and then the OK button to close the properties window

Next time you compile, the -std=c++11 flag will be present on your compiler command line and the C++11 standard will be used.