{"id":908,"date":"2024-11-07T12:43:49","date_gmt":"2024-11-07T04:43:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/908\/"},"modified":"2024-11-07T12:43:49","modified_gmt":"2024-11-07T04:43:49","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-go-%e4%b8%ad%e4%bd%bf%e7%94%a8-aes-%e5%8a%a0%e5%af%86%e6%96%b9%e6%b3%95%e5%af%b9%e6%98%8e%e6%96%87%e8%bf%9b%e8%a1%8c%e5%8a%a0%e5%af%86%e5%b9%b6%e7%bc%96%e7%a0%81%e4%b8%ba-b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/908\/","title":{"rendered":"\u5982\u4f55\u5728 Go \u4e2d\u4f7f\u7528 AES \u52a0\u5bc6\u65b9\u6cd5\u5bf9\u660e\u6587\u8fdb\u884c\u52a0\u5bc6\u5e76\u7f16\u7801\u4e3a base64 \u5b57\u7b26\u4e32\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-996\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173061938058043.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173061938058043.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173061938058043-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173061938058043-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173061938058043-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"\u5982\u4f55\u5728 Go \u4e2d\u4f7f\u7528 AES \u52a0\u5bc6\u65b9\u6cd5\u5bf9\u660e\u6587\u8fdb\u884c\u52a0\u5bc6\u5e76\u7f16\u7801\u4e3a base64 \u5b57\u7b26\u4e32\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Go \u4e2d\u4f7f\u7528 AES \u52a0\u5bc6\u65b9\u6cd5\u5bf9\u660e\u6587\u8fdb\u884c\u52a0\u5bc6\u5e76\u7f16\u7801\u4e3a base64 \u5b57\u7b26\u4e32\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>aes \u52a0\u5bc6\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 go \u4e2d\u5b9e\u73b0 aes \u52a0\u5bc6\u65f6\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u5bfc\u5165\u4f9d\u8d56\u5e93\uff1a<\/strong>import &#8220;crypto\/aes&#8221;<\/li>\n<li> <strong>\u521b\u5efa aes \u5bc6\u7801\u5757\uff1a<\/strong>cipher, _ := aes.newcipher(key)<\/li>\n<li> <strong>\u4e3a\u660e\u6587\u8fdb\u884c\u586b\u5145\uff1a<\/strong>\u660e\u6587\u7684\u957f\u5ea6\u53ef\u80fd\u4e0d\u662f aes \u5bc6\u7801\u5757\u5927\u5c0f\u7684\u500d\u6570\uff0c\u56e0\u6b64\u9700\u8981\u8fdb\u884c\u586b\u5145\u4ee5\u6ee1\u8db3\u8981\u6c42\u3002<\/li>\n<li> <strong>\u6267\u884c\u52a0\u5bc6\uff1a<\/strong>cipher.encrypt([]byte(ciphertext), []byte(plaintext))<\/li>\n<li> <strong>\u5bf9\u5bc6\u6587\u8fdb\u884c base64 \u7f16\u7801\uff1a<\/strong>ciphertext = base64.stdencoding.encodetostring([]byte(ciphertext))<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684 go \u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"crypto\/aes\"\n    \"encoding\/base64\"\n)\n\nfunc main() {\n    str := []byte(\"406BF0AD11310101220213481000320000\")\n    key := []byte(\"ER2Fb6ts3ECX\")\n\n    cipher, _ := aes.NewCipher(key)\n    length := (len(str) + aes.BlockSize) \/ aes.BlockSize\n    plain := make([]byte, length*aes.BlockSize)\n    copy(plain, str)\n\n    cipherText := make([]byte, len(plain))\n    cipher.Encrypt(cipherText, plain)\n\n    result := base64.StdEncoding.EncodeToString(cipherText)\n    println(result)\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6210\u529f\u5bf9\u7ed9\u5b9a\u7684\u660e\u6587\u8fdb\u884c aes \u52a0\u5bc6\uff0c\u5e76\u5c06\u5bc6\u6587\u7f16\u7801\u4e3a base64 \u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Go \u4e2d\u4f7f\u7528 AES \u52a0\u5bc6\u65b9\u6cd5\u5bf9\u660e\u6587\u8fdb\u884c\u52a0\u5bc6\u5e76\u7f16\u7801\u4e3a base64 \u5b57\u7b26\u4e32\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>aes \u52a0\u5bc6\u65b9\u6cd5 \u5728 go \u4e2d\u5b9e\u73b0 aes \u52a0\u5bc6\u65f6\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u5bfc\u5165\u4f9d\u8d56\u5e93\uff1aimport &#8220;crypto\/aes&#8221; \u521b\u5efa aes \u5bc6\u7801\u5757\uff1acipher, _ := aes.newcipher(key) \u4e3a\u660e\u6587\u8fdb\u884c\u586b\u5145\uff1a\u660e\u6587\u7684\u957f\u5ea6\u53ef\u80fd\u4e0d\u662f aes \u5bc6\u7801\u5757\u5927\u5c0f\u7684\u500d\u6570\uff0c\u56e0\u6b64\u9700\u8981\u8fdb\u884c\u586b\u5145\u4ee5\u6ee1\u8db3\u8981\u6c42\u3002 \u6267\u884c\u52a0\u5bc6\uff1acipher.encrypt([]byte(ciphertext), []byte(plaintext)) \u5bf9\u5bc6\u6587\u8fdb\u884c base64 \u7f16\u7801\uff1aciphertext = base64.stdencoding.encodetostring([]byte(ciphertext)) \u4e0b\u9762\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684 go \u4ee3\u7801\u793a\u4f8b\uff1a package main import ( &#8220;crypto\/aes&#8221; &#8220;encoding\/base64&#8221; ) func main() { str := []byte(&#8220;406BF0AD11310101220213481000320000&#8221;) key := []byte(&#8220;ER2Fb6ts3ECX&#8221;) cipher, _ := aes.NewCipher(key) length := (len(str) + aes.BlockSize) \/ aes.BlockSize [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-908","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=908"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/908\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}