【发布时间】:2012-09-27 12:33:00
【问题描述】:
我正在尝试制作一个非常简单的凯撒密码算法来加密和解密游戏中的玩家数据,但我得到了一些奇怪的结果。算法的任务很简单,只需向前或向后推动ascii 表。
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());
输出:
encrypt:rovvyu@
decrypt:hellok
我对加密知之甚少,我对 ascii 和整个字符在 c 中的工作原理知之甚少
【问题讨论】:
-
正在解密游戏的文本部分,如果不是,您为什么不使用已建立库中的现代加密技术?在当今时代,字母替换算法对任何保护都没有用处。哎呀,人们这样做是为了好玩in the newspaper。
-
sizeof(char) == 1,顾名思义。 -
你应该释放
newchar。out将制作自己的字符串副本。 -
如果你已经在使用
std::string为什么要malloc输出数组? -
@Scott ,我不想使用任何外部库,因为我想让它保持简单,我并不真正关心用户找到真实数据,我只是想让它对他们来说很困难.
标签: c++ encryption char