insert & fetching RSA encrypted data in SQLite

  1. Varnit Khandelwal

    Varnit Khandelwal New Member

    We are encrypting data ( string ) with RSA algorithm. Then we are trying to insert this encrypted data to SQLite Android database using varchar type. But whenever the encrypted data contains single quote ('), the data is not inserted as we are using varchar.

    Please suggest which data type we have to use to hold the encrypted data in database.

    If insertion of encrypted data is success, then we are not able to decrypt the data that we are fetching from the SQLite database.

    Please suggest some solution.

    char* encryptValue(char* value){
    char msg[KEY_LENGTH /8];
    strncpy(msg, value, sizeof(msg)-1);
    msg[(KEY_LENGTH /8)-1]='\0';
    LOGD("Message is: %s", msg);

    LOGD("gen rsa8");

    LOGD("Message is %s",msg);
    fgets(msg, KEY_LENGTH -1, stdin);
    LOGD("gen rsa8a");

    LOGD("Message is %s",msg);
    LOGD("gen rsa8b");

    LOGD("gen rsa8b1");

    LOGD("value of r size is: %d", RSA_size(r));
    encrypt = malloc(RSA_size(r));

    err = malloc(130);

    if((encrypt_len = RSA_public_encrypt(strlen(msg)+1,(unsigned char*) value,(unsigned char*) encrypt, r, RSA_PKCS1_OAEP_PADDING))==-1){
    LOGD("gen rsa9a");
    ERR_error_string(ERR_get_error(), err);
    LOGD("gen rsa10");//LOGD(stderr,"Error encrypting message: %s\n", err);

    //goto free_stuff;

    // Write the encrypted message to a file//int c =0;
    LOGD("gen rsa10b");FILE*encryptFile = fopen("/sdcard/encrypt.txt","w");
    LOGD("gen rsa10c");
    fwrite(encrypt, sizeof(*encrypt), RSA_size(r), encryptFile);
    LOGD("gen rsa10d");

    //LOGD("gen rsa11");//printf("Generating RSA (%d bits) r...", KEY_LENGTH);
    LOGD("encrypt sizeof: %d", strlen(encrypt));
    LOGD("Encrypt is: %s", encrypt);//LOGD("encrypt sizeof: %d", strlen(encrypt));
    LOGD("Encrypt_len value is %d", encrypt_len);//printf("Size Of Encrypt: %d", strlen(encrypt));

    return encrypt;}

    char *decryptValue(char* value){//Read it back
    // printf("Reading back encrypted message and attempting decryption...\n");

    LOGD("*********Value of r size is: %d", RSA_size(r));
    encrypt = malloc(RSA_size(r));

    LOGD("*********Value of value field is: %s",value);

    //strncpy(encrypt, value, sizeof(encrypt)-1);//encrypt[strlen(encrypt)]='\0';

    LOGD("*********Value of Encrypt is: %s",encrypt);

    LOGD("*********gen rsa12a");

    LOGD("*********gen rsa12b");

    LOGD("*********gen rsa12c");

    LOGD("*********gen rsa12d");

    decrypt = malloc(encrypt_len);
    LOGD("*********Encrypt_len value is %d", encrypt_len);//LOGD("Value of msg length is: "+ strlen(msg)+1);
    LOGD("*********Length of Encrypt is %d", strlen(encrypt));
    LOGD("*********Value of Decrypt is: %s", decrypt);
    LOGD("*********gen rsa12e");if(RSA_private_decrypt(encrypt_len,(unsigned char*) value,(unsigned char*) decrypt, r, RSA_PKCS1_OAEP_PADDING)==-1){
    ERR_error_string(ERR_get_error(), err);

    LOGD("*********gen rsa13");


    LOGD("*********Decrypted message: %s\n", decrypt);
    LOGD("*********Decrypted String length is %d", strlen(decrypt));return decrypt;


  2. EarlyMon

    EarlyMon The PearlyMon Moderator

    Moved to development. :rolleyes:

Share This Page