dimanche 22 février 2015

android - how to escape the ' character while saving encrypted text in sqlLite

I am trying to save encrypted data in sqlLite database. So when the entered data is encrypted, some character might change into ' character which prevents the insertion of record in the database.


Now, the problem aggravates as I cannot escape the character by finding it in the encrypted text because if I escape the character then during decryption the output text will get corrupted.



char sql[] = "INSERT INTO ";
//strcpy(sql, "INSERT INTO ");
strcat(sql, _TABLE_NAME_VALUE);
strcat(sql, "(");
strcat(sql, _source_id_value);
strcat(sql, ",");
strcat(sql, _command_id_value);
strcat(sql, ",");
strcat(sql, _fingerprint_value);
strcat(sql, ",");
strcat(sql, _app_id_value);
strcat(sql, ",");
strcat(sql, _rule_type_value);
strcat(sql, ")");
strcat(sql, " VALUES (");
strcat(sql, "'");
strcat(sql, encryptSourceId);
strcat(sql, "'");
strcat(sql, ",");

char temp[1000];
//itoa(commandIdInt,temp,10);
//LOGD("Temp value is: %s", temp);
sprintf(temp, "%s%d",sql, commandId);
LOGD("Temp value is: %s", temp);
LOGD("SQL command before copy is: %s", sql);

strcpy(sql, temp);
LOGD("SQL command after copy is: %s", sql);
strcat(sql, ",");
//strcat(sql, commandId);
//strcat(sql, ",");

strcat(sql, "'");
strcat(sql, encryptFingerPrint);
strcat(sql, "'");
strcat(sql, ",");

strcat(sql, "'");
strcat(sql, encryptAppId);
strcat(sql, "'");
strcat(sql, ",");

strcat(sql, "'");
strcat(sql, encryptRuleType);
strcat(sql, "'");
strcat(sql, ")");


LOGD("SQL Insert Query is: %s", sql);
//char *name = "Name";
//char *fullname = "My " name;

//LOGD("%s",fullname);


// Execute SQL Statement
//rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
rc = sqlite3_exec(db, sql, NULL, NULL, &zErrMsg);
if(sqlite3_exec(db, sql, NULL, NULL, &zErrMsg)!= SQLITE_OK) {
//fprint(stderr, "SQL error: %s\n", zErrMsg);
LOGD("SQL Error is: %s\n", zErrMsg);
LOGD("INSERT TABLE FAILED");
//throw_sqlite3_exception(env, db);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "Table Created Successfully\n");
LOGD("Table Created Successfully");
}


Note : I am using RSA algorithm and JNI for all the above operations.


Please help !!!


Aucun commentaire:

Enregistrer un commentaire