How to see the oiginal data via decrypt in mysql? - mysql

I encrypted my password field and inserted it into a MySQL table.
After that I decrypted that column by
select fld_user,fld_pwd,AES_DECRYPT(fld_encryptedpwd,'key')
from users
where fld_id='1903';
But, the result is showing "BLOB". I used Varbinary() as the datatype for encrypted column. What I should do ?

Solution for in MySQL Workbench is to toggle the following option for the SQL Editor: "Treat BINARY/VARBINARY as nonbinary character string." At least on MacOS X, you'll need to restart Workbench for the option to take effect. (You can also right click the value and do 'Open Value in Viewer').
If you don't want to change the options in Workbench, you can use the CAST() function to return the result of AES_DECRYPT() as a string:
SELECT fld_user, fld_pwd, CAST(AES_DECRYPT(fld_encryptedpwd, 'key') AS CHAR)
FROM users WHERE fld_id='1903';
A trick to check/make sure what data type a function would return is to do the following using the MySQL CLI:
mysql> CREATE TALBE tmp1 AS SELECT AES_DECRYPT(fld_encryptedpwd, 'key')
FROM users WHERE fld_id='1903';
mysql> DESC tmp1;
That will show which data type will be returned.
(Again, storing encrypted passwords: not so good.)

Related

Decode Base64 column in mysql shows BLOB [duplicate]

I keep finding that MySQL Workbench shows query results as BLOB. e.g:
SELECT INET_NTOA(167773449) --> BLOB
If I select to 'view value' I can determine the text value is '10.0.5.9' but it's pretty irritating when I SELECT multiple rows and want to glance at the contents.
Is there a way around this or is it a limitation of the tool?
Background:
This problem occurs when the binary string values (BINARY/VARBINARY type) are returned in the results. The binary strings contain the zero bytes and for some reason, apparently security, have not been shown by default. More details about binary strings here.
Even in the reported example SELECT INET_NTOA(167773449), the function returns binary string. Check this for reference.
Solution:
Since MySQL Workbench v5.2.22, it can be set through preferences whether to SHOW or HIDE such values.
In MySQL Workbench, go to: "Edit -> Preferences... -> SQL Queries" OR "Edit -> Preferences... -> SQL Editor -> SQL Execution" (depending upon what version of Workbench you have).
Check the option 'Treat BINARY/VARBINARY as nonbinary character string' to show the actual value.
Reference:
The original issue has been reported and answered with fix here.
What you can do is Cast your BLOB type to a string. This will simply allow you to glance at whats in your BLOB type when browsing your select statement.
SELECT CAST(`blob_column_name` AS CHAR(10000) CHARACTER SET utf8) FROM `table_name`;
Another (slightly shorter) solution
SELECT CONVERT(FROM_BASE64(myMEDIUMTEXTcol) using utf8) notAblobject FROM myTable;
Unrelated background
My table contains a MEDIUMTEXT column which stores a Base64 string. In my frontend JS code I kept getting an Object returned instead of a string. The Object appeared to contain an array of ASCII values.
Testing the associated query in phpMyAdmin indicated the result was a BLOB. I updated my SQL query, as above, in order to get a simple string returned instead of an Object.

Getting Incorrect string value: '\x92\xB7\xFF\xF3' while using AES_ENCRYPT in MySQL

I know and I've researched a lot of very similar problems in Stackoverflow. I've tried all solutions and nothing has worked to me.
I've used following query to insert user records
INSERT INTO `user`(user_name,`password`) VALUE ("D001",AES_ENCRYPT('password1234',"hello"))
When I do this, I'm getting
1 row(s) affected, 1 warning(s)
Execution Time : 00:00:00:046
Transfer Time : 00:00:00:000
Total Time : 00:00:00:046
Warning Code : 1366
Incorrect string value: '\x92\xB7\xFF\xF3\xD1\xF6...' for column 'password' at row 1
---------------------------------------------------
with the above updation, row is getting inserted with just user_user but password is empty.
What I've done by referring similar problems are as follows
1) SET NAMES utf8mb4
2) ALTER TABLE user CONVERT TO CHARACTER SET utf8mb4;
3) Inside my.ini configuration file, I've added character_set_server=utf8mb4
Still I'm getting the same problem. How to resolve this?
UPDATE:
Problem is not specific to just password. What I wanted to do to scramble all required columns so that users can't see those columns normally.
So, I've used AES_ENCRYPT.
The issue is that you are storing binary data in a char,varchar or text column.
Either
store your data as hex:
INSERT INTO user(user_name,password) VALUE ("D001",HEX(AES_ENCRYPT('password1234',"hello")))
And use the UNHEX() function when you need to decrypt it; AES_DECRYPT(UNHEX(password),"hello")
Or
convert your password column to a type that can store binary data, such as the binary or varbinary type.

Incorrect string value errors in AES_ENCRYPT/AES_DECRYPT

I'm introducing myself to the encryption functions in MYSQL.
Just wrote a simple SQL statement to add an encrypted entry into a field
INSERT INTO test_table (field1) VALUES(aes_encrypt('fieldentry','password'))
When I execute the SQL I get the following error
Error 1366: Incorrect string value: '\xC7\xE13\xC4\xF4!...' for column 'field1' at row 1 SQL Statement - CHANGE COLUMN field1 VARCHAR(255) NOT NULL COMMENT ''
Now I've read it may have something to do with CHARACTER SET, and tried changing it from utf8-default collation to utf8mb4 - default collation as recommended, but that didnt make any difference.
I've also tried changing the column type from VARCHAR to VARBINARY. The SQL statement then ran successfully, however, when I tried the following to retreive the data:
SELECT AES_DECRYPT(field1, '12345') AS endata FROM test_table
Do Until rst.EOF
Response.Write(rst("endata"))
rst.movenext
Loop
The loop runs but no values are returned (blank lines)
I'm just looking for a straightforward what to encrypt and then decrypt my data using a password in the function AES_ENCRYPT/AES_DECRYPT.
AES_ENCRYPT() encrypts a string and returns a binary string.
AES_DECRYPT() decrypts the encrypted string and returns the original
string.
So you should change the type of field1,firstname from VARCHAR(255)to VARBINARY(255). It will fix the issue..
EDIT : For type missmatch.. please try this..
SELECT *,
CAST(AES_DECRYPT(field1, 'mypassword') AS CHAR(50)) end_data
FROM user
Hope this helps..

MD5 function returns empty result

I am trying to return an MD5 encoded string of a value from my database, but it just returns a blank result (not null, just blank).
I have tried just running this query and get the same result:
SELECT MD5('test');
I have tried restarting the MySQL server, MySQL Workbench, etc. But get the same result.
If I try running the same command on a different database/server, it returns the hash string just fine.
What am I doing wrong? Is there a setting I disabled on accident?
Prior to MySQL v5.5.3, MD5() returned a binary string.
By default, MySQL Workbench does not display binary strings (to avoid accidental misinterpretation); however it is possible to display binary string values in output grids: View > Edit > Preferences > SQL Editor > Treat BINARY/VARBINARY as nonbinary character string.
Alternatively, either upgrade your MySQL server or transcode the result to a non-binary character set:
SELECT CONVERT(MD5('test') USING utf8)

How can I directly view blobs in MySQL Workbench

I'm using MySQL Workbench CE 5.2.30 CE / Rev 6790 . When execute the following statement:
SELECT OLD_PASSWORD("test")
I only get back a nice BLOB icon, I need to left-click to select the cell, right-click and choose "Open Value in viewer" and select the "Text" tab.
Using the same with phpMyAdmin, I get directly back the value of the OLD_PASSWORD call. It's just an example, but is there a way to directly see such results in the output?
In short:
Go to Edit > Preferences
Choose SQL Editor
Under SQL Execution, check Treat BINARY/VARBINARY as nonbinary character string
Restart MySQL Workbench (you will not be prompted or informed of this requirement).
In MySQL Workbench 6.0+
Go to Edit > Preferences
Choose SQL Queries
Under Query Results, check Treat BINARY/VARBINARY as nonbinary character string
It's not mandatory to restart MySQL Workbench (you will not be prompted or informed of this requirement).*
With this setting you will be able to concatenate fields without getting blobs.
I think this applies to versions 5.2.22 and later and is the result of this MySQL bug.
Disclaimer: I don't know what the downside of this setting is - maybe when you are selecting BINARY/VARBINARY values you will see it as plain text which may be misleading and/or maybe it will hinder performance if they are large enough?
I'm not sure if this answers the question but if if you right click on the "blob" icon in the field (when viewing the table) there is an option to "Open Value in Editor". One of the tabs lets you view the blob. This is in ver. 5.2.34
Perform three steps:
Go to "WorkBench Preferences" --> Choose "SQL Editor" Under "Query Results": check "Treat BINARY/VARBINARY as nonbinary character string"
Restart MySQL WorkBench.
Now select SELECT SUBSTRING(<BLOB_COLUMN_NAME>,1,2500) FROM <Table_name>;
casting works, but it is a pain, so I would recommend using spioter's method unless you are using a lot of truly blob data.
SELECT CAST(OLD_PASSWORD("test") AS CHAR)
You can also cast as other types, and even restrict the size, but most of the time I just use CHAR:
http://dev.mysql.com/doc/refman/5.5/en/cast-functions.html#function_cast
select CONVERT((column_name) USING utf8) FROM table;
In my case, Workbench does not work. so i used the above solution to show blob data as text.
Doesn't seem to be possible I'm afraid, its listed as a bug in workbench:
http://bugs.mysql.com/bug.php?id=50692
It would be very useful though!
had the same problem, according to the MySQL documentation, you can select a Substring of a BLOB:
SELECT id, SUBSTRING(comment,1,2000) FROM t
HTH, glissi
I pieced a few of the other posts together, as the workbench 'preferences' fix did not work for me. (WB 6.3)
SELECT CAST(`column` AS CHAR(10000) CHARACTER SET utf8) FROM `table`;
Work bench 6.3
Follow High scoring answer then use UNCOMPRESS()
(In short:
1. Go to Edit > Preferences
2. Choose SQL Editor
3. Under SQL Execution, check Treat BINARY/VARBINARY as nonbinary character string
4. Restart MySQL Workbench (you will not be prompted or informed of this requirement).)
Then
SELECT SUBSTRING(UNCOMPRESS(<COLUMN_NAME>),1,2500) FROM <Table_name>;
or
SELECT CAST(UNCOMPRESS(<COLUMN_NAME>) AS CHAR) FROM <Table_name>;
If you just put UNCOMPRESS(<COLUMN_NAME>) you can right click blob and click "Open Value in Editor".
there is few things that you can do
SELECT GROUP_CONCAT(CAST(name AS CHAR))
FROM product
WHERE id IN (12345,12346,12347)
If you want to order by the query you can order by cast as well like below
SELECT GROUP_CONCAT(name ORDER BY name))
FROM product
WHERE id IN (12345,12346,12347)
as it says on this blog
http://www.kdecom.com/mysql-group-concat-blob-bug-solved/
NOTE: The previous answers here aren't particularly useful if the BLOB is an arbitrary sequence of bytes; e.g. BINARY(16) to store 128-bit GUID or md5 checksum.
In that case, there currently is no editor preference -- though I have submitted a feature request now -- see that request for more detailed explanation.
[Until/unless that feature request is implemented], the solution is HEX function in a query: SELECT HEX(mybinarycolumn) FROM mytable.
An alternative is to use phpMyAdmin instead of MySQL Workbench - there hex is shown by default.