如何将xor加密从使用std::string转换为仅使用char或char*? 这样,我所使用的库本质上不能使用STD::String,因为它使用了一个略微贬值的C++版本,我需要将这个XOR函数从使用STD::String转换成只使用char或char *。我一直在尝试,但我不能找出我做错了什么,因为我得到了一个错误。代码如下: string encryptDecrypt(string toEncrypt) { char key[] = "DSIHKGDSHIGOK$%#%45434etG34th8349ty"; //Any chars will work string output = toEncrypt; for (int i = 0; i < toEncrypt.size(); i++) output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))]; return output; } 字符串加密解密(字符串到加密){ char key[]=“DSIHKGDSHIGOK$%#%45434etG34th8349ty”//任何字符都可以使用 字符串输出=加密; for(int i=0;i
如果有人能帮我,那就太好了。我不确定为什么我不能简单地将字符串更改为char* 编辑: 我尝试的是:如何将xor加密从使用std::string转换为仅使用char或char*? 这样,我所使用的库本质上不能使用STD::String,因为它使用了一个略微贬值的C++版本,我需要将这个XOR函数从使用STD::String转换成只使用char或char *。我一直在尝试,但我不能找出我做错了什么,因为我得到了一个错误。代码如下: string encryptDecrypt(string toEncrypt) { char key[] = "DSIHKGDSHIGOK$%#%45434etG34th8349ty"; //Any chars will work string output = toEncrypt; for (int i = 0; i < toEncrypt.size(); i++) output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))]; return output; } 字符串加密解密(字符串到加密){ char key[]=“DSIHKGDSHIGOK$%#%45434etG34th8349ty”//任何字符都可以使用 字符串输出=加密; for(int i=0;i,c++,string,char,xor,C++,String,Char,Xor,如果有人能帮我,那就太好了。我不确定为什么我不能简单地将字符串更改为char* 编辑: 我尝试的是: char * encryptDecrypt(char * toEncrypt) { char key[] = "DSIHKGDSHIGOK$%#%45434etG34th8349ty"; //Any chars will work char * output = toEncrypt; for (int i = 0; i < sizeof(toEncrypt);
char * encryptDecrypt(char * toEncrypt) {
char key[] = "DSIHKGDSHIGOK$%#%45434etG34th8349ty"; //Any chars will work
char * output = toEncrypt;
for (int i = 0; i < sizeof(toEncrypt); i++)
output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];
return output;
}
char*加密解密(char*toEncrypt){
char key[]=“DSIHKGDSHIGOK$%#%45434etG34th8349ty”//任何字符都可以使用
字符*输出=加密;
对于(int i=0;i
请注意,我并没有试图将std::string转换为char,我只是无法在该函数的任何实例中使用std::string。因此,我的问题没有得到回答。在标记问题答案之前,请仔细阅读我的问题…为什么不改为字符串输出=加密:
char *output = new char[std::strlen(toEncrypt) + 1];
std::strcpy(output, toEncrypt);
为什么不改为字符串输出=toEncrypt:
char *output = new char[std::strlen(toEncrypt) + 1];
std::strcpy(output, toEncrypt);
为什么不改为字符串输出=toEncrypt:
char *output = new char[std::strlen(toEncrypt) + 1];
std::strcpy(output, toEncrypt);
为什么不改为字符串输出=toEncrypt:
char *output = new char[std::strlen(toEncrypt) + 1];
std::strcpy(output, toEncrypt);
这里的问题是
char * output = toEncrypt;
这使得输出
指向加密
,而这不是您想要做的。您需要做的是分配一个新的字符*
,然后将的内容复制到输出中
char * encryptDecrypt(char * toEncrypt) {
char key[] = "DSIHKGDSHIGOK$%#%45434etG34th8349ty"; //Any chars will work
int string_size = std::strlen(toEncrypt);
char * output = new char[string_size + 1]; // add one for the null byte
std::strcpy(output, toEncrypt); //copy toEncrypt into output
for (int i = 0; i < string_size; i++)
output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];
return output;
}
char*加密解密(char*toEncrypt){
char key[]=“DSIHKGDSHIGOK$%#%45434etG34th8349ty”//任何字符都可以使用
int string_size=std::strlen(toEncrypt);
char*output=new char[string_size+1];//为空字节添加一个
std::strcpy(输出,toEncrypt);//将toEncrypt复制到输出中
对于(int i=0;i
因为我们在这里使用动态内存分配,所以我们需要确保调用方在执行此操作时删除内存,否则将导致内存泄漏。这里的问题是
char * output = toEncrypt;
这使得输出
指向加密
,而这不是您想要做的。您需要做的是分配一个新的字符*
,然后将的内容复制到输出中
char * encryptDecrypt(char * toEncrypt) {
char key[] = "DSIHKGDSHIGOK$%#%45434etG34th8349ty"; //Any chars will work
int string_size = std::strlen(toEncrypt);
char * output = new char[string_size + 1]; // add one for the null byte
std::strcpy(output, toEncrypt); //copy toEncrypt into output
for (int i = 0; i < string_size; i++)
output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];
return output;
}
char*加密解密(char*toEncrypt){
char key[]=“DSIHKGDSHIGOK$%#%45434etG34th8349ty”//任何字符都可以使用
int string_size=std::strlen(toEncrypt);
char*output=new char[string_size+1];//为空字节添加一个
std::strcpy(输出,toEncrypt);//将toEncrypt复制到输出中
对于(int i=0;i
因为我们在这里使用动态内存分配,所以我们需要确保调用方在执行此操作时删除内存,否则将导致内存泄漏。这里的问题是
char * output = toEncrypt;
这使得输出
指向加密
,而这不是您想要做的。您需要做的是分配一个新的字符*
,然后将的内容复制到输出中
char * encryptDecrypt(char * toEncrypt) {
char key[] = "DSIHKGDSHIGOK$%#%45434etG34th8349ty"; //Any chars will work
int string_size = std::strlen(toEncrypt);
char * output = new char[string_size + 1]; // add one for the null byte
std::strcpy(output, toEncrypt); //copy toEncrypt into output
for (int i = 0; i < string_size; i++)
output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];
return output;
}
char*加密解密(char*toEncrypt){
char key[]=“DSIHKGDSHIGOK$%#%45434etG34th8349ty”//任何字符都可以使用
int string_size=std::strlen(toEncrypt);
char*output=new char[string_size+1];//为空字节添加一个
std::strcpy(输出,toEncrypt);//将toEncrypt复制到输出中
对于(int i=0;i
因为我们在这里使用动态内存分配,所以我们需要确保调用方在执行此操作时删除内存,否则将导致内存泄漏。这里的问题是
char * output = toEncrypt;
这使得输出
指向加密
,而这不是您想要做的。您需要做的是分配一个新的字符*
,然后将的内容复制到输出中
char * encryptDecrypt(char * toEncrypt) {
char key[] = "DSIHKGDSHIGOK$%#%45434etG34th8349ty"; //Any chars will work
int string_size = std::strlen(toEncrypt);
char * output = new char[string_size + 1]; // add one for the null byte
std::strcpy(output, toEncrypt); //copy toEncrypt into output
for (int i = 0; i < string_size; i++)
output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];
return output;
}
char*加密解密(char*toEncrypt){
char key[]=“DSIHKGDSHIGOK$%#%45434etG34th8349ty”//任何字符都可以使用
int string_size=std::strlen(toEncrypt);
char*output=new char[string_size+1];//为空字节添加一个
std::strcpy(输出,toEncrypt);//将toEncrypt复制到输出中
对于(int i=0;i
由于我们在这里使用动态内存分配,因此需要确保调用方在执行此操作时删除内存,否则将导致内存泄漏。sizeof()是一个编译时运算符,用于计算其参数类型的大小。当您执行sizeof(toEncrypt)
时,实际上只是执行sizeof(char*)
——而不是您想要的字符串长度。您需要以某种方式指示toEncrypt
字符串的长度。以下是两种可能的解决方案:
向encryptDecrypt
添加一个整数参数,指定toEncrypt
的长度(以字符为单位)
如果您知道toEncrypt
永远不会包含空字节作为加密/解密的有效字符(不确定您的应用程序),并且可以假设toEncrypt
以空结尾,则可以使用<