1

I am trying to encrypt some data in cakephp 3.5 with Security::encrypt.

I did a test:

$key = '111111111111111111111111111111111';
    $enc = Security::encrypt('123', $key);
    $dec = Security::decrypt($enc, $key);

    Log::write(LOG_DEBUG, "Encrypt $key: $enc");
    Log::write(LOG_DEBUG, "Decrypt $key: $dec");

In my log I get:

Encrypt 111111111111111111111111111111111: c4f3fd8f937afe66a6f211a4333c97be06700655c8f591f9b5bf3c85489a3c0a~ΊCdΔA€Νίύι_Yτ΅\i©EpcΛ*Kβ 
Decrypt 111111111111111111111111111111111: 123

encrypted value is not an ANSI string? Decrypt is done correctlly but if I save this data to database and then decrypt it it fails to give me the correct original data. The problem is that not the correct encrypted key is saved to the database. My column (in mysql) in utf8_unicode_ci. What am I doing wrong?

Thanks

Harris
  • 1,128
  • 4
  • 17
  • 41

1 Answers1

1

If you want to store the data as is, then you need to use a binary type column supported by CakePHP, for MySQL what would be BINARY and BLOB's (when reading such data, CakePHP will give you a data: stream wrapper resource handle).

If you would have to store it in a non-binary column, then you'd need to convert the data accordingly, for example using Base64.

You may also want to look at custom database types that can do the encryption/decryption/conversion transparently.

See also

ndm
  • 59,784
  • 9
  • 71
  • 110