简体   繁体   English

Base64编码XXTEA加密的字符串错误

[英]Base64 encode a XXTEA encrypted string error

I would like to secure my data so I try to encrypt it with XXTEA. 我想保护我的数据,所以我尝试使用XXTEA对其进行加密。 I do this way: 我这样做:

  • inputString -> XXTEA encrypt -> outputString inputString-> XXTEA加密-> outputString
  • outputString -> XXTEA decrypt -> inputString outputString-> XXTEA解密-> inputString

Everything is encrypt and decrypt ok. 一切都可以加密和解密了。 But when I try to make a base64 encode the output after XXTEA encrypt it and base64 decode it before XXTEA decrypt, the result is wrong: 但是,当我尝试在XXTEA加密输出并使其在XXTEA解密之前将base64解码后,使base64编码输出时,结果是错误的:

  • input -> XXTEA encrypt -> base64 encode -> output 输入-> XXTEA加密-> base64编码->输出
  • output -> base64 decode -> XXTEA decrypt != input 输出-> base64解码-> XXTEA解密!=输入

When I test with http://www.tools4noobs.com/online_tools/xxtea_encrypt/ and http://www.tools4noobs.com/online_tools/xxtea_decrypt/ 当我使用http://www.tools4noobs.com/online_tools/xxtea_encrypt/http://www.tools4noobs.com/online_tools/xxtea_decrypt/进行测试时

My example's input string is hello and its final result is bjz/S2f3Xkxr08hu 我的示例的输入字符串是hello ,其最终结果是bjz/S2f3Xkxr08hu

But when I test with my code (see below), the final result is bjz/Sw== 但是当我用我的代码进行测试(见下文)时,最终结果是bjz/Sw==

Here is my encryption code : 这是我的encryption code

std::string ProjectUtils::encrypt_data_xxtea(std::string input, std::string secret) {
//Encrypt with XXTEA
xxtea_long retLength = 0;

unsigned char data[input.length()];
strncpy((char*)data, input.c_str(), sizeof(data));
xxtea_long dataLength = (xxtea_long) sizeof(data);

unsigned char key[secret.length()];
strncpy((char*)key, secret.c_str(), sizeof(key));
xxtea_long keyLength = (xxtea_long) sizeof(key);

unsigned char *encryptedData = xxtea_encrypt(data, dataLength, key, keyLength, &retLength);

//Encode base64
char* out = NULL;
base64Encode(encryptedData, sizeof(encryptedData), &out);

CCLOG("xxtea encrypted data: %s", out);
return out;

} }

Here is my decryption code : 这是我的decryption code

char* ProjectUtils::decrypt_data_xxtea(std::string input, std::string secret) {
//Decode base64
unsigned char* output = NULL;
base64Decode((unsigned char*)input.c_str(), (unsigned int)strlen(input.c_str()), &output);
xxtea_long dataLength = (xxtea_long) sizeof(output);

xxtea_long retLength = 0;

unsigned char key[secret.length()];
strncpy((char*)key, secret.c_str(), sizeof(key));
xxtea_long keyLength = (xxtea_long) sizeof(key);

//Decrypt with XXTEA
char *decryptedData = reinterpret_cast<char*>(xxtea_decrypt(output, dataLength, key, keyLength, &retLength));

CCLOG("xxtea decrypted data: %s", decryptedData);
return decryptedData;

} }

Do you know what is wrong with my code? 您知道我的代码有什么问题吗? Any help would be appreciated! 任何帮助,将不胜感激! Thanks very much. 非常感谢。

here is full working code on cocos2d-x 3.4 这是cocos2d-x 3.4的完整工作代码

std::string UserProfile::encryptString(std::string input, std::string secret) {
    //Encrypt with XXTEA
    xxtea_long retLength = 0;

    unsigned char data[input.length()];
    strncpy((char*)data, input.c_str(), sizeof(data));
    xxtea_long dataLength = (xxtea_long) sizeof(data);

    unsigned char key[secret.length()];
    strncpy((char*)key, secret.c_str(), sizeof(key));
    xxtea_long keyLength = (xxtea_long) sizeof(key);

    unsigned const char *encryptedData = reinterpret_cast<unsigned const char*>(xxtea_encrypt(data, dataLength, key, keyLength, &retLength));

    char* out = NULL;
    cocos2d::base64Encode(encryptedData, retLength, &out); // use real length returned by xxtea_encrypt
    CCLOG("xxtea encrypted data: [%s][%s]", encryptedData, out);


    std::string outStr(out); // make string object
    CCLOG("xxtea encrypted data: [%s][%s]", encryptedData, outStr.c_str());


    setStr(KEY, outStr); // function that store value in user defaults


    std::string revertStr = getStr(KEY); // get string value back
    CCLOG("xxtea revertStr: [%s]", revertStr.c_str());

    unsigned char* output = NULL;
    int outLength = cocos2d::base64Decode((unsigned char*)revertStr.c_str(), (unsigned int)strlen(revertStr.c_str()), &output); // get real length of decoded string
    char *decryptedData = reinterpret_cast<char*>(xxtea_decrypt(output, outLength, key, keyLength, &retLength)); // use it

    CCLOG("xxtea decrypted data: %s", decryptedData); // string the same as original for me

    return "";
}

thanks for your code, it works for me) 感谢您的代码,它对我有用)

I have replaced length of base64 string by actual retLength 我已经用实际的retLength替换了base64字符串的长度

base64Encode(encryptedData, retLength, &out);

and back, get actual size as well 然后返回实际尺寸

int outLength = cocos2d::base64Decode((unsigned char*)revertStr.c_str(), (unsigned int)strlen(revertStr.c_str()), &output);
char *decryptedData = reinterpret_cast<char*>(xxtea_decrypt(output, outLength, key, keyLength, &retLength)); 

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

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