Napisałem prostą klasę, którym zadaniem jest szyfrowanie stringów, szyfrowanie ma polegać na tym, że stringi są szyfrowane przed kompilacją kodu źródłowego a odszyfrowywanie dopiero przy jego 1 użyciu. Szyfruje zwykłym XOR'em, w STAŁEJ zmiennej trzymam klucz który jest liczbą całkowitą, lecz ta stała liczba zmienia swoją wartość w różnych częściach programu? O to kod:
string.h:
#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED
#include <initializer_list>
#include <windows.h>
#include <iostream>
using namespace std;
#define Random(s) ((unsigned long)((s[0] * s[4] * s[6]) ^ (s[1] * s[3] * s[7])))
const unsigned long Key = Random(__TIME__);
class EncryptedString
{
public:
EncryptedString();
EncryptedString(std::initializer_list<char> str);
~EncryptedString();
unsigned long Length() const { return _length; }
operator const char* ();
private:
void Decrypt();
bool _isEncrypted;
unsigned long _length;
char* _string;
};
template <char c>
struct Encrypt { enum { Char = c ^ Key }; };
#define E(c) ((char)(Encrypt<c>::Char))
#define EncryptString1(s) { E(s[0]), ('\0') }
#define EncryptString2(s) { E(s[0]), E(s[1]), ('\0') }
#define EncryptString3(s) { E(s[0]), E(s[1]), E(s[2]), ('\0') }
#define EncryptString4(s) { E(s[0]), E(s[1]), E(s[2]), E(s[3]), ('\0') }
#define EncryptString5(s) { E(s[0]), E(s[1]), E(s[2]), E(s[3]), E(s[4]), ('\0') }
#define EncryptString6(s) { E(s[0]), E(s[1]), E(s[2]), E(s[3]), E(s[4]), E(s[6]), ('\0') }
#endif // STRING_H_INCLUDED
string.cpp:
#include "string.h"
#include <iostream>
using namespace std;
EncryptedString::EncryptedString() : _isEncrypted(true), _length(0), _string(NULL)
{
}
EncryptedString::EncryptedString(std::initializer_list<char> str) : _isEncrypted(true)
{
_length = str.size() - 1;
_string = (char*)malloc(str.size());
unsigned long i = 0;
for(char c : str) _string[i++] = c;
}
EncryptedString::~EncryptedString()
{
if(_string != NULL) free(_string);
}
EncryptedString::operator const char* ()
{
if(_isEncrypted == true) Decrypt();
return _string;
}
void EncryptedString::Decrypt()
{
cout << "Wartosc stalego klucza w funkcji Decrypt: " << Key << endl << endl;
for(unsigned long i = 0; i < _length; i++)
{
_string[i] = _string[i] ^ Key;
}
_isEncrypted = false;
}
main.cpp:
#include <iostream>
#include "..\String.h"
using namespace std;
int main()
{
cout << "Wartosc stalego klucza w funkcji main: " << Key << endl << endl;
cout << "_____________________________________________" << endl;
EncryptedString str1 = { E('d'), E('u'), E('p'), E('a'), '\0' };
cout << "Wyświetl napis zaszyfrowany: " << str1 << endl;
cout << "______________________________________________" << endl;
char str2[] = { E('d'), E('u'), E('p'), E('a'), '\0' };
cout << "Tablica z tymi samymi znakami przed odszyfrowaniem: " << str2 << endl;
for(int i = 0; i < 4; i++) str2[i] ^= Key;
cout << "Tablica z tymi samymi znakami po odszyfrowaniu: " << str2 << endl;
cout << endl;
cout << "Wartosc stalego klucza w funkcji main: " << Key << endl << endl;
system("pause");
return 0;
}
Nie mam pojęcia co jest grane, że stała zmienna ma inną wartość w różnych miejscach :(