{"id":142,"date":"2026-01-22T09:22:33","date_gmt":"2026-01-22T09:22:33","guid":{"rendered":"https:\/\/blog.gpst.net.cn:4008\/?p=142"},"modified":"2026-01-28T06:24:15","modified_gmt":"2026-01-28T06:24:15","slug":"%e4%bd%bf%e7%94%a8-openssl-%e8%bf%9b%e8%a1%8c-aes-256-cbc-%e5%8a%a0%e8%a7%a3%e5%af%86%e7%9a%84-bash-shell-%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/opshub.com.cn\/?p=142","title":{"rendered":"\u4f7f\u7528 OpenSSL \u8fdb\u884c AES-256-CBC \u52a0\u89e3\u5bc6\u7684 bash Shell \u811a\u672c"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n##############################################################################\n# \u811a\u672c\u540d\u79f0: aes_256_cbc_encrypt_decrypt.sh\n# \u811a\u672c\u529f\u80fd: \u4f7f\u7528OpenSSL\u5b9e\u73b0AES-256-CBC\u7b97\u6cd5\u7684\u5b57\u7b26\u4e32\u52a0\u89e3\u5bc6\n# \u52a0\u5bc6\u53c2\u6570\u8bf4\u660e:\n#   - \u5bc6\u94a5(key): 32\u5b57\u8282\u768416\u8fdb\u5236\u5b57\u7b26\u4e32\uff08AES-256\u8981\u6c42\u5bc6\u94a5\u957f\u5ea632\u5b57\u8282\uff09\n#   - \u521d\u59cb\u5411\u91cf(iv): 16\u5b57\u8282\u768416\u8fdb\u5236\u5b57\u7b26\u4e32\uff08CBC\u6a21\u5f0f\u8981\u6c42IV\u957f\u5ea616\u5b57\u8282\uff09\n#   - \u6a21\u5f0f: AES-256-CBC\uff08\u5206\u7ec4\u52a0\u5bc6\u6a21\u5f0f\uff09\n#   - \u8f93\u51fa: Base64\u7f16\u7801\u540e\u7684\u52a0\u5bc6\u5b57\u7b26\u4e32\n# \u4f7f\u7528\u4f9d\u8d56: \u9700\u5b89\u88c5openssl\u5de5\u5177\uff08CentOS: yum install openssl -y\uff09\n##############################################################################\n\n# ======================== \u914d\u7f6e\u9879 ========================\n# \u5f85\u52a0\u5bc6\u7684\u539f\u59cb\u5b57\u7b26\u4e32\uff08\u5305\u542b\u591a\u4e2a\u4e1a\u52a1\u53c2\u6570\uff09\nsource_str=\"USER_INTENRALID=-1        MAC=9c2ea13ee931        USER_IP=168296450        USER_PORT=-1        IN_IP=-1        IN_PORT=-1        USER_ACCESSTIME=1579332881302        PACKETAGETYPE=DNS        HOST=180.153.184.132        IMEI=-1        IMSI=-1        LONGITUDE=-1        LATITUDE=-1        CHANNEL=170000        PATH=LTE=        COOKIE=LTE=        REFERER=LTE=        POSTDATA=djJzLWlvcy55dW50b25ndi5jb20=\"\n \n# AES-256-CBC\u52a0\u5bc6\u5bc6\u94a5\uff0832\u5b57\u828216\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u517164\u4e2a16\u8fdb\u5236\u5b57\u7b26\uff09\nkey=\"f86e92574eaf977469098a59553e2450bec9e4acb1db1a2e2fa35e21bb55dd89\"\n# \u521d\u59cb\u5411\u91cfIV\uff0816\u5b57\u828216\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u517132\u4e2a16\u8fdb\u5236\u5b57\u7b26\uff09\niv=\"c18d8c0b6f5e54f64ac4a401497ea37f\"\n\n# ======================== AES-256-CBC\u52a0\u5bc6 ========================\n# openssl enc\u53c2\u6570\u8bf4\u660e:\n#   -e: \u52a0\u5bc6\u6a21\u5f0f\n#   -aes-256-cbc: \u4f7f\u7528AES-256-CBC\u7b97\u6cd5\n#   -a: \u8f93\u51faBase64\u7f16\u7801\uff08\u4fbf\u4e8e\u4f20\u8f93\u548c\u5b58\u50a8\uff09\n#   -K: \u6307\u5b9a16\u8fdb\u5236\u683c\u5f0f\u7684\u5bc6\u94a5\uff08\u5927\u5199K\uff0c\u533a\u522b\u4e8e\u5c0f\u5199k\uff09\n#   -iv: \u6307\u5b9a16\u8fdb\u5236\u683c\u5f0f\u7684\u521d\u59cb\u5411\u91cf\n#   -nosalt: \u4e0d\u4f7f\u7528\u76d0\u503c\uff08\u786e\u4fdd\u52a0\u89e3\u5bc6\u7ed3\u679c\u56fa\u5b9a\uff09\nencrypt_str=$(echo -n \"${source_str}\" | openssl enc -e -aes-256-cbc -a -K ${key} -iv ${iv} -nosalt)\necho -e \"\u52a0\u5bc6\u540e\u7684\u5b57\u7b26\u4e32(Base64):\\n${encrypt_str}\\n\"\n\n# ======================== AES-256-CBC\u89e3\u5bc6 ========================\n# \u4fee\u590d\u539f\u811a\u672c\u9519\u8bef\uff1a\u89e3\u5bc6\u9700\u4f7f\u7528-d\u53c2\u6570\uff0c\u4e14\u8981\u79fb\u9664\u539f\u9519\u8bef\u7684-e\u53c2\u6570\n# openssl enc\u53c2\u6570\u8bf4\u660e:\n#   -d: \u89e3\u5bc6\u6a21\u5f0f\uff08\u6838\u5fc3\u4fee\u590d\u70b9\uff09\n#   \u5176\u4ed6\u53c2\u6570\u4e0e\u52a0\u5bc6\u4fdd\u6301\u4e00\u81f4\uff0c\u786e\u4fdd\u52a0\u89e3\u5bc6\u53c2\u6570\u5339\u914d\ndecrypt_str=$(echo -n \"${encrypt_str}\" | openssl enc -d -aes-256-cbc -a -K ${key} -iv ${iv} -nosalt)\necho -e \"\u89e3\u5bc6\u540e\u7684\u539f\u59cb\u5b57\u7b26\u4e32:\\n${decrypt_str}\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-9"],"_links":{"self":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=142"}],"version-history":[{"count":1,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}