encrypt/decrypt a field - php/mysql

Posted on 2006-06-06
Medium Priority
Last Modified: 2008-01-09
I'm testing out a DB for a friend. He's got a field with a field being encrypted using the following:


When I try to decode the string from the DB, the following doesn’t work....


I've been searching online for a bit, and the closest thing that I've found is: DECODE(crypt_str,pass_str)   as outlined on http://dev.mysql.com/doc/refman/5.0/en/encryption-functions.html

But I'm not sure where to get the pass_str from.
Question by:webmanager
  • 3
  • 2

Author Comment

ID: 16841974
a few minor updates....

I'm now (for testing purposes) using:


When I try to decode the string from the DB, the following doesn’t work....


The error I get on the webpage that has the decode script is:
Fatal error: Call to undefined function: decode() in test.php on line 24
LVL 33

Expert Comment

ID: 16842702
I think the encode and decode are MySQL functions, not PHP functions.  So you'd want something like this:
$sql = "SELECT DECODE('{$row['numberPass']}', 'someValue') AS password";
$result = mysql_query($sql) or die(mysql_error());

pass_str will be built in to whatever code encodes and stores passwords.

Author Comment

ID: 16843559
I guess I should have said this before... lol

I have an index.php page with a single field form called numberPass.
The form on index.php submits to submit.php

On submit.php, this is what I have...

$query = "INSERT INTO tablename ( numberPass ) VALUES ( ENCODE('$_POST[numberPass]','123') )";
<h3>Thank you for your entry.</h3>

on the page that I want to view the encrypted info (it's called view.php), this is what I have.

<title>View info</title>
$query = "SELECT numberPass FROM tablename";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
<li><?php print $row[numberPass]; ?></li>
<li><?php DECODE("$row[numberPass]","123"); ?></li>

LVL 33

Accepted Solution

snoyes_jw earned 200 total points
ID: 16843638
As I said, the DECODE function is a MySQL function, not a PHP function.  So that view.php should contain a query like
$query = "SELECT numberPass, DECODE(numberPass, '123') AS decoded FROM tablename";

and further on down,
<?php print $row['numberPass']; ?>
<?php print $row['decoded']; ?>

Author Comment

ID: 16843717
Works like a charm...  


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we’ll look at how to deploy ProxySQL.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question