Tuesday, January 23, 2018

Simple Two way Encryption and Decryption Function PHP



Hello guys, after a long time, I came back to my blog with new flavor of articles, and I am plan to concentrate my blog half my working day here after. So my followers can expect valuable development ideas from me through comment and feed.

Here we go our come back article, Here now I teach you the Two way encryption and decryption method with full explanation.

Objective Build a function for Two Way Encryption and Decryption:

            Encrypt as well as Decrypt our given string with our own Encrypt Decrypt Key and Initiative Vector (IV) (?), with Advanced Encryption Standard (AES) (?) this method is highly secure method all time ever with few PHP functions to build Two way Encryption and Decryption.

Functions Involved in Two Day Encryption and Decryption.

hash - Generate a hash value (message digest)
string hash ( string $algo , string $data [, bool $raw_output = false ] )

substr - Return part of a string
            string substr ( string $string , int $start [, int $length ] )

base64_encode - Encodes data with MIME base64
string base64_encode ( string $data )

base64_decode - Decodes data encoded with MIME base64
            string base64_decode ( string $data [, bool $strict = false ] )

openssl_encrypt - Encrypts data
string openssl_encrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string &$tag = NULL [, string $aad = "" [, int $tag_length = 16 ]]]]] )

openssl_decrypt - Decrypts data
            string openssl_decrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string $tag = "" [, string $aad = "" ]]]] ) 

Two Encryption & Decryption Function

/*
Written by: Jailani M, http://www.W3Lessons.com
Defineds:
ED_KEY - Encryption and Decription key your choice
IV_KEY - INITIATIVE VECTOR your choice
*/
define("ED_KEY","your_key");
define("IV_KEY","your_iv");

function w3_encode( $string) {
    $encrypt_method = "AES-256-CBC";
    $key = hash( 'md5', ED_KEY );
    $iv = substr( hash( 'md5', IV_KEY ), 0, 16 );
    $encoded_string = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
    return $encoded_string;
}

function w3_decode($string)
{
    $encrypt_method = "AES-256-CBC";
    $key = hash( 'md5', ED_KEY );
    $iv = substr( hash( 'md5', IV_KEY ), 0, 16 );
    $decoded_string = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
    return $decoded_string;
}



Download This Script     Live Demo     Download Script

Thursday, April 7, 2016

Determine Image Dimension based on Font size in PHP


Working with images and text, we need to set image size based on font size is very useful for dynamic image processing. like CAPTCHA and text images. if you looking for PHP script to set image size based on font size here is the solution for you.

<?php


if($_GET['fontsize']!="" and is_numeric($_GET['fontsize']))
$size=$_GET['fontsize'];
else
$size=20;


define("F_SIZE", $size);
define("F_ANGLE", 0);
define("F_FONT", "verdana.ttf");


$text = "W3 Lessons";

$Leading=0;
$W=0;
$H=0;
$X=0;
$Y=0;

$_bx = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, $text);
$s = preg_split("/\n]+/", $text);  // Array of lines
$nL = count($s);  // Number of lines

$W = ($W==0)?abs($_bx[2]-$_bx[0]):$W;
$H = ($H==0)?abs($_bx[5]-$_bx[3])+2+($nL>1?($nL*$Leading):0):$H;

$W*=1.05;
$H*=1.5;

$img = @imagecreate($W+8, $H) or die("Cannot Initialize new GD image stream");

$white = imagecolorallocate($img, 255,255,255);
$txtColor = imagecolorallocate($img, 29,56,131);

// Adjust padding right:
$W+=8;
$bgwhite = imagecolorallocatealpha($img,238,238,238,0);
imagefilledrectangle($img, 0, 0,$W,$H, $bgwhite);

$alpha = "".range("a", "z");
$alpha = $alpha.strtoupper($alpha).range(0, 9);

// Use the string to determine the height of a line
$_b = imageTTFBbox(F_SIZE, F_ANGLE, F_FONT, $alpha);
$_H = abs($_b[5]-$_b[3]);
$__H = 0;

// Use the string to determine the width of a line
$_b = imagettfbbox(F_SIZE, F_ANGLE, F_FONT, $text);
$_W = abs($_b[2]-$_b[0]);

// Final width and height
$_X = abs($W/2)-abs($_W/2);
$__H += $_H;

imagettftext($img, F_SIZE, F_ANGLE, $_X, $__H, $txtColor, F_FONT, $text);

header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
// END EMAIL IMAGE  
exit();
?>