Login   Register  
PHP Classes
elePHPant
Icontem

File: openssl_test.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Ricky Robinson  >  TA_OpenSSL  >  openssl_test.php  >  Download  
File: openssl_test.php
Role: Example script
Content type: text/plain
Description: Test PHP CGI Script
Class: TA_OpenSSL
Encrypts and decrypts data using certificates
Author: By
Last change: Edited to use css
Date: 2003-09-15 00:50
Size: 4,157 bytes
 

Contents

Class file image Download
<?php
include("openssl_test_header.html");
/**
*  Steps to Using Class:
*    1. Include the class file in your source
*    2. Create an instance of the class
*    3. Set the public key path
*    4. Set the private key path
*    5. Set the passphrase ( set to "" if passphrase not used)
*    6. To Encrypt:
*            a. Call encrypt_data_public() to encrypt
*            b. Call get_encrypted_data() to retrieve data
*
*    7. To Decrypt:
*             a. Call decrypt_data_private
*             b. Call get_decrypted_data() to retrieve data
*/

//1. Include the class file in your source
require_once("ta_encrypt.inc");

//2. Create an instance of the class
$cls_encrypt = new TA_OpenSSL;

//set or get? required parameters
//this way it works in development environment
//and through test posting page
if (!isset($_POST["public_key"]))
{
  
$path_to_cert "F:\\ApacheGroup\\Apache\\htdocs\\cvs_working\\rterra\\online_signup\\includes\\cacert.pem";
}else{
  
$path_to_cert $_POST["public_key"];
}

if (!isset(
$_POST["private_key"]))
{
  
$path_to_key "F:\\ApacheGroup\\Apache\\htdocs\\cvs_working\\rterra\\online_signup\\includes\\privkey.pem";
}else{
  
$path_to_key $_POST["private_key"];
}
if (!isset(
$_POST["passphrase"]))
{
  
$pass_phrase "testing";
}else{
  
$pass_phrase $_POST["passphrase"];
}

if (!isset(
$_POST["encrypt_string"]))
{
  
$string_to_encrypt "411111111111-09/2004";
}else{
  
$string_to_encrypt $_POST["encrypt_string"];
}


// 3. set the public key path
$cls_encrypt->set_public_key($path_to_cert);

// 4. set the private key path
$cls_encrypt->set_private_key($path_to_key);

// 5. set the passphrase if applicable
$cls_encrypt->set_passphrase($pass_phrase);


echo (
"<div class=\"data-header\">Data to Encrypt:</div>\n");
echo (
"<div class=\"data\">".$string_to_encrypt."</div>\n");


echo (
"<div class=\"processing\">Encrypting Data....</div>\n\n");

// 6a. Call encrypt_data_public() to encrypt
$ret $cls_encrypt->encrypt_data_public($string_to_encrypt);
if ( 
$ret != TA_SUCCESS )
{
  
print_error($cls_encrypt->get_error_number(), $cls_encrypt->get_error_string(),$cls_encrypt);
  die();
}

//6b. Call get_encrypted_data() to retrieve data
$encrypted_data $cls_encrypt->get_encrypted_data();

//output encryption strength just for fun:)
$encryption_strength strlen($encrypted_data) * 8;

echo (
"<div class=\"data-header\">Encryption Strength:</div>\n");
echo (
"<div class=\"data\">".$encryption_strength." bit Encryption</div>\n");



echo (
"<div class=\"data-header\">Encrypted Data:</div>\n");
echo (
"<div class=\"data\">".$encrypted_data."</div>\n");



echo (
"<div class=\"data-header\">URL Encoded Encrypted Data:</div>\n");
echo (
"<div class=\"data\">"urlencode($encrypted_data)."</div>\n");


echo (
"<div class=\"data-header\">Hex Encoded Encrypted Data:</div>\n");
echo (
"<div class=\"data\">"bin2hex($encrypted_data)."</div>\n");


echo (
"<div class=\"processing\">Decrypting Data...</div>\n\n");


//7a. Call decrypt_data_private
$ret $cls_encrypt->decrypt_data_private($encrypted_data);
if ( 
$ret != TA_SUCCESS )
{
  
print_error($cls_encrypt->get_error_number(), $cls_encrypt->get_error_string(),$cls_encrypt);
  die();
}

//7b. Call get_decrypted_data() to retrieve data
$decrypted_data $cls_encrypt->get_decrypted_data();

echo (
"<div class=\"data-header\">Decrypted Data:</div>\n");
echo (
"<div class=\"data\">".$decrypted_data."</div>\n");

echo (
"<div class=\"email\"><a href=\"mailto:".hex_encode("dev@terraaccess.com")."\">TA Developement Center Email</a></div>\n\n");

echo (
"</body></html>");
function 
print_error($errno$error$cls)
{
 echo( 
"<div class=\"error\"><p>Class Error(s):</p>".$errno." ** ".$error."</div>");
 echo( 
"<div class=\"error\"><p>OpenSSL Error(s):</p>");
 
$cls->kick_openssl_errors();
 echo( 
"</div>\n\n");
}

function 
hex_encode ($email_address)    {
       
$encoded bin2hex("$email_address");
       
$encoded chunk_split($encoded2'%');
       
$encoded '%' substr($encoded0strlen($encoded) - 1);
       return 
$encoded;
}

?>