{"id":567,"date":"2015-12-10T11:20:13","date_gmt":"2015-12-10T11:20:13","guid":{"rendered":"http:\/\/www.intelli.ch\/?p=567"},"modified":"2016-02-26T09:04:13","modified_gmt":"2016-02-26T09:04:13","slug":"mysql-aes-encription","status":"publish","type":"post","link":"https:\/\/www.dev-metal.ch\/?p=567","title":{"rendered":"Mysql AES Encription"},"content":{"rendered":"<h1>MySql AES Encription<\/h1>\n<p>Mysql bietet die M\u00f6glichkeit an, Eintr\u00e4ge bzw. Felder verschl\u00fcsselt abzuspeichern. Dazu wird die Stored Function AES_Encrypt und AES_Decrypt angeboten:<\/p>\n<pre class=\"lang:mysql decode:true \">CREATE  TABLE `user` (\r\n`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,\r\n`first_name` LONGBLOB NULL ,\r\n`address` LONGBLOB NOT NULL ,\r\nPRIMARY KEY (`id`))\r\nENGINE = InnoDB\r\nDEFAULT CHARACTER SET = utf8\r\nCOLLATE = utf8_general_ci<\/pre>\n<p><!--more--><\/p>\n<h5>Mit Standard aes-128-ecb<\/h5>\n<pre class=\"lang:mysql decode:true \">INSERT into user (first_name, address) VALUES (AES_ENCRYPT('Hansueli', SHA2('supersichereraeskeyAlsHash',512)),AES_ENCRYPT('Heimatadresse', SHA2('supersichereraeskeyAlsHash',512)));<\/pre>\n<pre class=\"lang:sh decode:true\">SELECT AES_DECRYPT(first_name, SHA2('supersichereraeskeyAlsHash',512)) as first_name, AES_DECRYPT(address, SHA2('supersichereraeskeyAlsHash',512)) as address from user;<\/pre>\n<h5>Mit aes-256-cbc<\/h5>\n<p>Ab Version 5.6.17 bietet MySQL nebst ecb auch noch weitere Modi an. Z.B. 256 bit cbc. CBC ben\u00f6tigt aber zus\u00e4tzlich noch IV-Bytes:<\/p>\n<pre class=\"lang:mysql decode:true\">SET @iv = RANDOM_BYTES(16);<\/pre>\n<p>IV-Bytes werden entsprechend verwendet:<\/p>\n<pre class=\"lang:mysql decode:true\">ALTER TABLE user ADD COLUMN iv BINARY(16);<\/pre>\n<pre class=\"lang:mysql decode:true\">INSERT into user (id, first_name, address, iv) VALUES (10, AES_ENCRYPT('Hansueli', SHA2('supersichereraeskeyAlsHash',512),@iv), AES_ENCRYPT('Heimatadresse', SHA2('supersichereraeskeyAlsHash',512),@iv),@iv);<\/pre>\n<pre>SELECT AES_DECRYPT(first_name, SHA2('supersichereraeskeyAlsHash',512), (select iv from user where id = 10)) as first_name, AES_DECRYPT(address, SHA2('supersichereraeskeyAlsHash',512),(select iv from user where id = 10)) as address, iv from user where id = 10;<\/pre>\n<p>Die IV-Bytes m\u00fcssen mit (wirklichem!!) Zufallsprinzip erstellt und abgespeichert werden.<\/p>\n<h2>Verschl\u00fcsselungs-Algorithmus<\/h2>\n<p>Mysql benutzt per Standard einen 128-bit Schl\u00fcssel mit\u00a0ECB block cipher mode. Das reicht wohl f\u00fcr die meisten Bed\u00fcrfnisse aus. Dies kann aber auch bei Bedarf ge\u00e4ndert werden.<\/p>\n<h3>Anzeigen des verwendeten Algorithmus<\/h3>\n<pre class=\"lang:mysql decode:true \">mysql&gt; SELECT @@session.block_encryption_mode;\r\n+---------------------------------+\r\n| @@session.block_encryption_mode |\r\n+---------------------------------+\r\n| aes-128-ecb                     |\r\n+---------------------------------+\r\n1 row in set (0.00 sec)<\/pre>\n<p>\u00c4ndern des Algorithmus<\/p>\n<pre class=\"lang:mysql decode:true \">mysql&gt; SET @@session.block_encryption_mode = 'aes-256-cbc';\r\nQuery OK, 0 rows affected (0.00 sec)<\/pre>\n<p>Der Standard-Algorithmus kann aber auch auf der DB konfiguriert werden. Dazu muss im File\u00a0my.ini folgendes eingetragen werden:<\/p>\n<pre class=\"lang:mysql decode:true \">[mysqld]\r\n...\r\nblock_encryption_mode=aes-256-cbc<\/pre>\n<p>Zu erw\u00e4hnen ist, dass dieser Standard \u00fcber den obigen SET-Befehl \u00fcbersteuert werden kann.<\/p>\n<p>&nbsp;<\/p>\n<h1>Referenzen<\/h1>\n<ul>\n<li><a href=\"http:\/\/thinkdiff.net\/mysql\/encrypt-mysql-data-using-aes-techniques\/\" target=\"_blank\">http:\/\/thinkdiff.net\/mysql\/encrypt-mysql-data-using-aes-techniques\/<\/a><\/li>\n<li><a href=\"http:\/\/mysqlblog.fivefarmers.com\/2014\/03\/27\/mysql-5-6-17-now-with-better-encryption\/\" target=\"_blank\">http:\/\/mysqlblog.fivefarmers.com\/2014\/03\/27\/mysql-5-6-17-now-with-better-encryption\/<\/a><\/li>\n<li><a href=\"http:\/\/mysqlserverteam.com\/understand-and-satisfy-your-aes-encryption-needs-with-5-6-17\/\" target=\"_blank\">http:\/\/mysqlserverteam.com\/understand-and-satisfy-your-aes-encryption-needs-with-5-6-17\/<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySql AES Encription Mysql bietet die M\u00f6glichkeit an, Eintr\u00e4ge bzw. Felder verschl\u00fcsselt abzuspeichern. Dazu wird die Stored Function AES_Encrypt und AES_Decrypt angeboten: CREATE TABLE `user` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT , `first_name` LONGBLOB NULL , `address` LONGBLOB NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 COLLATE = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[17],"tags":[],"class_list":["post-567","post","type-post","status-publish","format-standard","hentry","category-mysql"],"modified_by":"ralph","_links":{"self":[{"href":"https:\/\/www.dev-metal.ch\/index.php?rest_route=\/wp\/v2\/posts\/567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-metal.ch\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-metal.ch\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-metal.ch\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-metal.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=567"}],"version-history":[{"count":9,"href":"https:\/\/www.dev-metal.ch\/index.php?rest_route=\/wp\/v2\/posts\/567\/revisions"}],"predecessor-version":[{"id":621,"href":"https:\/\/www.dev-metal.ch\/index.php?rest_route=\/wp\/v2\/posts\/567\/revisions\/621"}],"wp:attachment":[{"href":"https:\/\/www.dev-metal.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-metal.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-metal.ch\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}