简体   繁体   English

如何将使用std :: string的xor加密转换为char或char *?

[英]How do I convert xor encryption from using std::string to just char or char *?

So essentially with the libraries that i'm working with I cannot use std::string, as it uses a somewhat depreciated version of C++ I need to convert this xor function from using std::string to just using char or char *. 因此,从本质上讲,我正在使用的库不能使用std :: string,因为它使用的C ++有点贬值,我需要将此xor函数从使用std :: string转换为仅使用char或char *。 I have been trying but I cannot figure out what I am doing wrong, as I get an error. 我一直在尝试,但是由于出现错误,我无法弄清楚自己在做什么错。 Here is the code: 这是代码:

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;
}

If anyone could help me out, that would be great. 如果有人可以帮助我,那将很棒。 I am unsure as to why I cannot do it by simply changing the strings to char *. 我不确定为什么不能通过简单地将字符串更改为char *来做到这一点。

Edit: 编辑:

What I have tried is: 我试过的是:

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; 
}

Please note I am not trying to convert an std::string to char, I simply cannot use std::string in any instance of this function. 请注意,我不是试图将std :: string转换为char,我只是不能在此函数的任何实例中使用std :: string。 Therefore, my question is not answered. 因此,我的问题没有得到回答。 Please read my question more carefully before marking it answered... 在标记回答之前,请更仔细地阅读我的问题...

The issue here is 这里的问题是

char * output = toEncrypt; 

This is making output point to toEncrypt which is not what you want to do. 这使output指向toEncrypt ,这不是您想要执行的操作。 What you need to do is allocate a new char* and then copy the contents of toEncrypt into output 您需要做的是分配一个新的char* ,然后将toEncrypt的内容复制到output

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; 
}

Live Example 现场例子

Since we are using dynamic memory allocation here we need to make sure that the caller deletes the memory when done otherwise it will be a memory leak. 由于我们在这里使用动态内存分配,因此我们需要确保调用者在完成后删除内存,否则将导致内存泄漏。

sizeof() is a compile-time operator that evaluates the size of the type of its argument. sizeof()是一个编译时运算符,用于评估其参数类型的大小。 When you do sizeof(toEncrypt) , you're really just doing sizeof(char*) -- not the length of the string, which is what you want. 当您执行sizeof(toEncrypt) ,您实际上只是在执行sizeof(char*) ,而不是您想要的字符串长度。 You'll need to somehow indicate how long the toEncrypt string is. 您需要以某种方式指示toEncrypt字符串有多长时间。 Here are two possible solutions: 这是两个可能的解决方案:

  1. Add an integer argument to encryptDecrypt specifying the length of toEncrypt in characters. 添加的整数参数encryptDecrypt指定的长度toEncrypt在字符。

  2. If you know that toEncrypt will never contain the null byte as a valid character for encryption / decryption (not sure of your application) and can assume that toEncrypt is null-terminated, you could use the strlen function to determine string length at runtime. 如果您知道toEncrypt会包含空字节作为用于加密/解密的有效字符(不确定您的应用程序),并且可以假定toEncrypt是空终止的,则可以在运行时使用strlen函数确定字符串长度。

I'd recommend option 1, as strlen can introduce security holes if you're not careful, and also because it allows the use of null bytes within your string arguments. 我建议使用选项1,因为如果您不小心, strlen可能会引入安全漏洞,并且还因为它允许在字符串参数中使用空字节。

What error are you getting? 你遇到了什么错误? You can easily use a char* to do the same thing, I've included a sample program that verifies the functionality. 您可以轻松地使用char *来完成相同的操作,我提供了一个示例程序来验证功能。 This was built under VS2012. 这是在VS2012下构建的。

#include <string>
#include <stdio.h>

std::string encryptDecrypt( std::string toEncrypt)
{
    char key[] = "DSIHKGDSHIGOK$%#%45434etG34th8349ty"; //Any chars will work
    std::string output = toEncrypt;

    for (int i = 0; i < toEncrypt.size(); i++)
        output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];

    return output;
}

void encryptDecrypt( char* toEncrypt )
{
    char key[] = "DSIHKGDSHIGOK$%#%45434etG34th8349ty"; //Any chars will work
    int len = strlen( toEncrypt );

    for (int i = 0; i < len; i++)
        toEncrypt[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];
}

int main( int argc, char* argv[] )
{
    const char* sample = "This is a sample string to process";
    int len = strlen( sample );
    char* p = new char[ len + 1 ];
    p[len] = '\0';
    strcpy( p, sample );

    std::string output = encryptDecrypt( sample );
    encryptDecrypt( p );

    bool match = strcmp(output.c_str(), p) == 0;
    printf( "The two encryption functions %smatch.\n", match ? "" : "do not "   );

    return 0;
}

Why not instead of string output = toEncrypt : 为什么不代替字符串output = toEncrypt:

char *output = new char[std::strlen(toEncrypt) + 1];
std::strcpy(output, toEncrypt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM