﻿{"id":848,"date":"2022-11-16T15:39:47","date_gmt":"2022-11-16T07:39:47","guid":{"rendered":"http:\/\/blog.sway.com.cn\/?p=848"},"modified":"2022-11-16T15:41:37","modified_gmt":"2022-11-16T07:41:37","slug":"jdk%e7%9a%84aes%e5%8a%a0%e5%af%86%e5%bc%82%e5%b8%b8java-security-invalidkeyexception-illegal-key-size-%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/blog.sway.com.cn\/?p=848","title":{"rendered":"JDK\u7684AES\u52a0\u5bc6\u5f02\u5e38java.security.InvalidKeyException: Illegal key size \u7684\u89e3\u51b3\u65b9\u6cd5"},"content":{"rendered":"<p>\u4eca\u5929\u5728\u8c03\u7528\u83b7\u53d6\u5fae\u4fe1\u652f\u4ed8\u5e73\u53f0\u8bc1\u4e66V3\u63a5\u53e3\u65f6\uff0c\u9700\u8981\u5bf9\u5176\u5185\u5bb9\u8fdb\u884c\u89e3\u5bc6\u3002\u6839\u636e\u5b98\u65b9\u7684demo\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">static final int KEY_LENGTH_BYTE = 32;\r\n\tstatic final int TAG_LENGTH_BIT = 128;\r\n\r\n\tpublic String decryptToString(byte[] apiV3Key, byte[] associatedData, byte[] nonce, String ciphertext) throws GeneralSecurityException, IOException {\r\n\r\n\t\tif (apiV3Key.length != KEY_LENGTH_BYTE) {\r\n\t\t\tthrow new IllegalArgumentException(\"\u65e0\u6548\u7684ApiV3Key\uff0c\u957f\u5ea6\u5fc5\u987b\u4e3a32\u4e2a\u5b57\u8282\");\r\n\t\t}\r\n\r\n\t\ttry {\r\n\t\t\tCipher cipher = Cipher.getInstance(\"AES\/GCM\/NoPadding\");\r\n\r\n\t\t\tSecretKeySpec key = new SecretKeySpec(apiV3Key, \"AES\");\r\n\t\t\tGCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);\r\n\r\n\t\t\tcipher.init(Cipher.DECRYPT_MODE, key, spec);\r\n\t\t\tcipher.updateAAD(associatedData);\r\n\r\n\t\t\treturn new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), \"utf-8\");\r\n\t\t} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {\r\n\t\t\tthrow new IllegalStateException(e);\r\n\t\t} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {\r\n\t\t\tthrow new IllegalArgumentException(e);\r\n\t\t}\r\n\t}<\/pre>\n<p>\u53d1\u73b0\u6267\u884c\u5230 cipher.init(Cipher.DECRYPT_MODE, key, spec); \u7684\u65f6\u5019\u62a5\u00a0java.security.InvalidKeyException: Illegal key size \u9519\u8bef\u3002<\/p>\n<p>\u7ecf\u8fc7\u8d44\u6599\u67e5\u9605\uff0c\u5f97\u51fa\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n<p>JDK\u53d7\u7248\u672c\u5b89\u5168\u9650\u5236\uff0c\u9ed8\u8ba4\u53ea\u5141\u8bb8128\u4f4d\u957f\u5ea6\u4ee5\u5185\u7684\u3002\u79d8\u94a5\u957f\u5ea6\uff0c\u5982\u679c\u5bc6\u94a5\u5927\u4e8e128, \u4f1a\u629b\u51fajava.security.InvalidKeyException: Illegal key size \u5f02\u5e38. java\u8fd0\u884c\u65f6\u73af\u5883\u9ed8\u8ba4\u8bfb\u5230\u7684\u662f\u53d7\u9650\u7684policy\u6587\u4ef6. \u6587\u4ef6\u4f4d\u4e8e${java_home}\/jre\/lib\/security, \u8fd9\u79cd\u9650\u5236\u662f\u56e0\u4e3a\u7f8e\u56fd\u5bf9\u8f6f\u4ef6\u51fa\u53e3\u7684\u63a7\u5236\u6240\u9020\u6210\u7684\u7684.JDK1.8\u4e4b\u540e\u5df2\u7ecf\u517c\u5bb9\u4e86\u8be5\u95ee\u9898\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p><strong>\u65b9\u6cd5\u4e00\uff1a\u5347\u7ea7\u4e0d\u53d7\u9650\u5236JDK\u7248\u672c<\/strong><br \/>\n\u5347\u7ea7JDK9\u7ea7\u4ee5\u4e0a\u7248\u672c\uff0c\u672a\u6d4b\u8bd5<\/p>\n<p><strong>\u65b9\u6cd5\u4e8c\uff1a\u66ff\u6362JDK\u53d7\u9650\u6587\u4ef6\u7ea7\u914d\u7f6e<\/strong><br \/>\nJDK7\u7684\u4e0b\u8f7d\u5730\u5740: Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 7 Download<br \/>\nJDK8\u7684\u4e0b\u8f7d\u5730\u5740: JCE Unlimited Strength Jurisdiction Policy Files for JDK\/JRE 8 Download<br \/>\n\u4e0b\u8f7d\u540e\u89e3\u538b\uff0c\u53ef\u4ee5\u770b\u5230local_policy.jar\u548cUS_export_policy.jar\u4ee5\u53careadme.txt<br \/>\n\u5982\u679c\u5b89\u88c5\u4e86JRE\uff0c\u5c06\u4e24\u4e2ajar\u6587\u4ef6\u653e\u5230%JRE_HOME%\\lib\\security\u76ee\u5f55\u4e0b\u8986\u76d6\u539f\u6765\u7684\u6587\u4ef6<br \/>\n\u5982\u679c\u5b89\u88c5\u4e86JDK\uff0c\u8fd8\u8981\u5c06\u4e24\u4e2ajar\u6587\u4ef6\u4e5f\u653e\u5230%JDK_HOME%\\jre\\lib\\security\u76ee\u5f55\u4e0b\u8986\u76d6\u539f\u6765\u6587\u4ef6<\/p>\n<p><strong>\u65b9\u6cd5\u4e09\uff1aJDK1.8 \u4ee3\u7801\u7b56\u7565\u4fee\u6539<\/strong><br \/>\nJDK1.8 \u5df2\u7ecf\u652f\u6301\u4e0d\u53d7\u9650\u7684jar\uff0c\u4f46\u914d\u7f6e\u9ed8\u8ba4\u662f\u53d7\u9650\u7684<br \/>\n\u4fee\u6539\u4ee3\u7801\u914d\u7f6e\uff0c\u5728\u52a0\u5bc6\u4e4b\u524d\uff0c\u4fee\u6539\u4e0d\u53d7\u9650\u914d\u7f6e.<\/p>\n<pre class=\"lang:default mark:2 decode:true\">WXBizMsgCrypt wxcpt = createWXBizMsgCrypt(suiteId, getCorpId(postData), false);\r\nSecurity.setProperty(\"crypto.policy\", \"unlimited\");\r\nsMsg = wxcpt.decryptMsg(msgSignature, timestamp, nonce, postData);<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5728\u8c03\u7528\u83b7\u53d6\u5fae\u4fe1\u652f\u4ed8\u5e73\u53f0\u8bc1\u4e66V3\u63a5\u53e3\u65f6\uff0c\u9700\u8981\u5bf9\u5176\u5185\u5bb9\u8fdb\u884c\u89e3\u5bc6\u3002\u6839\u636e\u5b98\u65b9\u7684demo\u5982\u4e0b\uff1a static fin [&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-848","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\/848","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=848"}],"version-history":[{"count":2,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/848\/revisions"}],"predecessor-version":[{"id":850,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/848\/revisions\/850"}],"wp:attachment":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=848"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}