How to encrypt multiple variables inside a file with php?

3

I'm using php to read a file for a $string , it has some variable words that I need to find and replace with others, ie I need to get these variables, encrypt them with AES and write back where they were, making a replace .

The variables do not have the same name , and they see after the parameter cmd= , the amount of them can vary.

Sample file with variables:

Inicio
texto=nome?cmd=0a7-5.ext
data...
texto=nome?cmd=12345.ext
data...
texto=nome?cmd=abcd.ext

The value to be replaced is the encrypted variable itself with the following AES:

//fnEncrypt("0a7-5", "1234567890123456");
function fnEncrypt($sValue, $sSecretKey)
{
    return rtrim(
        base64_encode(
            mcrypt_encrypt(
                MCRYPT_RIJNDAEL_256,
                $sSecretKey, $sValue, 
                MCRYPT_MODE_ECB, 
                mcrypt_create_iv(
                    mcrypt_get_iv_size(
                        MCRYPT_RIJNDAEL_256, 
                        MCRYPT_MODE_ECB
                    ), 
                    MCRYPT_RAND)
                )
            ), "
Inicio
texto=nome?cmd=dxZIKbLDEtI/m81cEGnB+dVbmy1C+fcIxmeBGHpyYY0=
data...
texto=nome?cmd=+FFzyIqAENF13PnEj5XqpHYlMD2u08uHmWK7HYDyPos=
data...
texto=nome?cmd=cTySfjmfN85GgcWS3aVHSwExC2ZBldBqh9xJq9ndAmc=
" ); }

What is expected:

Inicio
texto=nome?cmd=0a7-5.ext
data...
texto=nome?cmd=12345.ext
data...
texto=nome?cmd=abcd.ext

How can I replace these variables with their AES-encrypted version, knowing that the number of them and their values are different?

    
asked by anonymous 19.10.2016 / 06:16

1 answer

3

Assuming you always start with "?cmd=" and ending at the end of the line, you can use

  

preg_replace_callback ($ regex, $ callback, $ string)

     

The callback that will be called and passed an array of the combined elements in the string


Regular expression:

/(\?cmd=)(.+)/

Code:

function fnEncrypt($sValue, $sSecretKey)
{
    return rtrim(
        base64_encode(
            mcrypt_encrypt(
                MCRYPT_RIJNDAEL_256,
                $sSecretKey, $sValue, 
                MCRYPT_MODE_ECB, 
                mcrypt_create_iv(
                    mcrypt_get_iv_size(
                        MCRYPT_RIJNDAEL_256, 
                        MCRYPT_MODE_ECB
                    ), 
                    MCRYPT_RAND)
                )
            ), "
Inicio
texto=nome?cmd=dxZIKbLDEtI/m81cEGnB+dVbmy1C+fcIxmeBGHpyYY0=
data...
texto=nome?cmd=+FFzyIqAENF13PnEj5XqpHYlMD2u08uHmWK7HYDyPos=
data...
texto=nome?cmd=cTySfjmfN85GgcWS3aVHSwExC2ZBldBqh9xJq9ndAmc=
" ); } $string = " Inicio texto=nome?cmd=0a7-5.ext data... texto=nome?cmd=12345.ext data... texto=nome?cmd=abcd.ext "; function fnEncrypt_match($matches) { return $matches[1] . fnEncrypt($matches[2], "1234567890123456"); } $stringAES = preg_replace_callback('/(\?cmd=)(.+)/', 'fnEncrypt_match', $string); echo "<pre> $stringAES </pre>";

Result:

/(\?cmd=)(.+)/


  

Warning: The code used to encrypt is not completely secure.   We recommend using libsodium .

     

More information: PHP AES encrypt / decrypt

    
19.10.2016 / 10:18