std::string encodeForSaving(const char* value, const char* user_token) { std::string res(""); if ((user_token != NULL) && (strlen(user_token) > 0)) { if (value == NULL || strlen(value) == 0) { return res; } DataTransformer* enc = DataTransformerFactory::getEncoder(DT_DES); TransformationInfo info; info.size = (long)strlen(value)*sizeof(char); info.password = user_token; char* v = strdup(value); char* desValue = enc->transform(v, info); DataTransformer* b64Enc = DataTransformerFactory::getEncoder(DT_B64); char* b64Value = b64Enc->transform(desValue, info); free(v); if (desValue) { free(desValue); } res = b64Value; delete [] b64Value; } else { res = value; } return res; }
USE_NAMESPACE // // IMPORTANT: this test case encodes/decodes a UNICODE string; make sure // to update it to UTF8 strings when used with such content. // void testEncryption() { char* clearText = "This is clear text.\nLet's see if encryption/decryption works!"; char* password = "******"; DataTransformer* b64e = DataTransformerFactory::getEncoder("b64"); DataTransformer* b64d = DataTransformerFactory::getDecoder("b64"); DataTransformer* dese = DataTransformerFactory::getEncoder("des"); DataTransformer* desd = DataTransformerFactory::getDecoder("des"); TransformationInfo infoe, infod; infoe.size = strlen(clearText)*sizeof(char); infoe.password = password; LOG.info("Clear text"); LOG.info("%s", clearText); char* desText = dese->transform(clearText, infoe); char* b64Text = b64e->transform(desText, infoe); LOG.info("Clear text"); LOG.info("%s", b64Text); delete [] desText; infod.size = infoe.size; infod.password = infoe.password; desText = b64d->transform(b64Text, infod); clearText = desd->transform(desText, infod); char* clearString = new char[infod.size/sizeof(char)+1]; strncpy(clearString, clearText, infod.size/sizeof(char)); clearString[infod.size/sizeof(char)] = 0; LOG.info("Clear text"); LOG.info("%s", clearString); delete [] clearString; delete [] clearText; delete [] b64Text; delete [] desText; }
std::string decodeAfterReading(const char *value, const char* user_token) { std::string res(value); if ((user_token != NULL) && (strlen(user_token) > 0)) { if (value == NULL || strlen(value) == 0) { return ""; } // Password is stored as b64(des(password)) DataTransformer* b64Dec = DataTransformerFactory::getDecoder(DT_B64); TransformationInfo info; char* v = strdup(value); info.size = (long)strlen(v)*sizeof(char); char* b64DecValue = b64Dec->transform(v, info); DataTransformer* dec = DataTransformerFactory::getDecoder(DT_DES); info.password = user_token; char* desValue = dec->transform(b64DecValue, info); if (desValue != NULL && info.size > 0) { desValue[info.size] = 0; } free(v); if (desValue != NULL) { res = desValue; } delete [] b64DecValue; if (info.newReturnedData == true) { delete [] desValue; } } return res; }