encryption openssl in php

I want to encrypt and decrypt strings and files in PHP and Ive found a few examples (including some on experts-exchange and some answers to my own questions) however reading up cryptography Ive found several suggesting to use openssl instead of older cryptography solutions. Problem is Ive found very limited resources and examples of using openssl in php for encrypting strings and files.

Anyone got any resources or examples of encrypting and decryption using openssl in php?

Thank you in advance
tonelm54Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ahmed MerghaniSoftware EngineerCommented:
Hi,

There are 2 examples of encryption/decryption in this PHP manual link for openssl:
http://php.net/manual/en/book.openssl.php

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tonelm54Author Commented:
Just for anyone else digging around for this I found this a good example.

To generate a Personally signed SSL certificate I used https://www.trustico.co.uk/ssltools/create/certificate-pem/create-self-signed-ssl-certificate.php

And then used:-
<?php
function EncryptData($source)
{
    $fp=fopen("c:/Rob/cert.txt","r");
    $pub_key=fread($fp,8192);
    fclose($fp);
    openssl_get_publickey($pub_key);
    /*
    * NOTE:  Here you use the $pub_key value (converted, I guess)
    */
    openssl_public_encrypt($source,$crypttext,$pub_key);
    return(base64_encode($crypttext));
}

function DecryptData($source)
{
    #print("number : $number");
    $fp=fopen("c:/Rob/key.txt","r");
    $priv_key=fread($fp,8192);
    fclose($fp);
    // $passphrase is required if your key is encoded (suggested)
    //$res = openssl_get_privatekey($priv_key,$passphrase);
    $res = openssl_get_privatekey($priv_key);
    /*
    * NOTE:  Here you use the returned resource value
    */
    $decoded_source = base64_decode($source);
    openssl_private_decrypt($decoded_source,$newsource,$res);
    return($newsource);
}

$originalText = 'This is a test';
$encryptedText = EncryptData($originalText);
$dectyptedText = DecryptData($encryptedText);

echo $originalText . "<hr/>" . $encryptedText . "<hr/>" . $dectyptedText;

?>

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.