What is the fastest wide string encoding and decoding method? What is the fastest method to secure wide strings? Can we use shifting to encode or decode a wide string? Can we use shifting on Wide-Strings or on binary data? Do Wide strings help me build C++ applications with the use of a C++ compiler? Let’s answer these questions.
What are bitwise operations?
The bit is the most basic unit of information in computing and digital communications. In real all operators are mainly based on Bit Operations which are also called Bitwise Operations. In computer programming, a Bitwise Operation operates on a bit string, a bit array, or a binary numeral (considered as a bit string) at the level of its individual bits, 1s, and 0s. The Bitwise Operation is the base of higher-level arithmetic operations, and it is a fast and simple action because it is directly supported by the processors. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.
Because of all these basics of the microarchitecture of computers, it is very important to know Bitwise Operators. The C Programming language is one of the oldest programming languages and a lot of operands, operators in other programming languages got inspiration from this language. C and C++ have the same operators and most of them are the same in other programming languages too. We have explained well about operators in general in this Learn How To Use Operators In C++ post before. Now let’s see Bit Shifting and Encoding – Decoding examples.
What is the simplest example of encoding and decoding a Wide String?
We can do encoding and decoding operations on integers, floating numbers, chars, strings, wide strings or binary data. Because all data operations are in done bit form in real. We can use bitwise left shifting operation to encode a data or we can use right shifting operation to decode this encoded data.
We can simply left shift (or right shift) the character of a string and we can simply right shift (or left shif) the character of a wide string as below,
1 2 3 4 5 6 7 |
std::wstring str = "This is a char string that will be encoded!"; wstr[i] = ((wchar_t)str[i])<<1; // left bit shifting string char wstr[i] = ((wchar_t)str[i])>>1; // right bit shifting string char |
This example below encodes and decodes wide string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> #include <string> #include <bitset> int main() { std::string str = "This is a char string that will be encoded!"; std::wcout << L"Original WString: " << wstr << '\n'; for(int i=0; i< str.length(); i++) { wstr[i]=((wchar_t)wstr[i])<<1; } std::wcout << "Encoded WString : " << wstr << '\n'; for(int i=0; i< str.length(); i++) { wstr[i]=((wchar_t)wstr[i])>>1; } std::wcout << "Decoded WString : " << wstr << '\n'; getchar(); return 0; } |
Output should be somethings like this, encoded characters may be different because of font will be used.
1 2 3 4 5 |
Original WString: This is a char string that will be encoded! Encoded WString : ¿ºÊµ@ʵ@┬@ãº┬õ@µ×õÊ▄╬@׺┬×@¯ÊÏÏ@─╩@╩▄ãÌ╚╩╚B Decoded WString : This is a char string that will be encoded! |
How can I encode (and decode) a Wide String with Circular Bit Shifting?
If you check that example output above each character symbol refers to another character symbol. If we want to avoid this we can add a complexity function which is iterated by the index number of the String Member. We can add complexity function to bitwise operation for each index of elements. For example we can use (index%8). So these kind of data is hard to encode if you don’t know this complexity function. You can formulate your own function. Be sure it is reversible and depends on index number, and some other parameters. We can use Circular Bit Shifting with Complexity to Encode or Decode Wide strings safely, as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> #include <string> #include <bitset> int main() { std::wstring wstr= L"This is a wide string that will be encoded!"; std::wcout << L"Original WString: " << wstr << '\n'; for(int i=0; i< str.length(); i++) { // encoding by circular left shifting each wide char bits to the left with a complexity wstr[i]=((wchar_t)wstr[i]) << (1+i%7) | ((wchar_t)wstr[i] >> ( 16 - (1+i%7)) ; (x << 2)|(x >> (8 - 2)); } std::wcout << "Encoded WString : " << wstr << '\n'; for(int i=0; i< str.length(); i++) { // decoding by circular right shifting each wide char bits to the left with a complexity wstr[i]=((wchar_t)wstr[i]) >> (1+i%7) | ((wchar_t)wstr[i] << ( 16 - (1+i%7)) ; } std::wcout << "Decoded WString : " << wstr << '\n'; getchar(); return 0; } |
the output will be as below,
1 2 3 4 5 |
Original WString: This is a wide string that will be encoded! Encoded WString : Tº??????a@??????tõ??????a×Ç?????b╩Ç?????e╚ä Decoded WString : This is a wide string that will be encoded! |
As you see, in a console application, most encoded characters are written with ‘?’ but they hold different wide chars, thus decoding works perfect as we can see in the output above. In the strings example we used i%8 too. Wide strings has 2 bytes char, here you can use i%16 or any number between 0 to 16.
Note that i%8 also does nothing if i is 0, so (1+i)%7 might be better to hide all characters.
Bit shifting is the fastest data manipulation. We could use this bit shifting method to protect our data, user names, etc. and similar sensitive items as a fun exercise but bit shifting in this way is a form of security through obscurity. We can use this with the support of a second industrial security system There are industry standard security practices such as salting passwords and using industry standard encryption that provide real security.
We can also hash this Encoded String by using powerful Cryptographic Hash Functions In Modern C++ On Windows (SHA, SHA2, MD5, BobJenkins, etc.). So you can put another dimension to your data systems. See this Learn To Use Powerful Cryptographic Hash Functions In Modern C++ On Windows (SHA, SHA2, MD5, BobJenkins)
If you are working with regular strings, you can also read this article.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition