PHP: Encryption and Decryption Example
On this page we are demonstrating how we can use our Cryptor class to encrypt, transmit and decrypt a simple message. The steps involved are: encrypt, transmit, decrypt.
Working Encryption Example
Enter a short string here that you want to encrypt and transmit:
This form submits your string using Ajax to a server-side script for encryption:
<?PHP use \Chirp\Cryptor; $encryption_key = '...'; $cipher_method = '...'; $cryptor = new Cryptor($encryption_key, $cipher_method); $crypted = $cryptor->encrypt($token); ?>
Your string has now been encrypted ready for secure transmission in the form below. Note that we've also wiped the original input so now we only have the encrypted value.
You can now either submit the form to send the encrypted value as a POST variable, or click the link under the form to send using GET:
The Cryptor class uses hex-encoding by default, but escaping of the values is still necessary in case of '+', '=' or '&' characters in the output. We've used htmlspecialchars() for the form value, and urlencode() for the link variable.
The strings encrypted on this page can only be decrypted here and not on another server because you will be missing the encryption key, which is specific to this page, and the cipher method used.
Similarly if you encrypt a string on your server using your own cipher method and encryption key it will not decode here.
In order to decrypt data using openssl_decrypt you need to know both those values.
Related Articles - Encryption
- Basic two-way encryption [PHP]
- Better Password Encryption using Blowfish [PHP]
- Encryption and Decryption Example [PHP]
Send a message to The Art of Web:
press <Esc> or click outside this box to close