2

I have written a piece of code which, for testing purposes, only encrypts and then decrypts some text.

I used phpseclib and it works well. When I store encrypted text in a mysql database, and then retrieve it from there and try to decrypt it, I get a decryption error. In order to store encrypted text in the database I have to use addslashes() php function, and then stripslashes() later before decrypting. When I compare the string I stored in the database, it's exactly the same after stripping slashes, but somehow decryption doesn't work.

Is this a common problem? Is there a special way to create a database that will hold all the characters that RSA produces?

FelipeAls
  • 21,711
  • 8
  • 54
  • 74
Marko Lekić
  • 123
  • 1
  • 7
  • Sounds like you may be storing the data incorrectly in the database. What type of column are you storing the output from RSA in? – majic bunnie Nov 07 '11 at 03:40
  • 1
    Use a binary column type, not a character one. – Paŭlo Ebermann Nov 07 '11 at 03:41
  • I was storing it in varchar(100) column. I tried with a binary column, with the same result. As I said, when I print the data from the database, it's exactly the same as it was before adding it. However, decryption still won't work – Marko Lekić Nov 07 '11 at 04:06
  • I take back what I said about the binary column. I can't seem to store the string correctly as a binary. I found this code for converting to binary: $bin_str = unpack("B*", pack("u*", $cyphertext)); and this for converting from binary: $recypher = unpack("u*",pack("B*", $bin_str); But I get php errors like "Type u: unknown format code" – Marko Lekić Nov 07 '11 at 04:20
  • You needn't convert anything, the output of the encryption function should be binary data, store it as a binary data and retrieve it back in binary form when you need to decrypt. – majic bunnie Nov 07 '11 at 04:37
  • 2
    The problem was actually with the public key and it's solved now. Storing the string in the database works well after addslashes() function. Thank you all for your help – Marko Lekić Nov 07 '11 at 14:49

0 Answers0