1

I am trying to hide a few strings in my c++ 'program', I've noticed if my 'program' is opened (in for example: Notepad) people can view strings and edit value then save, then re-run program which contains changed strings and the string they edited will take place of my original text.

I am not looking for anything very advanced due to the fact i am only trying to keep people from editing the text in which can easily be viewed when 'program' is opened in Notepad, i am not trying to keep any 'hackers' from doing this, only skids trying to edit text in my 'program'.

I've tried something like:

char hiddentxt[10];
strcat(hiddentxt,"h");
strcat(hiddentxt,"e");
strcat(hiddentxt,"l");
strcat(hiddentxt,"l");
strcat(hiddentxt,"o");

But, I'm sure as you can tell this is very time consuming when it comes to huge strings (note: obviously this isn't a "secure" method of hiding strings, but please realize, i AM NOT trying to keep hackers from editing strings, only people who don't really know what they're doing)

I've also looked into using XOR but it doesn't seem like a good solution in my case.

Any help would be appreciated!

JeffCoderr
  • 283
  • 1
  • 4
  • 16
  • Random idea: Each byte value += 128 (with unsigned variables to prevent UB) – deviantfan Jul 05 '16 at 12:06
  • 3
    It's called ["security through obscurity"](https://en.wikipedia.org/wiki/Security_through_obscurity) and it have never really worked. If you want your strings to be "unreadable" then store them in a separate and encrypted file. – Some programmer dude Jul 05 '16 at 12:07
  • 1
    Possible duplicate of [How to hide a string in binary code?](http://stackoverflow.com/questions/1356896/how-to-hide-a-string-in-binary-code) – hlscalon Jul 05 '16 at 12:08
  • What a strange relations with other people who can see your code? – ilotXXI Jul 05 '16 at 12:08
  • @JoachimPileborg like i said I'm not trying to prevent anyone with any sort of programming experience from changing text value, just people opening my c++ file in for example notepad. – JeffCoderr Jul 05 '16 at 12:15
  • If the strings are sensitive (usernames, passwords, etc) they should be securely encrypted. If they're not, I don't understand what the problem is. – molbdnilo Jul 05 '16 at 12:39
  • Maybe you should tell us what your program actually does. Because if it handles sensitive information, this ain't a good idea. – O'Niel Jul 05 '16 at 13:11

2 Answers2

0

Alphabet number/place in alphabet

You can convert each letter of the alphabet, to the number that respectively the place of that letter.

  • a = 1
  • b = 2
  • ...
  • z = 26

This program is a quick 'tool' do this type of encoding. Separate it from your actual project.
Save as: MyStringEncoder.cpp and compile

#include <iostream>
#include <string>

using namespace std;

string encodeMyString(string s) {
    string encodedString;
    string alphabet = "abcdefghijklmnopqrstuvwxyz";
    for (int i = 0; i < s.length(); i++) {
        char toEncode = s[i];
        for (int j = 0; j < alphabet.length(); j++) {
            if (toEncode == alphabet[j]) {
                encodedString += to_string(j +1);
                encodedString += "_";
            }
        }
    }

    encodedString.erase(encodedString.length() -1, encodedString.length());
    return encodedString;
}

int main() {
    cout << "Give in string to encode: ";

    string stringToEncode;
    cin >> stringToEncode;

    cout << encodeMyString(stringToEncode);
    cin.ignore().get();
}

When you enter a string, it will be reversed to numbers. So "hello" will become "8_5_12_12_15".

In your actual program; you have to have these two functions:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

//Needed in your actual program
char toAlpha(int n) {
     return "abcdefghijklmnopqrstuvwxyz"[n-1];
}
string decodeMyString(string encodedString) {
     stringstream ss(encodedString);
     string part;
     vector<int> partList;

     while (getline(ss, part, '_')) {
          partList.push_back(stoi(part));
     }

    string decodedString;
    for (int i = 0; i < partList.size(); i++) {
        decodedString += toAlpha(partList[i]);
    }

    return decodedString;
}
//----------------------------------

int main() {

     cout << decodeMyString("8_5_12_12_15");
}

By putting the encoded number-string into the function decodeMyString() it'll get reversed back to "hello", because the eight character of the alphabet is "h", the fifth is "e",...

This way someone opening your file would only see numbers, but not actually words.

Of course, you still have to build in support for spaces and other special characters. But it's just a quick example.

Caesar Cipher

This cipher is one of the most basic and first versions of encryption, yes, this is actually encryption because you need a key to encrypt and decrypt it. However, it's a very weak form of encryption.

Anyway, it works like this. You have the alphabet, and you shift it a specific times. So,
ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
Is shifted, one time.

So, 1 is your 'secret' key.

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

//Function to encrypt
string toCaesar(string s, int key) {
    string alphabet = "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < s.length(); i++) {
        for (int j = 0; j < alphabet.length(); j++) {
            if (s[i] == alphabet[j]) {
                int index = j + key;
                if (index >= 26) index = index - 26;
                s[i] = alphabet[index];
                break;
            }
        }
    }

    return s;
}

//Function to decrypt
string decryptCaesar(string s, int key) {
    string alphabet = "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < s.length(); i++) {
        for (int j = 0; j < alphabet.length(); j++) {
            if (s[i] == alphabet[j]) {
                int index = j - key; //Only difference is the MINUS here
                if (index >= 26) index = index - 26;
                s[i] = alphabet[index];
                break;
            }
        }
    }

    return s;
}

int main() {
    int inputKey;
    string stringToEncrypt;

    cout << "Give your key: ";
    cin >> inputKey;
    cout << "String to encrypt: ";
    cin >> stringToEncrypt;

    cout << toCaesar(stringToEncrypt, inputKey) << endl;
    cout << decryptCaesar("fgh", 5);
}

This is weak, not only because it can be easily brute-forced. But also because if you take the most for common letter in English ('e') and the most for common in your cipher, you already have one of the 26 letters.

Note

However, note that both of these methods are weak, and unsecure. What you are trying to do is called security through obfuscation. And is dissadvised.

Another thing you're maybe interested in, is source code obfuscation.

O'Niel
  • 1,622
  • 1
  • 17
  • 35
  • I don't know why anyone would? Thanks for the information, this answers my questions :) – JeffCoderr Jul 05 '16 at 14:51
  • 1
    @JeffCoderr Good to hear that. :) Glad I could help you. – O'Niel Jul 05 '16 at 14:51
  • If you are going to do trivial encryption that's easy to implement then using XXTEA would probably be a better option than rot13 which is *increadibly* trivial to break. XXTEA will at least keep the script kiddies busy for a few minutes rather than seconds ;) – Jesper Juhl Jul 05 '16 at 15:04
-1

Replace each character with character to the right of it at the keyboard. Then "hello" becomes "jr;;p". (The rightmost character becomes the leftmost, i.e. '/' becomes 'z'.) Implement decode function, then use constants i.e. instead of

print("Hello world\n");

you need

static const char* sz_hello_world = decode("Jr;;p ept;f\n");
printf(sz_hello_world);

Special characters like '\n' or ' ' should not be touched.

Warning: since your decode function will allocate memory, you need to call each decode only once, to avoid memory leak. Thats why you need static const.

user31264
  • 6,557
  • 3
  • 26
  • 40