2017-11-02 2 views
0

ich ein sehr einfaches Verschleierungsmethode erstellt werden soll, im Grunde nur, ich will xor jede Saite soC++ #define, die einen String bekommt

printf("%s\n", OBF("Test")); 

so etwas wie Will erzeugen Aufruf

printf("%s\n", unxor("\x65\x54\x42\x45")); 

I XORed die String mit 1 in diesem Fall

+2

Ich glaube nicht, gibt es eine Möglichkeit, um eine Schleife über einen String im Preprocessor. – Barmar

+0

Schreiben Sie eine Funktion. –

+0

Könnte etwas mit [Präprozessor Metaprogrammierung] (http://www.boost.org/libs/preprocessor) abziehen, aber ich bin mir nicht sicher. –

Antwort

1

Mit modernen C++ können Sie es ohne Makros wie folgt schreiben:

#include <iostream> 
#include <array> 
#include <utility> 
#include <cstddef> 

constexpr const char key_byte{'1'}; 

template<::std::size_t VArrayItemsCount, ::std::size_t... Is> constexpr auto 
obf_impl 
(
    ::std::index_sequence<Is...> 
, char const (& sz_text)[VArrayItemsCount] 
) 
-> ::std::array<char, VArrayItemsCount> 
{ 
    return(::std::array<char, VArrayItemsCount>{static_cast<char>(sz_text[Is]^key_byte)..., '\0'}); 
} 

template<::std::size_t VArrayItemsCount> constexpr auto 
obf 
(
    char const (& sz_text)[VArrayItemsCount] 
) 
-> ::std::array<char, VArrayItemsCount> 
{ 
    return 
    (
     obf_impl<VArrayItemsCount> 
     (
      ::std::make_index_sequence<VArrayItemsCount - ::std::size_t{1}>() 
     , sz_text 
     ) 
    ); 
} 

int main() 
{ 
    constexpr const auto hello{obf("hello")}; 
    ::std::cout << hello.data() << ::std::endl; 
    return 0; 
} 

Run this code in online compiler