encryp form feild after submission

Hi
I wrote code in php to design encrypt and decrypt function
i test this code in php and it work fine

my question is how  i call the encrypt function to encrypt one field when user submit form

i try to use $_post and java script code but it still not working. i actually get empty field

is there is a possible way to achieve that.
where i can call encrypt function. in html code. can i use php code with html form

thanks
ang3lusAsked:
Who is Participating?
 
Greg AlexanderLead DeveloperCommented:
Sure, make your action page a php page

<form action="some_php_page.php" method="post">
Test Field <input type="text" name="test_field"><br />
<input type="submit" name="submit" value="Submit">
</form>

Then on your php page you can access the form variables (see attached code)
<?php
if(isset($_POST['submit'])){

$text = $_POST['test_field'];

encrypt_function($text);
//do more here
}
?>

Open in new window

0
 
Greg AlexanderLead DeveloperCommented:
This of course could be on one page put it would have to be a php page
0
 
Ray PaseurCommented:
In PHP variables are case-sensitive, so $_POST is not the same thing as $_post.  I can show you how to encrypt and decrypt information.  See the code snippet for an example of the code.  You can test it here:
http://www.laprbass.com/RAY_encrypt_decrypt.php

But the bigger question is probably, "Why do you want to do this?"  There are several well-known design patterns that are useful for securing data, but they must be applied in the appropriate context.  If you tell us a little more about what you are trying to protect we may be able to give you more on-target advice.  It might be as simple as using HTTPS.

Best regards, ~Ray
<?php // RAY_encrypt_decrypt.php
error_reporting(E_ALL);

// MAN PAGE: http://us.php.net/manual/en/ref.mcrypt.php

class Encryption
{
    protected $eot;
    protected $key;
    protected $ivs;
    protected $iv;

    public function __construct()
    {
        // SET KEY, DELIMITER, INITIALIZATION VECTOR - MUST BE KNOWN TO BOTH PARTS OF THE ALGORITHM
        $this->key = 'quay';
        $this->eot = '___EOT';
        $this->ivs = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $this->iv  = mcrypt_create_iv($this->ivs);
    }

    public function encrypt($text)
    {
        // APPEND END OF TEXT DELIMITER
        $text .= $this->eot;

        // ENCRYPT THE DATA
        $data = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB, $this->iv);

        // MAKE IT base64() STRING SAFE FOR STORAGE AND TRANSMISSION
        return base64_encode($data);
    }

    public function decrypt($text)
    {
        // DECODE THE DATA INTO THE BINARY ENCRYPTED STRING
        $text = base64_decode($text);

        // DECRYPT THE STRING
        $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB, $this->iv);

        // REMOVE END OF TEXT DELIMITER
        $data = explode($this->eot, $data);
        return $data[0];
    }
}

// INSTANTIATE THE CLASS
$c = new Encryption();

// INITIALIZE VARS FOR LATER USE IN THE HTML FORM
$encoded = '';
$decoded = '';

// IF ANYTHING WAS POSTED
if (!empty($_POST["clearstring"]))
{
    $encoded = $c->encrypt($_POST["clearstring"]);
    echo "<br/>{$_POST["clearstring"]} YIELDS ";
    var_dump($encoded);
}

if (!empty($_POST["cryptstring"]))
{
    $decoded = $c->decrypt($_POST["cryptstring"]);
    echo "<br/>{$_POST["cryptstring"]} YIELDS ";
    var_dump($decoded);
}

// END OF PHP - PUT UP THE FORM
?>
<form method="post">
<input name="clearstring" value="<?php echo $decoded; ?>" />
<input type="submit" value="ENCRYPT" />
<br/>
<input name="cryptstring" value="<?php echo $encoded; ?>" />
<input type="submit" value="DECRYPT" />
</form>

Open in new window

0
 
ang3lusAuthor Commented:
thanks a lot.
you save my time
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.