c ++简单凯撒密码算法 [英] c++ simple Caesar cipher algorithm
本文介绍了c ++简单凯撒密码算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 {
newchar [c] = char((int)chars [c])+ key); //我怀疑这里的某处是问题
}
std :: string out(newchar);
return out;
}
LOGI(encrypt:%s,Encrypt(hello,10).data());
LOGI(decrypt:%s,Encrypt(Encrypt(hello,10), - 10).data
输出:
code> encrypt:rovvyu @
decrypt:hellok
加密和我不知道如何ascii和整个字符的事情在c
解决方案
std :: string Encrypt(const std :: string& in,int key)
{
std :: string out(in);
for(int i = 0; i {
out [i] + = key;
}
return out;
}
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
解决方案
std::string Encrypt(const std::string & in, int key)
{
std::string out(in);
for(int i=0; i < in.length(); ++i)
{
out[i] += key;
}
return out;
}
这篇关于c ++简单凯撒密码算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文