Home > front end >  I have a text file which contains various lines in a similar format , data is seperated with symbol,
I have a text file which contains various lines in a similar format , data is seperated with symbol,

Time:08-10

i have a data.txt file which contains multiple lines of text in a predefined form which is

"Name" space "-&&-" symbol space "XSvzLr" a hash

and it looks like this in the file-

instagram-&&-aHJK7y9894ds==
facebook-&&-dKBHJ&^(8*==
somesite-&&-djahJHl*(&==

now i want to decrypt the hash in then end, i use php and will use openssl_decrypt() function to do so.

the thing it i want to get hashes seperated and decrypt and echo one line at a time , how can i do so , please help

CodePudding user response:

Hi Ashutosh_7i,

have a look at explode() in the PHP docs

One piece of advice. Please look at - How much research effort is expected of Stack Overflow users.

A quick google search for how to split a string in php points you into the right direction.

That's the solution for the string splitting. As you want it line for line, the construct around would be a loop. Loops


Edit - how to encrpyt and decrypt with openssl_...

$dataset = array(
        "instagram" => "cool",
        "facebook" => "uncool",
        "somesite" => "okay-ish");
    
    $strings = array();
    
    // encrypt sample data
    foreach ($dataset as $key => $value) {
        $encrytedText =     openssl_encrypt($value, 'aes-256-cbc-hmac-sha256', 'myAwesomePassPhrase');
        echo $encrytedText . PHP_EOL;
        $strings[] = $key . '-&&-' . $encrytedText;
    }
    
    
    // decrypt sample data
    foreach ($strings as $string) {
        $crpytedText = explode('-&&-',$string,)[1];
        $decryptedText = openssl_decrypt($crpytedText, 'aes-256-cbc-hmac-sha256', 'myAwesomePassPhrase');
        echo $decryptedText . PHP_EOL; 
    }

For options and further Infos see the documentation of openssl decrypt

CodePudding user response:

the explode() function of php works here by seperating the values and output them in an array, the "-&&-" symbol is used to distinguish between texts and the final php code is-


$data=file_get_contents( "Data.txt" );

$lines = explode(PHP_EOL, $data);
foreach($lines as $line){
    var_dump(explode('-&&-', $line));
}

echo nl2br("\n\n\n\n\n\n");
  •  Tags:  
  • php
  • Related