日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql aes encrypt_mysql加密函数aes_encrypt()和aes_decrypt()使用教程

發(fā)布時間:2025/3/15 数据库 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql aes encrypt_mysql加密函数aes_encrypt()和aes_decrypt()使用教程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

aes_encrypt()和aes_decrypt()在mysql中是進(jìn)行加密了,我們今天一起來和各位看看關(guān)于mysql中aes_encrypt()和aes_decrypt()函數(shù)的使用例子.

如果你需要對mysql某些字段進(jìn)行加解密的話,使用mysql的加解密函數(shù)可能比程序中處理更方便.

mysql-encrypt-funcs.png以aes_encrypt()和aes_decrypt()為例

特別需要注意的時mysql5.5及以下的版本僅支持aes-128-ecb模式,如果需要其它模式需要mysql5.6及以上版本才支持,可通過mysql全局變量如下方式指定:

mysql> SET block_encryption_mode = 'aes-256-cbc';

mysql> SET @key_str = SHA2('My secret passphrase',512);

mysql> SET @init_vector = RANDOM_BYTES(16);

mysql> SET @crypt_str = AES_ENCRYPT('text',@key_str,@init_vector);

mysql> SELECT AES_DECRYPT(@crypt_str,@key_str,@init_vector);

-----------------------------------------------

| AES_DECRYPT(@crypt_str,@key_str,@init_vector) |

-----------------------------------------------

| text????????????????????????????????????????? |

-----------------------------------------------

參考文檔如下:

https://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html#function_aes-encrypt

http://dev.mysql.com/doc/refman/5.7/en/encryption-functions.html#function_aes-encrypt

關(guān)于加密的二進(jìn)制數(shù)據(jù)在mysql中字段存什么類型(存blob還是varbinay類型)?

引用文檔一段話:

Many encryption and compression functions return strings for which the result might contain arbitrary byte values. If you want to store these results, use a column with a VARBINARY or BLOB binary string data type. This will avoid potential problems with trailing space removal or character set conversion that would change data values, such as may occur if you use a nonbinary string data type (CHAR, VARCHAR, TEXT).

盡量使用blob類型,原因如下:

There is no trailing-space removal for BLOB columns when values are stored or retrieved.

For indexes on BLOB columns, you must specify an index prefix length.

BLOB columns can not have DEFAULT values.

二進(jìn)制數(shù)據(jù)如何使用sql插入?

不可直接拼接sql插入,否則會被當(dāng)成字符串處理,不可你可以將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換程十六進(jìn)制或base64插入,相應(yīng)的,取出來的時候你也需要轉(zhuǎn)換。但是通過mysql prepared statement方式可以插入stream data,如php pdo可以類似如下實(shí)現(xiàn):

$db = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');

$stmt = $db->prepare("insert into images (id, contenttype, imagedata) values (?, ?, ?)");

$id = get_new_id(); // some function to allocate a new ID

// assume that we are running as part of a file upload form

// You can find more information in the PHP documentation

$fp = fopen($_FILES['file']['tmp_name'], 'rb');

$stmt->bindParam(1, $id);

$stmt->bindParam(2, $_FILES['file']['type']);

$stmt->bindParam(3, $fp, PDO::PARAM_LOB);

$db->beginTransaction();

$stmt->execute();

$db->commit();

總結(jié)

以上是生活随笔為你收集整理的mysql aes encrypt_mysql加密函数aes_encrypt()和aes_decrypt()使用教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。