{"id":92,"date":"2023-02-14T21:50:02","date_gmt":"2023-02-14T13:50:02","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=92"},"modified":"2023-02-14T21:50:43","modified_gmt":"2023-02-14T13:50:43","slug":"java-desede-encrypt-decrypt","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/14\/java-desede-encrypt-decrypt\/","title":{"rendered":"Java\u5b9e\u73b0DESede\u5bf9\u79f0\u52a0\u89e3\u5bc6"},"content":{"rendered":"<p>DESede\u662f\u7531DES\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u6539\u8fdb\u540e\u7684\u4e00\u79cd\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u3002\u4f7f\u7528 168 \u4f4d\u7684\u5bc6\u94a5\u5bf9\u8d44\u6599\u8fdb\u884c\u4e09\u6b21\u52a0\u5bc6\u7684\u4e00\u79cd\u673a\u5236\uff1b\u5b83\u901a\u5e38\uff08\u4f46\u975e\u59cb\u7ec8\uff09\u63d0\u4f9b\u6781\u5176\u5f3a\u5927\u7684\u5b89\u5168\u6027\u3002\u5982\u679c\u4e09\u4e2a56\u4f4d\u7684\u5b50\u5143\u7d20\u90fd\u76f8\u540c\uff0c\u5219\u4e09\u91cdDES\u5411\u540e\u517c\u5bb9DES\u3002<\/p>\n<h2>\u6dfb\u52a0\u4f9d\u8d56<\/h2>\n<p>\u4f7f\u7528\u8fd9\u4e2a\u4f9d\u8d56\u4e2d\u7684base64\u8fdb\u884c\u7f16\u89e3\u7801<\/p>\n<pre><code class=\"language-xml\">&lt;!-- base64\u7f16\u7801\u4f7f\u7528 --&gt;\n&lt;dependency&gt;\n     &lt;groupId&gt;commons-codec&lt;\/groupId&gt;\n     &lt;artifactId&gt;commons-codec&lt;\/artifactId&gt;\n     &lt;version&gt;1.12&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2>\u4ee3\u7801\u5b9e\u73b0<\/h2>\n<pre><code class=\"language-java\">import javax.crypto.Cipher;\nimport javax.crypto.KeyGenerator;\nimport javax.crypto.SecretKey;\nimport javax.crypto.spec.SecretKeySpec;\nimport org.apache.commons.codec.binary.Base64;\n\npublic class DESedeUtil {\n    \/**\n     * \u5bc6\u94a5\u7b97\u6cd5\n     *\/\n    private static final String KEY_ALGORITHM = &quot;DESede&quot;;\n\n    \/**\n     * \u52a0\u5bc6\/\u89e3\u5bc6\u7b97\u6cd5 \/ \u5de5\u4f5c\u6a21\u5f0f \/ \u586b\u5145\u65b9\u5f0f\n     * Java 6\u652f\u6301PKCS5Padding\u586b\u5145\u65b9\u5f0f\n     * Bouncy Castle\u652f\u6301PKCS7Padding\u586b\u5145\u65b9\u5f0f\n     *\/\n    private static final String CIPHER_ALGORITHM = &quot;DESede\/ECB\/PKCS5Padding&quot;;\n\n    \/**\n     * @Description: \u751f\u6210\u5bc6\u94a5, \u8fd4\u56de168\u4f4d\u7684\u5bc6\u94a5\n     * @return\n     * @throws Exception\n     *\/\n    public static String generateKey() throws Exception {\n        \/\/\u5b9e\u4f8b\u5316\u5bc6\u94a5\u751f\u6210\u5668\n        KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);\n        \/\/DESede \u8981\u6c42\u5bc6\u94a5\u957f\u5ea6\u4e3a 112\u4f4d\u6216168\u4f4d\n        kg.init(168);\n        \/\/\u751f\u6210\u5bc6\u94a5\n        SecretKey secretKey = kg.generateKey();\n        \/\/\u83b7\u5f97\u5bc6\u94a5\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\n        return Base64.encodeBase64String(secretKey.getEncoded());\n    }\n\n    \/**\n     * @Description: DES\u8fdb\u884c\u52a0\u5bc6\n     * @param source \u5f85\u52a0\u5bc6\u7684\u539f\u5b57\u7b26\u4e32\n     * @param key  \u52a0\u5bc6\u65f6\u4f7f\u7528\u7684 \u5bc6\u94a5\n     * @return   \u8fd4\u56de\u7ecf\u8fc7base64\u7f16\u7801\u7684\u5b57\u7b26\u4e32\n     * @throws Exception\n     *\/\n    public static String encrypt(String source, String key) throws Exception {\n        byte[] sourceBytes = source.getBytes(&quot;UTF-8&quot;);\n        byte[] keyBytes = Base64.decodeBase64(key);\n        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);\n        cipher.init(Cipher.ENCRYPT_MODE,new SecretKeySpec(keyBytes, KEY_ALGORITHM));\n        byte[] decrypted = cipher.doFinal(sourceBytes);\n        return Base64.encodeBase64String(decrypted);\n    }\n\n    \/**\n     * @Description:  DES\u89e3\u5bc6\n     * @param encryptStr  DES\u52a0\u5bc6\u540e\u7684\u518d\u7ecf\u8fc7base64\u7f16\u7801\u7684\u5bc6\u6587\n     * @param key  \u52a0\u5bc6\u4f7f\u7528\u7684\u5bc6\u94a5\n     * @return  \u8fd4\u56de utf-8 \u7f16\u7801\u7684\u660e\u6587\n     * @throws Exception\n     *\/\n    public static String decrypt(String encryptStr, String key) throws Exception {\n        byte[] sourceBytes = Base64.decodeBase64(encryptStr);\n        byte[] keyBytes = Base64.decodeBase64(key);\n        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);\n        cipher.init(Cipher.DECRYPT_MODE,new SecretKeySpec(keyBytes, KEY_ALGORITHM));\n        byte[] decoded = cipher.doFinal(sourceBytes);\n        return new String(decoded, &quot;UTF-8&quot;);\n    }\n\n    \/\/ test\n    public static void main(String[] args) {\n        try {\n            \/\/ \u751f\u6210\u79d8\u94a5\n            String key = generateKey();\n            System.out.println(&quot;\u79d8\u94a5\uff1a&quot;+key);\n\n            \/\/ \u52a0\u5bc6\n            String encryptStr = encrypt(&quot;hello&quot;, key);\n            System.out.println(&quot;\u5bc6\u6587\uff1a&quot;+ encryptStr);\n            \/\/ \u89e3\u5bc6\n            String resource = decrypt(encryptStr, key);\n            System.out.println(&quot;\u660e\u6587\uff1a&quot;+ resource);\n\n            System.out.println(&quot;\u6821\u9a8c\uff1a&quot;+ &quot;hello&quot;.equals(resource));\n\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>DESede\u662f\u7531DES\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u6539\u8fdb\u540e\u7684\u4e00\u79cd\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u3002\u4f7f\u7528 168 \u4f4d\u7684\u5bc6\u94a5\u5bf9\u8d44\u6599\u8fdb\u884c\u4e09\u6b21\u52a0\u5bc6\u7684\u4e00\u79cd\u673a\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[56],"class_list":["post-92","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-des"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}