Witam serdecznie
Szukam pomocy w przeanalizowaniu fragmentu kodu programu "Zaszyfrowany dziennik" Program dodaje,modyfikuje i usuwa wpisy które są numerowane i datowane a na końcu zapisuje wszystko w zaszyfrowanej postaci. Ogólnie wiem na czym polega sam XOR ale nie wiem jak tu zostało to zaimplementowane...
Np
tekst do szyfr. - programowanie
klucz szyfr. - qwerty
tekst zaszyfr - [ 33, 37, 42, 53, 38, 48, 58, 42, 37, 53, 63, 62, 32, ]
Oto kody:
XoR.cpp
#include "../include/XoRCode.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
// konstruktor
XoRCode::XoRCode():m(26)
{
cKey="";
key=0;
}
//funkcja szyfrujaca
const char * XoRCode::cCrypt(const char* str)
{
//pomocnicza zmienna
string mMesg(str);
int cVal;
int count=0;
ostringstream ss; // Konwersje int na string; Nagłówek sstream
tmp="";
tmp+="[ ";
for(int i=0;i<mMesg.length();++i)
{
if(count==cKey.length()-1)
count=0;
ss<<"";
ss.flush();
cVal=(((int)mMesg[i])^((int)(cKey[count])));
ss<<cVal;
tmp+=ss.str();
tmp+=", ";
count++;
ss.str("");
ss.clear();
}
tmp+=']';
return tmp.c_str();
}
const char* XoRCode::dCrypt(const char *cMesg)
{
//pomocnicza zmienna
string mMesg(cMesg);
int cVal;
int count=0;
tmp="";
string number="";
for(int i=2;i<mMesg.length();++i)
{
if(count==cKey.length()-1)
count=0;
if(mMesg[i]!=',')
number+=mMesg[i];
else
{
++i;
istringstream ss(number); // Konwersje string na int
ss>>cVal;
cVal=(cVal^((int)cKey[count]));
tmp+=(char)cVal;
number="";
++count;
}
}
return tmp.c_str();
}
void XoRCode::setKey(char* keys)
{
string tmp;
string mKey(keys);
transform(mKey.begin(), mKey.end(),
mKey.begin(), ::toupper);//wykonuje podaną funkcję toupper dla argumentów ze zbioru (mKey.begin(), mKey.end()) i zapisuje wyniki
//do zbioru zaczynającego się w mKey.begin()
if(mKey.length()<1)
{
throw string("Za krotkie haslo");
}
cKey=mKey;
}
int XoRCode::countHash(const char* key)
{
int sum=0;
string mKey(key);
transform(mKey.begin(), mKey.end(),
mKey.begin(), ::toupper);
for(int i=0;i<mKey.length();++i)
//sum+=((((long)pow(((int)mKey[i]-63),11))%2790003));
sum+=((((long)pow((double)((int)mKey[i]-63),11.0))%2790003));
return (sum * (-1));
}
char* XoRCode::getDescr()
{
return "XoR";
}
char* XoRCode::keyInfo()
{
return const_cast<char*>("Wybrano: Algorytm XOR Kluczem jest dowolny ciag liter");
}
XoRCode::~XoRCode()
{
}
Xor.h
#ifndef XORCODE_H_
#define XORONCODE_H_
#include "Code.h"
#include <map>
#include <string>
using namespace std;
class XoRCode : public CodeClass
{
//klucz
string cKey;
int key;
//ilosc znakow w alfabecie
int m;
string mask;
map<char,string> CodeMap;
map<char,string>::iterator it;
string tmp;
public:
XoRCode();
const char* cCrypt(const char *);
const char* dCrypt(const char *);
void setKey(char*);
int countHash(const char*);
char* getDescr();
char* keyInfo();
~XoRCode();
};
#endif