| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : xor_string.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#include <wx/crt.h> |
| 27 |
#include "xor_string.h" |
| 28 |
|
| 29 |
XORString::XORString() |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
XORString::~XORString() |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
wxString XORString::Decrypt(const wxChar byte) const |
| 38 |
{ |
| 39 |
wxString dec = fromHexString(m_value); |
| 40 |
dec = XOR(dec, byte); |
| 41 |
return dec; |
| 42 |
} |
| 43 |
|
| 44 |
wxString XORString::Encrypt(const wxChar byte) const |
| 45 |
{ |
| 46 |
wxString enc = XOR(m_value, byte); |
| 47 |
enc = toHexString(enc); |
| 48 |
return enc; |
| 49 |
} |
| 50 |
|
| 51 |
XORString::XORString(const wxString& value) |
| 52 |
: m_value(value) |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
wxString XORString::toHexString(const wxString &value) const |
| 57 |
{ |
| 58 |
wxString output; |
| 59 |
for(size_t i=0; i<value.length(); ++i) { |
| 60 |
wxChar buf[5] = {0, 0, 0, 0, 0}; |
| 61 |
wxSprintf(buf, "%04X", (int)value[i]); |
| 62 |
output << buf; |
| 63 |
} |
| 64 |
return output; |
| 65 |
} |
| 66 |
|
| 67 |
wxString XORString::fromHexString(const wxString& hexString) const |
| 68 |
{ |
| 69 |
wxString output; |
| 70 |
size_t cnt = hexString.length() / 4; |
| 71 |
for (size_t i = 0; cnt > i; ++i) { |
| 72 |
wxString dec = hexString.Mid(i*4, 4); |
| 73 |
int hexVal; |
| 74 |
wxSscanf(dec, "%X", &hexVal); |
| 75 |
output << (wxChar)hexVal; |
| 76 |
} |
| 77 |
return output; |
| 78 |
} |
| 79 |
|
| 80 |
wxString XORString::XOR(const wxString& str, const wxChar KEY) const |
| 81 |
{ |
| 82 |
wxString output; |
| 83 |
for(size_t i=0; i<str.length(); ++i) { |
| 84 |
|
| 85 |
wxChar ch = str[i]; |
| 86 |
wxChar x = (ch ^ KEY); |
| 87 |
output << x; |
| 88 |
} |
| 89 |
return output; |
| 90 |
} |