PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Jon Lawrence   LW_crypt   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example Usage of the class
Class: LW_crypt
Encrypt and decrypt data with AES and RSA keys
Author: By
Last change:
Date: 11 years ago
Size: 1,323 bytes
 

Contents

Class file image Download
<?php
require_once "crypt.class.php";

//Create an instance of the encryption class and provide the salt to use
$crypt = new LW_crypt("1234hfa3#%^");

//generate a random password to use:
$password = $crypt->generatePassword();

//Open the test image we will use to test the encryption
$pic = file_get_contents('test.jpg');
//Encrypt the picture with AES
$encPic = $crypt->encryptAES($pic,$password);
//Decrypt the picture with AES
$decPic = $crypt->decryptAES($encPic,$password);
//display the picture embeded into the page to make sure it still looks right
echo "<img src=\"data:image/jpg;base64,". base64_encode($decPic) ."\" />";

//Now the fun part of using a "public-key" scheme to do this
$keys = $crypt->makeKeyPair();
$privateKey = $keys['privateKey'];
$publicKey = $keys['publicKey'];

//Encrypt the picture using the public key (via secureSend
//which will use AES for the picture, and the public key
//for a random one-time password that is automatically generated)
$encPic = $crypt->secureSend($pic, $publicKey);
//Decrypt the picture with the private key via secureReceive
$decPic = $crypt->secureReceive($encPic, $privateKey);

//Now, display the picture again to make sure it still worked properly
echo "<img src=\"data:image/jpg;base64,". base64_encode($decPic) ."\" />";
?>