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?

6
  • Sounds like you may be storing the data incorrectly in the database. What type of column are you storing the output from RSA in? Nov 7, 2011 at 3:40
  • 1
    Use a binary column type, not a character one. Nov 7, 2011 at 3: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 Nov 7, 2011 at 4: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" Nov 7, 2011 at 4:20
  • 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 Nov 7, 2011 at 14:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.