grom Опубликовано 1 сентября, 2018 Поделиться Опубликовано 1 сентября, 2018 Мне нужно зашифровать строку, а потом если что расшифровать. Как я могу это сделать? Язык Visual С++. Ссылка на комментарий Поделиться на другие сайты Поделиться
JustHack Опубликовано 1 сентября, 2018 Поделиться Опубликовано 1 сентября, 2018 (изменено) Можешь как-то так #define fo 160 - 7 ^ st.length() _int c_l(_int *ss){ _int st{ *ss }; for (auto& m : st) m ^= fo; return !(-1*7!=7-+1) ? c_l(&st) : *ss = st; } Изменено 2 сентября, 2018 пользователем JustHack 1 Ссылка на комментарий Поделиться на другие сайты Поделиться
grom Опубликовано 1 сентября, 2018 Автор Поделиться Опубликовано 1 сентября, 2018 Мне на с++ нужно. Что это за язык. 1 Ссылка на комментарий Поделиться на другие сайты Поделиться
partoftheworlD Опубликовано 1 сентября, 2018 Поделиться Опубликовано 1 сентября, 2018 Спойлер template <int XORSTART, int BUFLEN, int XREFKILLER> class Xorstr { Xorstr(); public: char s[BUFLEN]; explicit Xorstr(const char * xs); ~Xorstr() { for (auto i = 0; i < BUFLEN; i++) { s[i] = 0; } } }; template <int XORSTART, int BUFLEN, int XREFKILLER> Xorstr<XORSTART, BUFLEN, XREFKILLER>::Xorstr(const char * xs) { auto xvalue = XORSTART; auto i = 0; for (; i < (BUFLEN -1) i++) { s[i] = xs[i - XREFKILLER] ^ xvalue; xvalue += 1; xvalue %= 256; } s[BUFLEN - 1] = 0; } #define XOR(a) a Ссылка на комментарий Поделиться на другие сайты Поделиться
JustHack Опубликовано 1 сентября, 2018 Поделиться Опубликовано 1 сентября, 2018 (изменено) [censored] Изменено 11 апреля, 2020 пользователем JustHack Ссылка на комментарий Поделиться на другие сайты Поделиться
Гость Ko3eBa9n Опубликовано 12 ноября, 2018 Поделиться Опубликовано 12 ноября, 2018 (изменено) Шифрование строк посредством XOR (Искл. "ИЛИ") : Пример реализации на С++ (Код тестировался на Visual Studio 2010 Utimate) Спойлер #include <iostream> using namespace std; #undef KEY #undef BUFLEN template <int XORSTART, int BUFLEN, int XREFKILLER> class XorStr { private: XorStr(); public: char s[BUFLEN]; XorStr(const char* xs); #ifndef DEBUG_OPTION ~XorStr(){ for(int i=0;i<BUFLEN;i++)s[i]=0;} // clear string from stack #endif }; template <int XORSTART, int BUFLEN, int XREFKILLER> XorStr<XORSTART,BUFLEN,XREFKILLER>::XorStr(const char* xs) // рассшифровка { int xvalue = XORSTART; int i = 0; for(;i<(BUFLEN-1);i++) { s[i] = xs[i-XREFKILLER]^xvalue; xvalue += 1; xvalue %= 256; } s[BUFLEN-1] = 0; } int main(int argc, char **argv) { cout<<"heyman"<<endl<<endl; // обычная строка cout<<(/*heyman*/XorStr<0x36,8,0x0AB9466D>("\x5E\x52\x41\x54\x5B\x55\x36"+0x0AB9466D).s); // зашифрованная строка посредство XOR(искл. ИЛИ) system("pause"); return 0; } !!! ВАЖНО !!! Для того, чтобы ЗАШИФРОВАТЬ строку, то есть привести строку к выражению по типу как в примере : "/*heyman*/XorStr<0x36,8,0x0AB9466D>("\x5E\x52\x41\x54\x5B\x55\x36"+0x0AB9466D).s" , создаём html страницу, то есть тупо копируйте код в блокнот, который указан ниже и сохраняете его как (пример: "xorgen.html") и запускаете в браузере, после чего на форме вводим строку которую хотим зашифровать и жмём на кнопочку "****** CONVERT TO XOR STRING ******" Спойлер <HTML> <HEAD> <SCRIPT language=JavaScript> var symbols = " !\"#$%&'()*+'-./0123456789:;<=>?@"; function toAscii (xx) { var loAZ = "abcdefghijklmnopqrstuvwxyz"; symbols+= loAZ.toUpperCase(); symbols+= "[\\]^_`"; symbols+= loAZ; symbols+= "{|}~"; var loc; loc = symbols.indexOf(xx); if (loc >-1) { Ascii_Decimal = 32 + loc; return (32 + loc); } return(0); // If not in range 32-126 return ZERO } function Dec2Hex(Decimal) { var hexChars = "0123456789ABCDEF"; var a = Decimal % 16; var b = (Decimal - a)/16; hex = "" + hexChars.charAt(b) + hexChars.charAt(a); L = hexChars.charAt(a); H = hexChars.charAt(b); return hex; } function randByte() { return Math.floor(Math.random()*256%256); } function blub(form) { s1 = form.inp.value; xvaluestart = randByte(); xrefkill = "0x" + Dec2Hex(randByte()) + Dec2Hex(randByte()) + Dec2Hex(randByte()) + Dec2Hex(randByte()); finallen = s1.length+1; hexsequence = '"'; xvalue = xvaluestart; for(i=0;i<s1.length;i++) { ch = s1.substr(i,1); var chval; if(ch=="\\"){ i++; ch = s1.substr(i,1); if(ch=="0") { chval = 0; } else if(ch=="n") { chval = 10; } else if(ch=="\\") { chval = toAscii("\\"); } else if(ch=="r") { chval = 13; } else { javascript:alert("invalid control sequence: \\"+ch); } --finallen; } else if(ch=="|"){ chval=0; } else { chval = toAscii(ch); if(chval==0) { form.ans.value = "invalid character: "+ch; return; } } chval ^=xvalue; xvalue += 1; xvalue %= 256; hexsequence += "\\x"+ Dec2Hex(chval); } hexsequence += '"'; s2 = "/*"+s1+"*/XorStr<0x" + Dec2Hex(xvaluestart) + "," + finallen + ","+xrefkill+'>(' s2 += hexsequence + "+" + xrefkill + ").s"; form.ans.value = s2; form.ans.focus(); form.ans.select(); } </SCRIPT> </HEAD> <CENTER> <FORM name=formx> <INPUT name=inp size=20 value="Insert String"> <br><br> <INPUT onclick=blub(this.form) type=button value="****** CONVERT TO XOR STRING ******"><br> <INPUT name=ans size=220 ?number?> </FORM> </CENTER> Изменено 12 ноября, 2018 пользователем Ko3eBa9n Пункт правил 1.5, в следующий раз предупреждение Ссылка на комментарий Поделиться на другие сайты Поделиться
re3ake Опубликовано 30 декабря, 2018 Поделиться Опубликовано 30 декабря, 2018 Credits: https://github.com/SMBB/R6SBase/blob/master/XorStr.h Спойлер #pragma once #include <string> #include <array> #include <cstdarg> #define BEGIN_NAMESPACE( x ) namespace x { #define END_NAMESPACE } BEGIN_NAMESPACE(XorCompileTime) constexpr auto time = __TIME__; constexpr auto seed = static_cast< int >(time[7]) + static_cast< int >(time[6]) * 10 + static_cast< int >(time[4]) * 60 + static_cast< int >(time[3]) * 600 + static_cast< int >(time[1]) * 3600 + static_cast< int >(time[0]) * 36000; // 1988, Stephen Park and Keith Miller // "Random Number Generators: Good Ones Are Hard To Find", considered as "minimal standard" // Park-Miller 31 bit pseudo-random number generator, implemented with G. Carta's optimisation: // with 32-bit math and without division template < int N > struct RandomGenerator { private: static constexpr unsigned a = 16807; // 7^5 static constexpr unsigned m = 2147483647; // 2^31 - 1 static constexpr unsigned s = RandomGenerator< N - 1 >::value; static constexpr unsigned lo = a * (s & 0xFFFF); // Multiply lower 16 bits by 16807 static constexpr unsigned hi = a * (s >> 16); // Multiply higher 16 bits by 16807 static constexpr unsigned lo2 = lo + ((hi & 0x7FFF) << 16); // Combine lower 15 bits of hi with lo's upper bits static constexpr unsigned hi2 = hi >> 15; // Discard lower 15 bits of hi static constexpr unsigned lo3 = lo2 + hi; public: static constexpr unsigned max = m; static constexpr unsigned value = lo3 > m ? lo3 - m : lo3; }; template <> struct RandomGenerator< 0 > { static constexpr unsigned value = seed; }; template < int N, int M > struct RandomInt { static constexpr auto value = RandomGenerator< N + 1 >::value % M; }; template < int N > struct RandomChar { static const char value = static_cast< char >(1 + RandomInt< N, 0x7F - 1 >::value); }; template < size_t N, int K > struct XorString { private: const char _key; std::array< char, N + 1 > _encrypted; constexpr char enc(char c) const { return c ^ _key; } char dec(char c) const { return c ^ _key; } public: template < size_t... Is > constexpr __forceinline XorString(const char* str, std::index_sequence< Is... >) : _key(RandomChar< K >::value), _encrypted{ enc(str[Is])... } { } __forceinline decltype(auto) decrypt(void) { for (size_t i = 0; i < N; ++i) { _encrypted[i] = dec(_encrypted[i]); } _encrypted[N] = '\0'; return _encrypted.data(); } }; //-------------------------------------------------------------------------------- //-- Note: XorStr will __NOT__ work directly with functions like printf. // To work with them you need a wrapper function that takes a const char* // as parameter and passes it to printf and alike. // // The Microsoft Compiler/Linker is not working correctly with variadic // templates! // // Use the functions below or use std::cout (and similar)! //-------------------------------------------------------------------------------- static auto w_printf = [](const char* fmt, ...) { va_list args; va_start(args, fmt); vprintf_s(fmt, args); va_end(args); }; static auto w_printf_s = [](const char* fmt, ...) { va_list args; va_start(args, fmt); vprintf_s(fmt, args); va_end(args); }; static auto w_sprintf = [](char* buf, const char* fmt, ...) { va_list args; va_start(args, fmt); vsprintf(buf, fmt, args); va_end(args); }; static auto w_sprintf_s = [](char* buf, size_t buf_size, const char* fmt, ...) { va_list args; va_start(args, fmt); vsprintf_s(buf, buf_size, fmt, args); va_end(args); }; #ifdef NDEBUG #define XorStr( s ) ( XorCompileTime::XorString< sizeof( s ) - 1, __COUNTER__ >( s, std::make_index_sequence< sizeof( s ) - 1>() ).decrypt() ) #else #define XorStr( s ) ( s ) #endif END_NAMESPACE Ссылка на комментарий Поделиться на другие сайты Поделиться
Рекомендуемые сообщения