Sometime we need to convert PHP strings to secret code which cannot be decoded easily by user. This method will help us to pass coded strings with URL GET arguments and also inside Hidden Form fields ..
Eg : http://myserver.com/index.php?username=j345kjh656bthbf6
PHP has inbuilt base64 encocde and base64 decode functions . but anyone can easily decrypt the string by passing it to base64 decoded functions .. So we need our own encode and decode function so that user cant identify the original content .
Simple encryption and decryption function is given below
Assign $key with your own secretpasskey
The string encrypted using function encrypt can be decoded only using decrypt function .
For extra security we have included base64 encryption also.
function encrypt($string, $key='mysecretcode123')
{
$result = '';
for($i=0; $i<strlen($string); $i++)
{
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)+ord($keychar));
$result.=$char;
}
return base64_encode($result);
}
function decrypt($string, $key=' mysecretcode123 ')
{
$result = '';
$string = base64_decode($string);
for($i=0; $i<strlen($string); $i++)
{
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}
Blogger templates
Blogger news
Blogroll
Archives
-
▼
2013
(219)
-
▼
August
(13)
- Listing subcategories or chilld categories using php
- PHP Encryption and Decryption Function
- Web Services
- PHP_Wincache - Wincache for PHP 5.4
- Contro strucrutes in Php: continue
- Woltlab Burning Board 2.3.3 Color Palette Fix for ...
- where in php with mysql
- Feach a URL and print as HTML using SafFeachURL
- Why we learn PHP
- Print full formated time using SafGetFullDateTime04
- Mudahnya Membuat Barcode dengan PHP
- PHP Forms
- for getting the text in a span and write it to the...
-
▼
August
(13)