From: "Peter Gibbons-BU" <[EMAIL PROTECTED]>

> Hi,
> i'd like to create a password field like in the user table in the mysql
> database.  The data would need to be encrypted.
>
> Can mysql do the whole job.  I would need it ideally to automatically
> decrypt it for me when I do a select..
>
> any suggestions - code example would be really helpful..


MySQL has some built-in encryption function -- they're detailed here:
http://www.mysql.com/doc/M/i/Miscellaneous_functions.html

Among them are:
 - PASSWORD() (non-reversable, usd in the mysql.user table)
 - ENCRYPT() (non-reversable, relies on the Unix crypt() command)
 - ENCODE() and DECODE() (take a password)

If, as you said, you want it automatically decrypted upon selecting, I'd go
with ENCODE() and DECODE(). The following works for what you want, provided
you keep track of the password used to ENCODE each userpass:

CREATE TABLE foo (
    username VARCHAR(20) NOT NULL PRIMARY KEY,
    pass VARCHAR(30) NOT NULL,
    added DATE
);
INSERT INTO foo SET username='testuser',
pass=ENCODE('userpass','encodepassword'), added=NOW();
SELECT * FROM foo;
SELECT DECODE(pass,'encodepassword') FROM foo WHERE username='testuser';
DROP TABLE foo;

Hope this suits your purposes...


--
denonymous
www.coldcircuit.net
denonymous.ne.mediaone.net



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to