|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
jothirajan Smarty Pro
Joined: 06 Feb 2009 Posts: 114 Location: India
|
Posted: Wed Nov 04, 2009 10:36 am Post subject: URL Encryption and Decrytion using Smarty plugin |
|
|
URL ID Encryption and Decryption in Smarty:
>> I have just tried out a sample URL ID encryption/decypt in Smarty. Just take a look and let me know your feedback
Yeap !!
- Encryption code in Smarty
- Decyption in PHP
MY TPL:”mytemplate.tpl”
In the TPL I just tried out the encyption code using the PLUGIN file. Here the code is
Code: | <a href="[b]myurl.php[/b]?hidden_Id={$Exe_users[mem].user_id|enc_dec}">Click</a> |
enc_dec >> is the plugin modifier file modifier.enc_dec.php
modifier.enc_dec.php
Code: | <<?php
/**
Plugin by jothirajan
*/
function smarty_modifier_enc_dec($data_input) {
$myFile ="[b]write.php[/b]";
$fh = fopen($myFile, 'r');
$theData = fread($fh,filesize($myFile));
fclose($fh);
$key = $theData;
$plain_text = trim($data_input);
$iv = substr(md5($key), 0,mcrypt_get_iv_size(MCRYPT_CAST_256,MCRYPT_MODE_CFB));
$c_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $plain_text, MCRYPT_ENCRYPT, $iv);
return trim(chop(base64_encode($c_t)));
}
?> |
See the line
can be like this
Code: | $key = “HUJGJDG564654543”; | but the URL will be like this
Code: | <a href="myurl.php?hidden_Id={$Exe_users[mem].user_id|enc_dec}">11</a> |
Code: | http://localhost/admin/myurl.php?hidden_Id=SA== | , that is for 11 we will get “SA==” as the encrypted value for all the times. But I need to change the values randomly just like below
Code: | http://localhost/admin/myurl.php?hidden_Id=SQ==
http://localhost/admin/myurl.php?hidden_Id=kg==
http://localhost/admin/myurl.php?hidden_Id=hQ== |
This is due to the static $key value, so I need to make a dynamic key value for that I have created a write.php file
write.php
Code: | 3K?Cad2HB#eA?7i95orjzIZSf |
But you need to generate this random alphanumeric values first. This is my main file
main.php
Code: | <?php
$p_length=25;
$char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$char_list .= "abcdefghijklmnopqrstuvwxyz";
$char_list .= "1234567890";
$char_list .= "!@#$?-_";
$random = "";
srand((double) microtime() * 1000000);
for($i = 0; $i < $p_length; $i++){$random .= substr($char_list, (rand() % (strlen($char_list))), 1);}
$random;
$file="write.php";
$fp=fopen($file, "w");
$StringData = "".$random."";
fwrite($fp, $StringData);
fclose($fp);
$objSmarty->display("[b]mytemplate.tpl[/b]");?> |
So each time the php file will create different alphanumeric values.
Finally I got the Encypted ID value, so the next step is to decrypt the same in my other php file so here myurl.php
myurl.php
Code: | <?php
$c_t="request the URL value here";
$myFile ="write.php";
$fh = fopen($myFile, 'r');
$theData = fread($fh,filesize($myFile));
fclose($fh);
$key = $theData;
$c_t = trim(chop(base64_decode($c_t)));
$iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
$p_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $c_t, MCRYPT_DECRYPT, $iv);
$decrypted_value=trim(chop($p_t));
?> |
$decrypted_value will provid my prompt output
Code: | [b]write.php(random code)->main.php(main.tpl | Smarty encrption code -> myurl.php (decypt the same).[/b] |
And I have restricted the users to change the URL ID’s in my phase II, will declare that one too here. Any feedback !!!!..
- Happy Coding – |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|