php - openssl_decrypt () function not working, returning null -


i used openssl_encrypt , openssl_decrypt function decrypt part not returning value, whereas using same key encrypt working fine. here function used. variable $decrypted return null . every small appreciated

function decryption($value) {      $methods = openssl_get_cipher_methods();     $clefsecrete = "flight";     echo '<pre>';     foreach ($methods $method) {         //$encrypted = openssl_encrypt($texteacrypter, $method, $clefsecrete); ----this used encryption         $decrypted = openssl_decrypt($value, $method, $clefsecrete);         echo "value=".$decrypted;         echo $method . ' : '. $decrypted . "\n";         break;     }     echo '</pre>';     return $decrypted; } 

i had same problem, googled question , ended here, on same question had asked. had search elsewhere.

i found this article useful in explaining shortcoming of official php documentation. article similar content here.

in end boils down key/password. openssl_encrypt library expects key not password. , size of key must size of cipher’s intrinsic key size. first article says if provide longer key, excess discarded , key shorter expected padded zero, i.e. \x00 bytes. have not tested fact.

i have edited code read below.

the idea have used size of initial vector cipher expects size of key expects. here, passing key not password doing. find way turning password key.

in code, did not pass options , iv (initialization vector).

the iv string cipher 'mixes' plaintext before encryption. cipher encrypts 'mixture'. important? yes! without 'mixing', pair of identical plaintexts result pair of identical ciphertexts, can lead attack; if 2 identical plaintext-ciphertext pairs not same user, these 2 users using same key! unique iv each plaintext therefore ensures no 2 plaintexts result identical ciphertexts. in other words, iv salt.

    $plaintext = 'testing openssl functions';     $methods = openssl_get_cipher_methods();     //$clefsecrete = 'flight';     echo '<pre>';            foreach ($methods $method) {         $ivlen = openssl_cipher_iv_length($method);         $clefsecrete = openssl_random_pseudo_bytes($ivlen);         $iv = openssl_random_pseudo_bytes($ivlen);          $encrypted = openssl_encrypt($plaintext, $method, $clefsecrete, openssl_raw_data, $iv);         $decrypted = openssl_decrypt($encrypted, $method, $clefsecrete, openssl_raw_data, $iv);         echo 'plaintext='.$plaintext. "\n";         echo 'cipher='.$method. "\n";         echo 'encrypted to: '.$encrypted. "\n";         echo 'decrypted to: '.$decrypted. "\n\n";     }     echo '</pre>'; 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -