﻿{"id":854,"date":"2022-11-16T20:51:09","date_gmt":"2022-11-16T12:51:09","guid":{"rendered":"http:\/\/blog.sway.com.cn\/?p=854"},"modified":"2022-11-16T20:51:09","modified_gmt":"2022-11-16T12:51:09","slug":"%e5%be%ae%e4%bf%a1%e6%94%af%e4%bb%98%e5%b9%b3%e5%8f%b0%e8%af%81%e4%b9%a6%e8%bf%94%e5%9b%9e%e8%a7%a3%e5%af%86%e6%96%b9%e6%b3%95%e5%b0%81%e8%a3%85","status":"publish","type":"post","link":"http:\/\/blog.sway.com.cn\/?p=854","title":{"rendered":"\u5fae\u4fe1\u652f\u4ed8\u5e73\u53f0\u8bc1\u4e66\u8fd4\u56de\u89e3\u5bc6\u65b9\u6cd5\u5c01\u88c5"},"content":{"rendered":"<p>\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u4e0a\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package com.isuidian.util;\r\n\r\nimport java.io.IOException;\r\nimport java.security.*;\r\nimport java.util.Base64;\r\n\r\nimport javax.crypto.Cipher;\r\nimport javax.crypto.NoSuchPaddingException;\r\nimport javax.crypto.spec.GCMParameterSpec;\r\nimport javax.crypto.spec.SecretKeySpec;\r\n\r\npublic class WechatStaticUtil {\r\n\r\n    private static final int KEY_LENGTH_BYTE = 32;\r\n    private static final int TAG_LENGTH_BIT = 128;\r\n\r\n    public static String decryptToString(byte[] apiV3Key, byte[] associatedData, byte[] nonce, String ciphertext) throws GeneralSecurityException, IOException {\r\n\r\n        if (apiV3Key.length != KEY_LENGTH_BYTE) {\r\n            throw new IllegalArgumentException(\"\u65e0\u6548\u7684ApiV3Key\uff0c\u957f\u5ea6\u5fc5\u987b\u4e3a32\u4e2a\u5b57\u8282\");\r\n        }\r\n\r\n        try {\r\n            Cipher cipher = Cipher.getInstance(\"AES\/GCM\/NoPadding\");\r\n\r\n            SecretKeySpec key = new SecretKeySpec(apiV3Key, \"AES\");\r\n            GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);\r\n            cipher.init(Cipher.DECRYPT_MODE, key, spec);\r\n            cipher.updateAAD(associatedData);\r\n\r\n            return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), \"utf-8\");\r\n        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {\r\n            throw new IllegalStateException(e);\r\n        } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {\r\n            throw new IllegalArgumentException(e);\r\n        }\r\n    }\r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u4e0a\u4ee3\u7801\uff1a package com.isuidian.util; import java.io. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-854","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=854"}],"version-history":[{"count":1,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/854\/revisions"}],"predecessor-version":[{"id":855,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/854\/revisions\/855"}],"wp:attachment":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}