Cur*_*519 15 c++ reverse-engineering constexpr c++11 c++14
我想要一个编译时字符串加密,这样我就可以在我的代码中编写:
const auto encryptedInvalidLicense = ENCRYPT("Invalid license");
std::cout << encryptedInvalidLicense.decrypt() << std::endl; // outputs "Invalid license"
Run Code Online (Sandbox Code Playgroud)
并且字符串"Invalid license"不会出现在二进制文件中.预构建可能是答案,但我正在寻找一个纯c ++ constexpr
解决方案来解决这个问题,它将得到VS2015的支持.
有什么建议?
我已经研究过编译时字符串加密,它没有为问题提供constexpr解决方案.
我还研究了http://www.unknowncheats.me/forum/c-and-c/113715-compile-time-string-encryption.html.虽然它是一个constexpr解决方案,但VS2015仍然将字符串纯文本添加到二进制文件中.
Chr*_*eck 16
我是这样做的:
1.)使用str_const
此处描述的constexpr字符串操作模板:在C++中方便地声明编译时字符串
码:
class str_const {
// constexpr string
private:
const char * const p_;
const std::size_t sz_;
public:
template <std::size_t N>
constexpr str_const(const char(&a)[N])
: p_(a)
, sz_(N - 1)
{}
constexpr char operator[](std::size_t n) const { return n < sz_ ? p_[n] : throw std::out_of_range(""); }
constexpr std::size_t size() const { return sz_; }
constexpr const char * get() const { return p_; }
};
Run Code Online (Sandbox Code Playgroud)
这使您可以在constexpr函数中执行str_const message = "Invalid license"
和操作message
.
2.)使用宏生成一个简单的编译时伪随机生成器__TIME__
并__LINE__
生成种子.这里有详细描述:在编译时用C++生成随机数
他们提供了一些基于模板的代码.
3.)创建一个结构,使用一个constexpr
ctor,它接受const char []
与模板类似的大小和模板本身str_const
,或者只接受一个str_const
,并生成两个str_const
它是它的成员变量.
str_const
长度的n
含有伪随机无符号字符,产生使用伪随机发生器,其中n
是输入的长度.("噪音串")str_const
长度,n
包含具有噪声字符的输入字符的入口和(作为无符号字符).("密文")然后它有一个成员函数decrypt
,它不需要是constexpr,并且可以返回a std::string
,它只是从密文的相应字符中减去噪声字符串的每个字符并返回结果字符串.
如果您的编译器仍然将原始字符串文字存储在二进制文件中,则意味着要么存储输入字符串文字(构造函数参数),我认为它不应该执行,因为它是临时的,或者它基本上内联decrypt
函数,你应该能够通过使用函数指针对其进行模糊处理或标记它volatile
或类似来防止它.
编辑:我不确定标准是否要求临时constexpr对象不应出现在二进制文件中.其实我现在很好奇.我的期望是,至少在发布版本中,一个好的编译器应该在不再需要它们时删除它们.
编辑:那么,你已经接受了我的回答.但无论如何,为了完整性,这里有一些实现上述想法的源代码,仅使用C++ 11标准.它适用于gcc-4.9和clang-3.6,即使在禁用优化时也是如此,我几乎可以说.
#include <array>
#include <iostream>
#include <string>
typedef uint32_t u32;
typedef uint64_t u64;
typedef unsigned char uchar;
template<u32 S, u32 A = 16807UL, u32 C = 0UL, u32 M = (1UL<<31)-1>
struct LinearGenerator {
static const u32 state = ((u64)S * A + C) % M;
static const u32 value = state;
typedef LinearGenerator<state> next;
struct Split { // Leapfrog
typedef LinearGenerator< state, A*A, 0, M> Gen1;
typedef LinearGenerator<next::state, A*A, 0, M> Gen2;
};
};
// Metafunction to get a particular index from generator
template<u32 S, std::size_t index>
struct Generate {
static const uchar value = Generate<LinearGenerator<S>::state, index - 1>::value;
};
template<u32 S>
struct Generate<S, 0> {
static const uchar value = static_cast<uchar> (LinearGenerator<S>::value);
};
// List of indices
template<std::size_t...>
struct StList {};
// Concatenate
template<typename TL, typename TR>
struct Concat;
template<std::size_t... SL, std::size_t... SR>
struct Concat<StList<SL...>, StList<SR...>> {
typedef StList<SL..., SR...> type;
};
template<typename TL, typename TR>
using Concat_t = typename Concat<TL, TR>::type;
// Count from zero to n-1
template<size_t s>
struct Count {
typedef Concat_t<typename Count<s-1>::type, StList<s-1>> type;
};
template<>
struct Count<0> {
typedef StList<> type;
};
template<size_t s>
using Count_t = typename Count<s>::type;
// Get a scrambled character of a string
template<u32 seed, std::size_t index, std::size_t N>
constexpr uchar get_scrambled_char(const char(&a)[N]) {
return static_cast<uchar>(a[index]) + Generate<seed, index>::value;
}
// Get a ciphertext from a plaintext string
template<u32 seed, typename T>
struct cipher_helper;
template<u32 seed, std::size_t... SL>
struct cipher_helper<seed, StList<SL...>> {
static constexpr std::array<uchar, sizeof...(SL)> get_array(const char (&a)[sizeof...(SL)]) {
return {{ get_scrambled_char<seed, SL>(a)... }};
}
};
template<u32 seed, std::size_t N>
constexpr std::array<uchar, N> get_cipher_text (const char (&a)[N]) {
return cipher_helper<seed, Count_t<N>>::get_array(a);
}
// Get a noise sequence from a seed and string length
template<u32 seed, typename T>
struct noise_helper;
template<u32 seed, std::size_t... SL>
struct noise_helper<seed, StList<SL...>> {
static constexpr std::array<uchar, sizeof...(SL)> get_array() {
return {{ Generate<seed, SL>::value ... }};
}
};
template<u32 seed, std::size_t N>
constexpr std::array<uchar, N> get_key() {
return noise_helper<seed, Count_t<N>>::get_array();
}
/*
// Get an unscrambled character of a string
template<u32 seed, std::size_t index, std::size_t N>
char get_unscrambled_char(const std::array<uchar, N> & a) {
return static_cast<char> (a[index] - Generate<seed, index>::value);
}
*/
// Metafunction to get the size of an array
template<typename T>
struct array_info;
template <typename T, size_t N>
struct array_info<T[N]>
{
typedef T type;
enum { size = N };
};
template <typename T, size_t N>
struct array_info<const T(&)[N]> : array_info<T[N]> {};
// Scramble a string
template<u32 seed, std::size_t N>
class obfuscated_string {
private:
std::array<uchar, N> cipher_text_;
std::array<uchar, N> key_;
public:
explicit constexpr obfuscated_string(const char(&a)[N])
: cipher_text_(get_cipher_text<seed, N>(a))
, key_(get_key<seed,N>())
{}
operator std::string() const {
char plain_text[N];
for (volatile std::size_t i = 0; i < N; ++i) {
volatile char temp = static_cast<char>( cipher_text_[i] - key_[i] );
plain_text[i] = temp;
}
return std::string{plain_text, plain_text + N};
}
};
template<u32 seed, std::size_t N>
std::ostream & operator<< (std::ostream & s, const obfuscated_string<seed, N> & str) {
s << static_cast<std::string>(str);
return s;
}
#define RNG_SEED ((__TIME__[7] - '0') * 1 + (__TIME__[6] - '0') * 10 + \
(__TIME__[4] - '0') * 60 + (__TIME__[3] - '0') * 600 + \
(__TIME__[1] - '0') * 3600 + (__TIME__[0] - '0') * 36000) + \
(__LINE__ * 100000)
#define LIT(STR) \
obfuscated_string<RNG_SEED, array_info<decltype(STR)>::size>{STR}
auto S2 = LIT(("Hewwo, I'm hunting wabbits"));
int main() {
constexpr auto S1 = LIT(("What's up doc"));
std::cout << S1 << std::endl;
std::cout << S2 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5835 次 |
最近记录: |