/* create table t12872 (id integer primary key, c1 char(100)); insert into t12872 values(1, 'abc'); ... http://dev.mysql.com/doc/mysql/en/encryption-functions.html "The return string is a binary string where the first character is CHAR(128 | key_num)." mysql> explain SELECT AES_DECRYPT(c1, '123') from t12872 order by id limit 1; +----+-------------+--------+-------+---------------+---------+---------+------+ ------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+-------+---------------+---------+---------+------+ ------+-------+ | 1 | SIMPLE | t12872 | index | NULL | PRIMARY | 4 | NULL | 15 | | +----+-------------+--------+-------+---------------+---------+---------+------+ ------+-------+ 1 row in set (0,00 sec) mysql> explain SELECT AES_DECRYPT(c1, '123') from t12872 order by id; +----+-------------+--------+------+---------------+------+---------+------+---- --+----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | row s | Extra | +----+-------------+--------+------+---------------+------+---------+------+---- --+----------------+ | 1 | SIMPLE | t12872 | ALL | NULL | NULL | NULL | NULL | 1 5 | Using filesort | +----+-------------+--------+------+---------------+------+---------+------+---- --+----------------+ 1 row in set (0,00 sec) mysql> explain SELECT AES_DECRYPT(c1, '123') from t12872; +----+-------------+--------+------+---------------+------+---------+------+---- --+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | row s | Extra | +----+-------------+--------+------+---------------+------+---------+------+---- --+-------+ | 1 | SIMPLE | t12872 | ALL | NULL | NULL | NULL | NULL | 1 5 | | +----+-------------+--------+------+---------------+------+---------+------+---- --+-------+ 1 row in set (0,01 sec) */ #include #include "mysql.h" #define SELECT1 "SELECT AES_DECRYPT(c1, '123') from t12872 order by id limit 1" #define SELECT2 "SELECT AES_DECRYPT(c1, '123') from t12872" int main() { MYSQL *conn; MYSQL_STMT *stmt; MYSQL_RES *meta; MYSQL_FIELD *field; MYSQL_BIND bind = {0}; int res; unsigned long len; char buf[21] = {0}; double d=-1.0; conn= mysql_init(NULL); conn= mysql_real_connect(conn, "127.0.0.1", "root", "", "test", 3306, 0, 0); if (!conn) { fprintf(stderr, "Cannot connect\n"); exit(1); } stmt= mysql_stmt_init(conn); res= mysql_stmt_prepare(stmt, SELECT1, strlen(SELECT1)); meta= mysql_stmt_result_metadata(stmt); field= mysql_fetch_field_direct(meta, 0); printf("type=%d\n", field->type); printf("length=%d\n", field->length); printf("decimals=%d\n", field->decimals); mysql_free_result(meta); res= mysql_stmt_prepare(stmt, SELECT2, strlen(SELECT2)); meta= mysql_stmt_result_metadata(stmt); field= mysql_fetch_field_direct(meta, 0); printf("type=%d\n", field->type); printf("length=%d\n", field->length); printf("decimals=%d\n", field->decimals); mysql_free_result(meta); mysql_close(conn); return 0; }