mysql對資料庫欄位儲存的資料加密

來源:互聯網
上載者:User

標籤:mysql   aes_encryupt   

原表裡面的資料沒有加密,建立了一張加密表,迴圈原表裡面的資料,加密後插入到加密表。最後建立一個觸發器,在原表裡面插入了資料,自動觸發在加密表裡面插入相同的資料。

  • 使用mysql的aes_encrypt加密資料
  • 使用Mysql的aes_decrypt解密資料
  • 因為加密後的資料比較難看,所以使用to_base64轉碼資料和from_base64解碼資料
  • 所以實際儲存的資料是加密後又轉碼的資料
  • 查看資料是先解碼資料在解密資料
  • 指令碼如下
# _*_ coding: utf-8 _*___author__ = ‘xiaoke‘__date__ = ‘2018/6/12 18:25‘"""添加觸發器CREATE TRIGGER `auth_enc_trigger` AFTER INSERT on authFOR EACH ROWINSERT into `auth_enc` (id, real_name, id_number) VALUES (NEW.id,to_base64(aes_encrypt(NEW.real_name, "slwh-dfh")),to_base64(aes_encrypt(NEW.id_number, "slwh-dfh")));查詢加密後的表select id,aes_decrypt(from_base64(real_name),‘slwh-dfh‘),aes_decrypt(from_base64(id_number),‘slwh-dfh‘) from auth_enc;"""import MySQLdbdb = MySQLdb.connect(host=‘localhost‘, port=3306, user=‘root‘, passwd=‘mypassword‘, db=‘my_db‘,                     charset=‘utf8‘)cursor = db.cursor()  # 建立一個遊標對象cursor.execute("select * from auth;")lines = cursor.fetchall()print("一共擷取資料%s條") % (len(lines))for data in lines:    id, real_name, id_number = data    sql = ‘insert into auth_enc (id, real_name, id_number) VALUE (%d,to_base64(aes_encrypt("%s","slwh-dfh")),to_base64(aes_encrypt("%s", "slwh-dfh")));‘ % (        id, real_name, id_number)    print(sql)    try:        cursor.execute(sql)        db.commit()    except Exception, e:        db.rollback()        print(e)cursor.close()

mysql對資料庫欄位儲存的資料加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.