I am trying to make a very simple Caesar cipher algorithm to encrypt and decrypt the player’s data in my game , but i am getting some weird results.The task of the algorithm is simple, just push foward or backwards the characters in the ascii table.
std::string Encrypt(std::string in,int key)
{
const char* chars=in.data();
char* newchar=(char*)malloc(sizeof(char)*in.length());
for(int c=0;c<in.length();c++)
{
newchar[c]=char(((int)chars[c])+key);//I suspect somewhere here is the problem
}
std::string out(newchar);
return out;
}
LOGI("encrypt:%s",Encrypt("hello",10).data());
LOGI("decrypt:%s",Encrypt(Encrypt("hello",10),-10).data());
Output:
encrypt:rovvyu@
decrypt:hellok
I dont know much about encryption and i know less about how ascii and the whole character thing works in c
1 Answer