0

im having some troubles with a PostGreSQL database and some cyphered data. Im usin a blowfish algorithm to encrypt strings, obtaining binray data in return. This binary can contain \0 and some other scape sequences.

I store the data this way:

`pqxx::binarystring destinatary(encryptedData);`

then i read it like this:

pqxx::binarystring bsmsg(r[0]["message"]);
encryptedData =bsmsg.get();

The problem is, some times, data gets back perfectly but, in other cases, data is returned correctly with some trash afterwards. Any clue on what direction should i modify the write/read to avoid this weird data being added to my encrypted strings?

Another try i did is storing the data this way:

pqxx::binarystring msg(testMsg,testMsgsize);

with the same result.

EDIT: As requested, did some logging research: im studyin this values:

std::string encryptedData;
    bf.Encrypt(&encryptedData, ""); // THIS IS $4
    const void * testMsg = encryptedData.c_str();
    size_t testMsgsize = encryptedData.size();
    pqxx::binarystring msg(testMsg,testMsgsize);
    bf.Encrypt(&encryptedData, "yo"); //$5
    pqxx::binarystring destinatary(encryptedData);
    bf.Encrypt(&encryptedData, "yomaaaaaaaaaaaaaaaaslargo");//$6
    pqxx::binarystring owner(encryptedData);
    bf.Encrypt(&encryptedData, "123A@u");//$7
    pqxx::binarystring key(encryptedData);

and set em among others into the

execute addUnconfirmedInvitation: INSERT INTO friendnotices (ownerserver, type, daterecieved, message, destinatary, owner, internaldata, state, internaldataaux) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)
2014-08-11 17:12:32 CEST DETAIL:  parameters: $1 = 'invite.server()', $2 = 'invitation', $3 = '2014-08-11', $4 = '_(\351\204\376L\036Z', $5 = 'yo\002\002', $6 = 'p\010\224\254m\243\370\177R\222\314\022e/\005*;~\254\2075\205\363\375o\001', $7 = '\375\271\305v\272\036_\377\006\006\006\006', $8 = 'f', $9 = '\372f'' :w\037\312\005\005'

Curious shit is.. i got the right row by searching using $5 and $6 by creating the same variables again

2014-08-11 17:22:35 CEST LOG:  execute searchUnconfirmedInvitation: SELECT * FROM friendnotices WHERE (destinatary=$1) AND (owner = $2) AND (type = $3) AND (ownerserver = $4)
2014-08-11 17:22:35 CEST DETAIL:  parameters: $1 = 'yo\002\002', $2 = 'p\010\224\254m\243\370\177R\222\314\022e/\005*;~\254\2075\205\363\375o\001', $3 = 'invitation', $4 = 'invite.server()'

but if i try to print the strings i showed before:

pqxx::binarystring bsmsg(r[0]["message"]);
        encryptedData =bsmsg.get();
        bf.Decrypt(&clearData, encryptedData);
        pqxx::binarystring bskey(r[0]["internaldata"]);
        encryptedData =bskey.get();
        bf.Decrypt(&clearData, encryptedData);
        pqxx::binarystring des(r[0]["destinatary"]);
        encryptedData =des.get();
        bf.Decrypt(&clearData, encryptedData);
        pqxx::binarystring own(r[0]["owner"]);
        encryptedData =own.get();
        bf.Decrypt(&clearData, encryptedData);

This is what i get (3 attemps):

"���"       //this shouls be ""
"123A@u"    //this is right
"yo"        //this is right   
"yomaaaaaaaaaaaaaaaaslargoy" //i used this as search criteria.. but the last y isnt correct! 
"���"    //second try
"123A@u" 
"yo" 
"yomaaaaaaaaaaaaaaaaslargo1" //wow... now is a 1
"���"  //third try
"123A@u" 
"yo" 
"yomaaaaaaaaaaaaaaaaslargo"  //this time this is correct :?

Everything continue changing with more trys. i can eventually get the correct "" into the first attribute. Attributes shown correctly on the first try, will always be shown correctly.

Xeyos
  • 33
  • 1
  • 6
  • It would be helpful if you could get the underlying SQL executed by the library - enabling `log_statement = 'all'` is generally useful here. (I don't really speak `libpqxx` though, so not sure I can be of much help). – Craig Ringer Aug 11 '14 at 01:59
  • Thnks for the response. Just took some time with configuring the postgresql server and trying things. Logs didnt show something clearly usefull for me. Edited the post with more info about the logs u said and some test cases. – Xeyos Aug 11 '14 at 15:35

2 Answers2

0

Ok i think ive solved it. It seems that pqxx::binarystring::get does not set a \0 at the end of the read and thats causing the random behaivour. It seems to be solved by replacing binaryString::get by binaryString::str

Xeyos
  • 33
  • 1
  • 6
0

You can use method str() instead of get():

encryptedData =bsmsg.get();
-> encryptedData =bsmsg.str();
Doan Quang Viet
  • 135
  • 1
  • 5