php - mcrypt not decrypting to same length -


I am trying to mcrypt some data using a class (methods below) such that you If you copy the data using the pack, you can use the unpack to retrieve the data.

  $ packed = $ server- & gt; Cash-> Pack ("pack", array (123,123,123), "password"); If (! $ Packed) {resonance "data could not be encrypted \ n"; } $ Server- & gt; Cash-> Unpack ("packed", "password");   

When I pack it, do i var_dump on json_encode () data and get it:
< Code> string (13) "[123,123,123]"

When I open it, then I enter var_dump on mcrypt_decode () string I get it, and get it:
string (32) "[123,123,123]"

Why length is different? When I do the mcrypt_decode () string on json_decode () , I get back to null , and this is the reason I trim data I work, so it works, but I should not trim it.

Here are the ways:

  & lt ;? Put Php public function ($ key, $ value, $ life = 0) {$ this-> Cache [$ of] = $ value; $ Life = (en) $ life; If ($ life> 0) {$ life = strtotime ("now + $ life seconds"); } $ This- & gt; Life [$ key] = $ life; } Get public function ($ key) {return $ this- & gt; Cash [$ Key]; } Public function pack ($ key, $ value, $ secret, $ life = 0) {if (function_exists ("mcrypt_encrypt")) {$ this- & gt; Put ($ key, $ value, $ life); return false; } $ Iv_size = mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $ Iv = mcrypt_create_iv ($ iv_size, MCRYPT_RAND); $ Value = json_encode ($ value); Var_dump ($ value); $ Cryptdata = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $ secret, $ value, MCRYPT_MODE_ECB, $ iv); $ This- & gt; Put ($ key, $ cryptdata, $ life); Back true; } Public function unpack ($ key, $ secret) {if (! Function_exists ("mcrypt_decrypt")) {return json_decode ($ this- & gt; get ($ key) is true); } $ Cryptdata = $ this- & gt; Get ($ key); $ Iv_size = mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $ Iv = mcrypt_create_iv ($ iv_size, MCRYPT_RAND); $ Data = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $ secret, $ cryptdata, MCRYPT_MODE_ECB, $ iv); // $ data = json_decode ($ data, true); Var_dump ($ data); }    

When using block cipher mode like ECB (you should not use it A BTW), encrypted NUL-pad data, so that its length is useful in block size of the encryption algorithm.

If you need to know, the size of block for resident-256 is 256 bits or 32 bytes.

Keeping in mind that you are encrypting JSON data, you just do not rtrim () data and do not worry about it unless there is any way around it You do not switch over to counter mode like CTR.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -