Ocean
Binary.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_BINARY_H
9 #define META_OCEAN_BASE_BINARY_H
10 
11 #include "ocean/base/Base.h"
12 #include "ocean/base/RandomI.h"
13 
14 namespace Ocean
15 {
16 
17 /**
18  * This class implements functions for binaries like functions allowing to hide information in the binary code (in the release build).
19  * Another possibility to hide a string in the binary code is the application of the HIDE_STRING_32 define.
20  * @see HIDE_STRING_32.
21  * @ingroup base
22  */
23 class Binary
24 {
25  public:
26 
27  /**
28  * Decrypts a given string by a simple (not very secure) encrypt/decrypt method.
29  * @param encryptedValue The string to decrypt
30  * @param size The length of the encryptedValue value, with range [1, infinity)
31  * @param key0 The first key to be used for the decryption, with same length as value
32  * @param key1 The second key to be used for the decryption, with same length as value
33  * @return The decrypted value, if succeeded
34  */
35  static inline std::wstring decrypt(const wchar_t* encryptedValue, const size_t size, const wchar_t* key0, const wchar_t* key1);
36 
37  /**
38  * Encrypts a given string by a simple (not very secure) encrypt/decrypt method.
39  * @param decryptedValue The string to encrypt
40  * @param size The length of the encrypted value, with range [1, infinity)
41  * @param key0 Resulting first key necessary for the decryption, with same length as value
42  * @param key1 Resulting second key necessary for the decryption, with same length as value
43  */
44  static inline std::wstring encrypt(const wchar_t* decryptedValue, const size_t size, std::wstring& key0, std::wstring& key1);
45 };
46 
47 inline std::wstring Binary::decrypt(const wchar_t* encryptedValue, const size_t size, const wchar_t* key0, const wchar_t* key1)
48 {
49  ocean_assert(size != 0);
50 
51  std::wstring result;
52  result.resize(size);
53 
54  for (size_t n = 0; n < size; ++n)
55  {
56  result[n] = wchar_t((encryptedValue[n] - key1[n]) ^ key0[n]);
57  }
58 
59  return result;
60 }
61 
62 inline std::wstring Binary::encrypt(const wchar_t* decryptedValue, const size_t size, std::wstring& key0, std::wstring& key1)
63 {
64  ocean_assert(size != 0);
65 
66  key0.resize(size);
67  key1.resize(size);
68 
69  std::wstring result;
70  result.resize(size);
71 
72  for (size_t n = 0; n < size; ++n)
73  {
74  wchar_t value = 0;
75 
76  while (value == 0)
77  {
78  key0[n] = wchar_t(RandomI::random(1u, 255u));
79  key1[n] = wchar_t(RandomI::random(1u, 255u));
80 
81  value = wchar_t((decryptedValue[n] ^ key0[n]) + key1[n]);
82  }
83 
84  result[n] = value;
85  }
86 
87  return result;
88 }
89 
90 // This define is part of the HIDE_STRING_32 define used internally.
91 // @see HIDE_STRING_32.
92 // @ingroup base
93 #define ENCRYPT_STRING_INTERN_32(value, key1, key2) wchar_t(((value)[0] ^ key1[0]) + key2[0]), wchar_t(((value)[1] ^ key1[1]) + key2[1]), wchar_t(((value)[2] ^ key1[2]) + key2[2]), \
94  wchar_t(((value)[3] ^ key1[3]) + key2[3]), wchar_t(((value)[4] ^ key1[4]) + key2[4]), wchar_t(((value)[5] ^ key1[5]) + key2[5]), \
95  wchar_t(((value)[6] ^ key1[6]) + key2[6]), wchar_t(((value)[7] ^ key1[7]) + key2[7]), wchar_t(((value)[8] ^ key1[8]) + key2[8]), \
96  wchar_t(((value)[9] ^ key1[9]) + key2[9]), wchar_t(((value)[10] ^ key1[10]) + key2[10]), wchar_t(((value)[11] ^ key1[11]) + key2[11]), \
97  wchar_t(((value)[12] ^ key1[12]) + key2[12]), wchar_t(((value)[13] ^ key1[13]) + key2[13]), wchar_t(((value)[14] ^ key1[14]) + key2[14]), \
98  wchar_t(((value)[15] ^ key1[15]) + key2[15]), wchar_t(((value)[16] ^ key1[16]) + key2[16]), wchar_t(((value)[17] ^ key1[17]) + key2[17]), \
99  wchar_t(((value)[18] ^ key1[18]) + key2[18]), wchar_t(((value)[19] ^ key1[19]) + key2[19]), wchar_t(((value)[20] ^ key1[20]) + key2[20]), \
100  wchar_t(((value)[21] ^ key1[21]) + key2[21]), wchar_t(((value)[22] ^ key1[22]) + key2[22]), wchar_t(((value)[23] ^ key1[23]) + key2[23]), \
101  wchar_t(((value)[24] ^ key1[24]) + key2[24]), wchar_t(((value)[25] ^ key1[25]) + key2[25]), wchar_t(((value)[26] ^ key1[26]) + key2[26]), \
102  wchar_t(((value)[27] ^ key1[27]) + key2[27]), wchar_t(((value)[28] ^ key1[28]) + key2[28]), wchar_t(((value)[29] ^ key1[29]) + key2[29]), \
103  wchar_t(((value)[30] ^ key1[30]) + key2[30]), wchar_t(((value)[31] ^ key1[31]) + key2[31]), '\0'
104 
105 // This define is part of the HIDE_STRING_32 define used internally.
106 // @see HIDE_STRING_32.
107 // @ingroup base
108 #define ENCRYPT_STRING_32(value, key1, key2) { ENCRYPT_STRING_INTERN_32(value L"\x91_#\x12\x62\0\x54tw\x44\0p==&\0\xA1\xB2O\xB3\x53\x12\x93\xF2\x31\xA5\x55x#\x63\x71\0", key1 L"\x85P>?\0\0z\0\0w!xX+$\x57uE\0b\x31\x77\x65qnn\x81\x51\xA4\x3F\0\x77", key2 L"\xD1g\0\x83~\0qwCaT//{qq7\x68\x2B\xC3\xD1#vN\xAA\xE1\xC3)C\x78\x43p") }
109 
110 /**
111  * This function is part of the HIDE_STRING_32 define used internally.
112  * @see HIDE_STRING_32.
113  * @ingroup base
114  */
115 inline std::wstring decryptString32(const wchar_t* value, const unsigned int sizePlusTwo, const wchar_t* key1, const wchar_t* key2)
116 {
117  ocean_assert(sizePlusTwo >= 2u && sizePlusTwo <= 34);
118  std::wstring result(sizePlusTwo - 2, ' ');
119 
120  for (unsigned int n = 0u; n < sizePlusTwo - 2; ++n)
121  {
122  result[n] = (value[n] - key2[n]) ^ key1[n];
123  }
124 
125  return result;
126 }
127 
128 // This define allows to hide a string in the final binary file (in the release build).
129 // The define can be used for strings with length up to 32 characters.<br>
130 // Larger strings must either be separated into smaller sub-strings or must be hided by application of Binary::decrypt().<br>
131 // To hide a string you need random characters within the value range [1, 255].
132 // @code
133 // // the following instruction hides the string "test"
134 //
135 // // we need a standard string value as target
136 // std::string value;
137 //
138 // // we provided the actual string to hide, the target value, a set of random character with the same amount of characters as the length of the string to hide, a second set of random characters, and finally the length of the string to hide plus two
139 // HIDE_STRING_32("test", value, "\x3C\x9F\x63\x44", "\x25\xEA\x48\x7B", 4 + 2);
140 //
141 // // now the value holds the string "test"
142 // ocean_assert(value == "test");
143 // @endcode
144 // @see Binary.
145 // @ingroup base
146 #define HIDE_STRING_32(value, result, key1, key2, sizePlusTwo) do { ocean_assert(wcslen(value) + 2 == sizePlusTwo); const wchar_t encryptedValue[] = ENCRYPT_STRING_32(value, key1, key2); result = Ocean::decryptString32(encryptedValue, sizePlusTwo, key1, key2); ocean_assert(result == value); } while (false)
147 
148 }
149 
150 #endif // META_OCEAN_BASE_BINARY_H
This class implements functions for binaries like functions allowing to hide information in the binar...
Definition: Binary.h:24
static std::wstring encrypt(const wchar_t *decryptedValue, const size_t size, std::wstring &key0, std::wstring &key1)
Encrypts a given string by a simple (not very secure) encrypt/decrypt method.
Definition: Binary.h:62
static std::wstring decrypt(const wchar_t *encryptedValue, const size_t size, const wchar_t *key0, const wchar_t *key1)
Decrypts a given string by a simple (not very secure) encrypt/decrypt method.
Definition: Binary.h:47
static unsigned int random(const unsigned int maxValue)
Returns one random integer value with specified maximum value.
std::wstring decryptString32(const wchar_t *value, const unsigned int sizePlusTwo, const wchar_t *key1, const wchar_t *key2)
This function is part of the HIDE_STRING_32 define used internally.
Definition: Binary.h:115
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15