2015-03-19 71 views
0

我想保護我的數據,所以我嘗試用XXTEA加密它。我這樣做的方法:Base64編碼XXTEA加密字符串錯誤

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

一切都是加密和解密確定。但是,當我試圖讓一個base64編碼後輸出XXTEA對其進行加密和BASE64 XXTEA解密之前對其進行解碼,結果是錯誤的:

  • 輸入 - > XXTEA加密 - >使用Base64編碼 - >輸出
  • 輸出 - >的base64解碼! - > XXTEA解密=輸入

當我與http://www.tools4noobs.com/online_tools/xxtea_encrypt/http://www.tools4noobs.com/online_tools/xxtea_decrypt/測試

我的例子的輸入字符串爲hello其最終的結果是bjz/S2f3Xkxr08hu

但是,當我用我的代碼(見下文)進行測試,最後的結果是bjz/Sw==

這是我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; 

}

這裏是我的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; 

}

你知道我的代碼有什麼問題嗎?任何幫助,將不勝感激! 非常感謝。

+0

我使用xxtea對base64進行編碼後進行編碼,因爲使用xxtea進行加密的結果具有特殊字符,如**©\ \ \ 327_1xA \ 344R **,我無法將其保存到userdefault中。有什麼辦法將一個字符串轉換爲正常格式的特殊字符? – 2015-03-20 08:47:00

回答

0

感謝您的代碼,它爲我的作品)

我已經實際retLength取代的base64字符串的長度

base64Encode(encryptedData, retLength, &out); 

和背部,得到實際大小以及

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)); 
+1

您能否詳細說明您的答案,並添加關於您提供的解決方案的更多描述? – abarisone 2015-06-16 07:23:16

1

這裏是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 ""; 
} 
+0

您能詳細解釋一下您提供的解決方案嗎? – abarisone 2015-06-16 08:14:45