Alphabet number/place in alphabet
You can convert each letter of the alphabet, to the number that respectively the place of that letter.
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.