# Database Discussions > MySQL >  Aes_decrypt

## Wnt2bsleepin

I managed to encrypt a string (Password) with AES_ENCRYPT but I am having difficulty decrypting it. I am using PHP and running MySQL. Any help is appreciated.

----------


## ua549

What does your code look like?

----------


## Wnt2bsleepin

Here is the code I am using to encrypt it



```
if(!define('SALT'))
define('SALT','n7g7b8necefAs5ecrEv3M72ra');

$pwd=$_POST['pass'];
 
$ins="INSERT INTO accounts (FirstName, LastName, School, Email, PASS)
VALUES ('$_POST[fname]', '$_POST[lname]', '$_POST[school]', '$_POST[email]', AES_ENCRYPT('$pwd','.salt'))";

mysql_query($ins, $con);
```

and the code, which is on a separate web page, to decrypt it.



```
if(!define('SALT'))
define('SALT','n7g7b8necefAs5ecrEv3M72ra');

$result = mysql_query("SELECT * FROM accounts");
while($row = mysql_fetch_array($result))
	{
       $pass = $row['PASS'];
	$une = AES_DECRYPT('$pass', '.salt');
	echo $une;
	}
```

I am trying to print out the decrypted string to see if it works.

----------


## ua549

What results are you getting? Errors? No output?

I'm not a coder, but now that you've posted the source another member may be able to debug your code.

----------


## Wnt2bsleepin

There is no output at all. I know it's encrypting because I can login to my SQL database and see the encrypted data. I just cant seem to decrypt it.

----------


## ua549

Are you actually getting the record?
Try outputting other info such as the encrypted password in addition to the decrypted password.

----------


## Wnt2bsleepin

I modified the code.

It only prints out something if I include a request other than the password



```
if(!define('SALT'))
define('SALT','n7g7b8necefAs5ecrEv3M72ra');

$result = mysql_query("SELECT * FROM accounts");
while($row = mysql_fetch_array($result))
	{
       echo $row['EMAIL'];
	echo $row['PASS'];
	echo AES_DECRYPT('$pass', '.salt');
	
	
 	}
```

It prints out nothing if I exclude the echo $row['EMAIL'];



```
if(!define('SALT'))
define('SALT','n7g7b8necefAs5ecrEv3M72ra');

$result = mysql_query("SELECT * FROM accounts");
while($row = mysql_fetch_array($result))
	{
       
	echo $row['PASS'];
	echo AES_DECRYPT('$pass', '.salt');
	
	
 	}
```

When I can get the unencrypted password to print, it looks like. 



```
��������������������
```

And even then it's only printing out the first row of data, it doesn't print out any of the other information in the table.

----------


## skraps

iv been playing with this alsp



```
if(!define('SALT'))
define('SALT','n7g7b8necefAs5ecrEv3M72ra');
$salt = SALT;
// from my understanding so far , you can't use a wild card in the select because AES_DECRYPT needs to be called inside the query. MySQL is actually doing the processing not PHP.  
$result = mysql_query("SELECT id,username,AES_DECRYPT('password','$sql') FROM accounts");
while($row = mysql_fetch_array($result))
	{
       //now heres the problem Im running into. When the data is returned it looks like this : 
	echo $row['AES_DECRYPT(password,n7g7b8necefAs5ecrEv3M72ra)'];
	
	//I really need it to come back like: $row['oassword'];
 	}
```

----------


## skraps

Ok great and I found the answer to my problem now, lol At least one of them.

http://www.daniweb.com/web-developme...60#post1700560


```
$result = mysql_query("SELECT id,username,AES_DECRYPT('password','$sql') as password FROM accounts");
```

----------


## skraps

// heres the way it needs to be I think
if(!define('SALT'))
define('SALT','n7g7b8necefAs5ecrEv3M72ra');
$salt = SALT;
// from my understanding so far , you can't use a wild card in the select because AES_DECRYPT needs to be called inside the query. MySQL is actually doing the processing not PHP.  
$result = mysql_query("SELECT id,username,AES_DECRYPT('password','$sql') as PASS FROM accounts");
}

----------

