Search code examples
phpencryptionmcryptphp-openssl

2 way crypt image filename


I would like to change an image filename with a crypted string.

I need also:

  • Crypting more multiple time the original string with the same password, will return the same string
  • The encrypted string should be decrypted (2 way crypt)

What php method could I use? I saw that mcrypt is not updated from lots of years, and openssl_crypt will return different encrypted strings each time I encrypt (even if the password is the same).

Thanks


Solution

  • openssl_encrypt & co should be fine. example:

    <?php
    $data='foo.jpg';
    $method='AES-192-CBC';
    $password='ogiughjklpdeorivjrhfnd';
    $iv=base64_encode(random_bytes(10));
    $encrypted=array();
    for($i=0;$i<10;++$i){
        $encrypted[]=openssl_encrypt($data,$method,$password,0,$iv );
    }
    $decrypted=openssl_decrypt ($encrypted[0] ,$method ,$password,0,$iv);
    
    var_dump($data,$encrypted[0],$decrypted,$data===$decrypted,$encrypted[2]===$encrypted[7],$encrypted);
    

    quote openssl_crypt will return different encrypted strings each time I encrypt (even if the password is the same). - not with AES-192-CBC, as long as the password AND IV is identical. i think that's why IV's was invented in the first place, to prevent the same data to encrypt to the exact same encrypted form