{"id":1383,"date":"2023-03-19T11:02:20","date_gmt":"2023-03-19T03:02:20","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1383"},"modified":"2023-04-28T21:12:06","modified_gmt":"2023-04-28T13:12:06","slug":"implementing-3des-encryption-algorithm-in-java","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/19\/implementing-3des-encryption-algorithm-in-java\/","title":{"rendered":"Java\u5b9e\u73b03DES\u52a0\u5bc6\u7b97\u6cd5"},"content":{"rendered":"<h2>\u52a0\u5bc6\u7b80\u4ecb<\/h2>\n<p>\u5355\u5411\u52a0\u5bc6\uff1a\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u6458\u8981\u8ba1\u7b97\u751f\u6210\u5bc6\u6587\uff0c\u5bc6\u6587\u4e0d\u53ef\u9006\u63a8\u8fd8\u539f\u3002\u7b97\u6cd5\u4ee3\u8868\uff1aMD5\uff0cSHA<\/p>\n<p>\u53cc\u5411\u52a0\u5bc6\uff1a\u4e0e\u5355\u5411\u52a0\u5bc6\u76f8\u53cd\uff0c\u53ef\u4ee5\u628a\u5bc6\u6587\u9006\u63a8\u8fd8\u539f\u6210\u660e\u6587\uff0c\u53cc\u5411\u52a0\u5bc6\u53c8\u5206\u4e3a\u5bf9\u79f0\u52a0\u5bc6\u548c\u975e\u5bf9\u79f0\u52a0\u5bc6<\/p>\n<p><!-- more --><\/p>\n<p>\u5bf9\u79f0\u52a0\u5bc6\uff1a\u6307\u6570\u636e\u4f7f\u7528\u8005\u5fc5\u987b\u62e5\u6709\u76f8\u540c\u7684\u5bc6\u94a5\u624d\u53ef\u4ee5\u8fdb\u884c\u52a0\u5bc6\u89e3\u5bc6\u3002\u7b97\u6cd5\u4ee3\u8868\uff1aDES\uff0c3DES\uff0cAES\uff0cIDEA\uff0cRC4\uff0cRC5;<\/p>\n<p>\u975e\u5bf9\u79f0\u52a0\u5bc6\uff1a\u975e\u5bf9\u79f0\u52a0\u5bc6\u662f\u4e00\u79cd\u201c\u4fe1\u606f\u516c\u5f00\u7684\u5bc6\u94a5\u4ea4\u6362\u534f\u8bae\u201d\u3002\u975e\u5bf9\u79f0\u52a0\u5bc6\u9700\u8981\u516c\u5f00\u5bc6\u94a5\u548c\u79c1\u6709\u5bc6\u94a5\u4e24\u7ec4\u5bc6\u94a5\uff0c\u516c\u5f00\u5bc6\u94a5\u548c\u79c1\u6709\u5bc6\u94a5\u662f\u914d\u5bf9\u8d77\u6765\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u4f7f\u7528\u516c\u5f00\u5bc6\u94a5\u8fdb\u884c\u6570\u636e\u52a0\u5bc6\uff0c\u53ea\u6709\u5bf9\u5e94\u7684\u79c1\u6709\u5bc6\u94a5\u624d\u80fd\u89e3\u5bc6\u3002\u7b97\u6cd5\u4ee3\u8868\uff1aRSA\uff0cDSA\u3002<\/p>\n<p>3DES\u7b97\u6cd5\uff1a3DES\u662f\u4e09\u91cd\u6570\u636e\u52a0\u5bc6\uff0c\u4e14\u53ef\u4ee5\u9006\u63a8\u7684\u4e00\u79cd\u7b97\u6cd5\u65b9\u6848\u3002\u4f46\u7531\u4e8e3DES\u7684\u7b97\u6cd5\u662f\u516c\u5f00\u7684\uff0c\u6240\u4ee5\u7b97\u6cd5\u672c\u8eab\u6ca1\u6709\u5bc6\u94a5\u53ef\u8a00\uff0c\u4e3b\u8981\u4f9d\u9760\u552f\u4e00\u5bc6\u94a5\u6765\u786e\u4fdd\u6570\u636e\u52a0\u89e3\u5bc6\u7684\u5b89\u5168\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4ecd\u6ca1\u6709\u4eba\u80fd\u7834\u89e33DES\u3002<\/p>\n<h2>3DES\u52a0\u5bc6\u7c7b<\/h2>\n<h3>CBC\u6a21\u5f0f\uff08\u9700\u8981\u6307\u5b9aIV\u52a0\u5bc6\u5411\u91cf\uff09<\/h3>\n<pre><code class=\"language-java\">package cn.appblog.util;\n\nimport javax.crypto.Cipher;\nimport javax.crypto.SecretKey;\nimport javax.crypto.SecretKeyFactory;\nimport javax.crypto.spec.DESKeySpec;\nimport javax.crypto.spec.DESedeKeySpec;\nimport javax.crypto.spec.IvParameterSpec;\n\nimport sun.misc.BASE64Decoder;\nimport sun.misc.BASE64Encoder;\n\n@SuppressWarnings({ &quot;restriction&quot; })\npublic class ThreeDES {\n    private static final String IV = &quot;1234567-&quot;;\n    public static final String KEY = &quot;AppBlog.CN&quot;;\n\n    \/**\n     * DESCBC\u52a0\u5bc6\n     *\n     * @param src\n     *            \u6570\u636e\u6e90\n     * @param key\n     *            \u5bc6\u94a5\uff0c\u957f\u5ea6\u5fc5\u987b\u662f8\u7684\u500d\u6570\n     * @return \u8fd4\u56de\u52a0\u5bc6\u540e\u7684\u6570\u636e\n     * @throws Exception\n     *\/\n    public String encryptDESCBC(final String src, final String key) throws Exception {\n\n        \/\/ --\u751f\u6210key\uff0c\u540c\u65f6\u5236\u5b9a\u662fdes\u8fd8\u662fDESede\uff0c\u4e24\u8005\u7684key\u957f\u5ea6\u8981\u6c42\u4e0d\u540c\n        final DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(&quot;UTF-8&quot;));\n        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(&quot;DES&quot;);\n        final SecretKey secretKey = keyFactory.generateSecret(desKeySpec);\n\n        \/\/ --\u52a0\u5bc6\u5411\u91cf\n        final IvParameterSpec iv = new IvParameterSpec(IV.getBytes(&quot;UTF-8&quot;));\n\n        \/\/ --\u901a\u8fc7Chipher\u6267\u884c\u52a0\u5bc6\u5f97\u5230\u7684\u662f\u4e00\u4e2abyte\u7684\u6570\u7ec4\uff0cCipher.getInstance(&quot;DES&quot;)\u5c31\u662f\u91c7\u7528ECB\u6a21\u5f0f\uff0ccipher.init(Cipher.ENCRYPT_MODE, secretKey)\u5373\u53ef\n        final Cipher cipher = Cipher.getInstance(&quot;DES\/CBC\/PKCS5Padding&quot;);\n        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);\n        final byte[] b = cipher.doFinal(src.getBytes(&quot;UTF-8&quot;));\n\n        \/\/ --\u901a\u8fc7base64\uff0c\u5c06\u52a0\u5bc6\u6570\u7ec4\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\n        final BASE64Encoder encoder = new BASE64Encoder();\n        return encoder.encode(b);\n    }\n\n    \/**\n     * DESCBC\u89e3\u5bc6\n     *\n     * @param src\n     *            \u6570\u636e\u6e90\n     * @param key\n     *            \u5bc6\u94a5\uff0c\u957f\u5ea6\u5fc5\u987b\u662f8\u7684\u500d\u6570\n     * @return \u8fd4\u56de\u89e3\u5bc6\u540e\u7684\u539f\u59cb\u6570\u636e\n     * @throws Exception\n     *\/\n    public String decryptDESCBC(final String src, final String key) throws Exception {\n        \/\/ --\u901a\u8fc7base64\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6210byte\u6570\u7ec4\n        final BASE64Decoder decoder = new BASE64Decoder();\n        final byte[] bytesrc = decoder.decodeBuffer(src);\n\n        \/\/ --\u89e3\u5bc6\u7684key\n        final DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(&quot;UTF-8&quot;));\n        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(&quot;DES&quot;);\n        final SecretKey secretKey = keyFactory.generateSecret(desKeySpec);\n\n        \/\/ --\u5411\u91cf\n        final IvParameterSpec iv = new IvParameterSpec(IV.getBytes(&quot;UTF-8&quot;));\n\n        \/\/ --Chipher\u5bf9\u8c61\u89e3\u5bc6Cipher.getInstance(&quot;DES&quot;)\u5c31\u662f\u91c7\u7528ECB\u6a21\u5f0f\uff0ccipher.init(Cipher.DECRYPT_MODE, secretKey)\u5373\u53ef\n        final Cipher cipher = Cipher.getInstance(&quot;DES\/CBC\/PKCS5Padding&quot;);\n        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);\n        final byte[] retByte = cipher.doFinal(bytesrc);\n\n        return new String(retByte);\n\n    }\n\n    \/\/ 3DESECB\u52a0\u5bc6\uff0ckey\u5fc5\u987b\u662f\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e 3*8 = 24 \u4f4d\n    public String encryptThreeDESECB(final String src, final String key) throws Exception {\n        final DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(&quot;UTF-8&quot;));\n        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(&quot;DESede&quot;);\n        final SecretKey securekey = keyFactory.generateSecret(dks);\n\n        final Cipher cipher = Cipher.getInstance(&quot;DESede\/ECB\/PKCS5Padding&quot;);\n        cipher.init(Cipher.ENCRYPT_MODE, securekey);\n        final byte[] b = cipher.doFinal(src.getBytes());\n\n        final BASE64Encoder encoder = new BASE64Encoder();\n        return encoder.encode(b).replaceAll(&quot;\\r&quot;, &quot;&quot;).replaceAll(&quot;\\n&quot;, &quot;&quot;);\n    }\n\n    \/\/ 3DESECB\u89e3\u5bc6\uff0ckey\u5fc5\u987b\u662f\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e 3*8 = 24 \u4f4d\n    public String decryptThreeDESECB(final String src, final String key) throws Exception {\n        \/\/ --\u901a\u8fc7base64\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6210byte\u6570\u7ec4\n        final BASE64Decoder decoder = new BASE64Decoder();\n        final byte[] bytesrc = decoder.decodeBuffer(src);\n        \/\/ --\u89e3\u5bc6\u7684key\n        final DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(&quot;UTF-8&quot;));\n        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(&quot;DESede&quot;);\n        final SecretKey securekey = keyFactory.generateSecret(dks);\n\n        \/\/ --Chipher\u5bf9\u8c61\u89e3\u5bc6\n        final Cipher cipher = Cipher.getInstance(&quot;DESede\/ECB\/PKCS5Padding&quot;);\n        cipher.init(Cipher.DECRYPT_MODE, securekey);\n        final byte[] retByte = cipher.doFinal(bytesrc);\n\n        return new String(retByte);\n    }\n}<\/code><\/pre>\n<h3>ECB\u6a21\u5f0f\uff08\u65e0\u9700\u6307\u5b9aIV\u52a0\u5bc6\u5411\u91cf\uff09<\/h3>\n<pre><code class=\"language-java\">\/**\n * 3DES\u7684\u5bc6\u94a5\u5fc5\u987b\u662f24\u4f4d\u7684byte\u6570\u7ec4\n * \u968f\u4fbf\u62ff\u4e00\u4e2aString.getBytes()\u662f\u4e0d\u884c\u7684\uff0c\u4f1a\u62a5\u5982\u4e0b\u9519\u8bef\n * \u00a0\u00a0java.security.InvalidKeyException:\u00a0Invalid\u00a0key\u00a0length:\u00a059\u00a0bytes\n * \u00a0\u00a0\u89e3\u51b3\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u2460\u6309\u5bc6\u94a5\u56fa\u5b9a\u957f\u5ea6\u91cd\u65b0\u5b9a\u4e49\u5b57\u7b26\u4e32\uff1b\u2461\u5148\u628a\u5b57\u7b26\u4e32\u7528Base64\u6216\u8005MD5\u52a0\u5bc6\uff0c\u7136\u540e\u622a\u53d6\u56fa\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u8f6c\u6210byte\u6570\u7ec4\uff1b\u2462\u5b57\u7b26\u4e32\u8f6c\u6210Byte\u6570\u7ec4\uff0c\u9488\u5bf9\u8be5\u6570\u7ec4\u8fdb\u884c\u4fee\u6539\uff0c\u82e5\u957f\u5ea6\u8fc7\u957f\u5219\u53ea\u622a\u53d6\u4e00\u90e8\u5206\uff0c\u82e5\u957f\u5ea6\u4e0d\u591f\u5219\u8865\u96f6\n * \u00a0\n * \u52a0\u5bc6\u7ed3\u679c\u7684\u7f16\u7801\u65b9\u5f0f\u8981\u4e00\u81f4\n * \u4ecebyte\u6570\u7ec4\u8f6c\u6210\u5b57\u7b26\u4e32\uff0c\u4e00\u822c\u6709\u4e24\u79cd\u65b9\u5f0f\uff0cbase64\u5904\u7406\u548c\u5341\u516d\u8fdb\u5236\u5904\u7406\u3002\n *\/\npublic class DESedeHelper {\n    \/\/\u5b9a\u4e49\u52a0\u5bc6\u7b97\u6cd5\uff0c\u6709DES\u3001DESede(\u53733DES)\u3001Blowfish\n    private static final String Algorithm = &quot;DESede&quot;;\n    private static final String charset = &quot;UTF-8&quot;;\n\n    private final String PASSWORD_CRYPT_KEY;\n    private DESedeHelper(String key) {\n        this.PASSWORD_CRYPT_KEY = key;\n    }\n\n    public static DESedeHelper getInstance(String key) {\n        return new DESedeHelper(key);\n    }\n\n    \/**\n     * \u52a0\u5bc6\u65b9\u6cd5\n     *\n     * @param src \u6e90\u6570\u636e\u7684\u5b57\u8282\u6570\u7ec4\n     * @return\n     *\/\n    public String encryptMode(String src) {\n        try {\n            SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);    \/\/\u751f\u6210\u5bc6\u94a5\n            Cipher cipher = Cipher.getInstance(Algorithm);    \/\/\u5b9e\u4f8b\u5316\u8d1f\u8d23\u52a0\u5bc6\/\u89e3\u5bc6\u7684Cipher\u5de5\u5177\u7c7b\n            cipher.init(Cipher.ENCRYPT_MODE, deskey);    \/\/\u521d\u59cb\u5316\u4e3a\u52a0\u5bc6\u6a21\u5f0f\n            return Base64.encodeBase64String(cipher.doFinal(src.getBytes(charset)));\n        } catch (java.security.NoSuchAlgorithmException e1) {\n            e1.printStackTrace();\n        } catch (javax.crypto.NoSuchPaddingException e2) {\n            e2.printStackTrace();\n        } catch (Exception e3) {\n            e3.printStackTrace();\n        }\n        return null;\n    }\n\n    \/**\n     * \u89e3\u5bc6\u51fd\u6570\n     *\n     * @param src \u5bc6\u6587\u7684\u5b57\u8282\u6570\u7ec4\n     * @return\n     *\/\n    public String decryptMode(String src) {\n        try {\n            SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);\n            Cipher cipher = Cipher.getInstance(Algorithm);\n            cipher.init(Cipher.DECRYPT_MODE, deskey);    \/\/\u521d\u59cb\u5316\u4e3a\u89e3\u5bc6\u6a21\u5f0f\n\n            return new String(cipher.doFinal(Base64.decodeBase64(src)), charset);\n        } catch (java.security.NoSuchAlgorithmException e1) {\n            e1.printStackTrace();\n        } catch (javax.crypto.NoSuchPaddingException e2) {\n            e2.printStackTrace();\n        } catch (Exception e3) {\n            e3.printStackTrace();\n        }\n        return null;\n    }\n\n    \/*\n     * \u6839\u636e\u5b57\u7b26\u4e32\u751f\u6210\u5bc6\u94a5\u5b57\u8282\u6570\u7ec4\n     * @param keyStr \u5bc6\u94a5\u5b57\u7b26\u4e32\n     * @return\n     * @throws UnsupportedEncodingException\n     *\/\n    public byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException {\n        byte[] key = new byte[24];    \/\/\u58f0\u660e\u4e00\u4e2a\u4f4d\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u9ed8\u8ba4\u91cc\u9762\u90fd\u662f0\n        byte[] temp = keyStr.getBytes(charset);    \/\/\u5c06\u5b57\u7b26\u4e32\u8f6c\u6210\u5b57\u8282\u6570\u7ec4\n\n        \/*\n         * \u6267\u884c\u6570\u7ec4\u62f7\u8d1d\n         * System.arraycopy(\u6e90\u6570\u7ec4\uff0c\u4ece\u6e90\u6570\u7ec4\u54ea\u91cc\u5f00\u59cb\u62f7\u8d1d\uff0c\u76ee\u6807\u6570\u7ec4\uff0c\u62f7\u8d1d\u591a\u5c11\u4f4d)\n         *\/\n        if (key.length &gt; temp.length) {\n            \/\/\u5982\u679ctemp\u4e0d\u591f\u4f4d\uff0c\u5219\u62f7\u8d1dtemp\u6570\u7ec4\u6574\u4e2a\u957f\u5ea6\u7684\u5185\u5bb9\u5230key\u6570\u7ec4\u4e2d\n            System.arraycopy(temp, 0, key, 0, temp.length);\n        } else {\n            \/\/\u5982\u679ctemp\u5927\u4e8e\u4f4d\uff0c\u5219\u62f7\u8d1dtemp\u6570\u7ec4\u4e2a\u957f\u5ea6\u7684\u5185\u5bb9\u5230key\u6570\u7ec4\u4e2d\n            System.arraycopy(temp, 0, key, 0, key.length);\n        }\n        return key;\n    }\n}<\/code><\/pre>\n<h3>\u6d4b\u8bd5\u7c7b<\/h3>\n<pre><code class=\"language-java\">import java.net.URLEncoder;\nimport cn.appblog.util.ThreeDES;\n\npublic class ThreeDESTest {\n    public static void main(String[] args) throws Exception {\n        final String key = &quot;AppBlog.CN&quot;;\n        \/\/ \u52a0\u5bc6\u6d41\u7a0b\n        String homepage = &quot;http:\/\/www.appblog.cn&quot;;\n        ThreeDES threeDES = new ThreeDES();\n        String telePhone_encrypt = &quot;&quot;;\n        homepageEncrypt = threeDES.encryptThreeDESECB(URLEncoder.encode(telePhone, &quot;UTF-8&quot;), key);\n        System.out.println(homepageEncrypt);\n\n        \/\/ \u89e3\u5bc6\u6d41\u7a0b\n        String homepageDecrypt = threeDES.decryptThreeDESECB(telePhone_encrypt, key);\n        System.out.println(&quot;\u6a21\u62df\u4ee3\u7801\u89e3\u5bc6: &quot; + homepageDecrypt);\n    }\n}<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\uff081\uff093DES\u5bc6\u94a5\u7684\u957f\u5ea6\u5fc5\u987b\u662f8\u7684\u500d\u6570\uff0c\u53ef\u53d624\u4f4d\u621632\u4f4d\uff1b\uff082\uff09\u52a0\u5bc6\u7ed3\u679c\u7684byte\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4e00\u822c\u91c7\u7528\u4e24\u79cd\u65b9\u5f0f\uff1aBase64\u5904\u7406\u6216\u5341\u516d\u8fdb\u5236\u5904\u7406\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u52a0\u5bc6\u7b80\u4ecb \u5355\u5411\u52a0\u5bc6\uff1a\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u6458\u8981\u8ba1\u7b97\u751f\u6210\u5bc6\u6587\uff0c\u5bc6\u6587\u4e0d\u53ef\u9006\u63a8\u8fd8\u539f\u3002\u7b97\u6cd5\u4ee3\u8868\uff1aMD5\uff0cSHA \u53cc\u5411\u52a0\u5bc6\uff1a\u4e0e\u5355 [&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":[277],"class_list":["post-1383","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-3des"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1383","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=1383"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1383\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}