The method and explanation of string encryption in C ++ using StringCrypt
What Will I Learn?
- You will learn how to protect string using StringCrypt. (The string can be a password)
- Thanks to the StringCrypt in C++ you will learn how to protect your privacy.
Requirements
- Windows or Linux OS (i prefer Linux)
- Basics of programming in C++
- Reading comprehension
Difficulty
- Intermediate / Advanced
Tutorial Contents
I will present you with an authoritative StringCrypter
class for encrypting text strings in a C ++ programming language.
The class is very simple to use.
Just create its instances by passing the encryption key to the constructor and use the two available methods, such as encryptString
and decryptString
.
The great advantage of this implemented class is the ability to use the returned results in other source code or text configuration files.
The encrypted string is transformed into a specially implemented format that uses a hexadecimal number system.
Thanks to this operation, the output data does not have any strange lines known from the ASCII board and can be used without any coding change in the programming editors.
Of course, if you want and are only looking for a way to convert hex to hex or string to C ++, then you can modify the StringCrypt
class.
An example of the practical use of our string cipher script is at the end of this entry.
If you want to use our class, all you need to do is place the stringCrypter.h and stringCrypter.cpp files in the folder with your project and attach it using the #include
preprocessor directive, as in the example below.
#include <iostream>
#include "StringCrypter.h"
using namespace std;
int main()
{
//encryption key
StringCrypter *obj = new StringCrypter("a9d1r6cx4x9v0d5p7w4m5k29v9xc02457sc64u1h");
//encrypt
string password = obj->encryptString("H3ll0 world p@ssword!");
cout << password << endl;
//decrypt
string plainText = obj->decryptString(password);
cout << plainText << endl;
return 0;
}
The result of the program:
Header file StringCrypter.h:
#ifndef STRINGCRYPTER_H
#define STRINGCRYPTER_H
#include <iostream>
#include <cstring>
using namespace std;
class StringCrypter
{
public:
StringCrypter(string key);
string encryptString(string text);
string decryptString(string text);
private:
string encryptDecrypt(string text, bool encrypted);
string strToHex(const string& text);
string hexToStr(const string& text);
string xorKey;
};
#endif // STRINGCRYPTER_H
Implementation of the StringCrypter.cpp class:
#include "StringCrypter.h"
StringCrypter::StringCrypter(string key)
{
this->xorKey=key;
}
string StringCrypter::strToHex(const string& text)
{
string hexCharset = "0123456789ABCDEF";
string result;
int textLenght = text.length();
result.reserve(2*textLenght);
for (int i=0; i<textLenght; i++)
{
const unsigned char c = text[i];
result.push_back(hexCharset[c >> 4]);
result.push_back(hexCharset[c & 15]);
}
return result;
}
string StringCrypter::hexToStr(const string& text)
{
const char* const hexCharset = "0123456789ABCDEF";
string result;
int textLenght = text.length();
result.reserve(textLenght/2);
for (int i=0; i<textLenght; i += 2)
{
char tmp1 = text[i];
const char* p = lower_bound(hexCharset, hexCharset + 16, tmp1);
char tmp2 = text[i + 1];
const char* q = lower_bound(hexCharset, hexCharset + 16, tmp2);
result.push_back(((p - hexCharset) << 4) | (q - hexCharset));
}
return result;
}
string StringCrypter::encryptDecrypt(string text, bool encrypted)
{
if (encrypted)
text=hexToStr(text);
string key = this->xorKey;
string result = text;
for (int i = 0; i < text.size(); i++)
result[i] = (int)(text[i] ^ key[i % key.length()]);
if (encrypted)
return result;
else
return strToHex(result);
}
string StringCrypter::encryptString(string text)
{
return this->encryptDecrypt(text, false);
}
string StringCrypter::decryptString(string text)
{
return this->encryptDecrypt(text, true);
}
You should still remember to reduce detection, which can be a very useful step!
in these simple steps you can do this:
- Encrypt function names with any encryption key
- Encrypt library names with any encryption key
- Put their encrypted versions in variables, constants or a file
- Place the encryption key in a variable, fixed file or download it from the internet in a dynamic way
- Before running the function indicators that load WinAPI functions, decrypt the functions and library you want to use
an example
We encoded the string with the example name of the CopyFileA
function and the name of the Kernel32.dll
library using the cryptographic key 1111r6cx4x9v0d5p7w4m5k29v9xc02457sc64u1h
. Then we put all three values in the source code.
Now it's enough to use the encrypted text values obtained in the source code as it does in the code list below.
#include <windows.h>
#include "StringCrypter.h"
using namespace std;
BOOL myCopyFile(LPCTSTR source, LPCTSTR destination)
{
typedef BOOL(WINAPI * _CF)
(
LPCTSTR lpExistingFileName,
LPCTSTR lpNewFileName,
BOOL bFailIfExists
);
StringCrypter *obj = new StringCrypter("1111r6cx4x9v0d5p7w4m5k29v9xc02457sc64u1h");
const char * encryptFunction = "22561448345F0F1D75";
const char * encryptLibrary = "2A5C165F175A504A1A1C551A";
_CF dynamicCopyFunction = (_CF)GetProcAddress(GetModuleHandle(obj->decryptString(encryptLibrary).c_str()), obj->decryptString(encryptFunction).c_str());
return dynamicCopyFunction(source, destination, true);
}
int main()
{
myCopyFile("C:\\Users\\HakerEduPL\\Desktop\\malw.exe", "C:\\Users\\sirsm\\Desktop\\backup.exe");
return 0;
}
In line 14-18 we can see in a simple way how we use encrypted functions in our application.
The names of functions and libraries are deciphered from strings only when we need them.
Of course, you can use the class for completely different related purposes, such as:
Hiding before analysis in text sequences of e-mail addresses / passwords,
Encrypts of collected logs,
Darkening of network communication in the client-server architecture.
explanation, how should the encrypt and decrypt be:
Encrypting
Decrypting
The first assumption of the program is that if you change the key (value), the encrypted value will be different.
When the user enters a different value than 1 or 2, he will display "Invalid Input".
Let us remember.
The string can be our password, so it's worth taking care of them and protecting them.
It is worth protecting our privacy.
Congratulations @mubek! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word
STOP
Vote for @Steemitboard as a witness and get one more award and increased upvotes!
Congratulations @mubek! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!