{"id":4070,"date":"2018-08-17T21:32:35","date_gmt":"2018-08-17T13:32:35","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4070"},"modified":"2018-08-17T21:32:35","modified_gmt":"2018-08-17T13:32:35","slug":"%e7%94%a8java%e8%bf%9b%e8%a1%8caes%e5%8a%a0%e8%a7%a3%e5%af%86","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4070.html","title":{"rendered":"\u7528Java\u8fdb\u884cAES\u52a0\u89e3\u5bc6"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u6574\u7406\u8bb0\u5f55\u4e00\u4e0b\u7528Java\u8fdb\u884cAES\u52a0\u89e3\u5bc6\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u65b9\u4fbf\u4ee5\u540e\u5feb\u901f\u68c0\u7d22\u548c\u4f7f\u7528\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>\u5e38\u89c4\u7684AES\u52a0\u89e3\u5bc6\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.utils;\r\n\r\nimport org.apache.commons.codec.binary.Base64;\r\n\r\nimport javax.crypto.Cipher;\r\nimport javax.crypto.spec.IvParameterSpec;\r\nimport javax.crypto.spec.SecretKeySpec;\r\n\r\npublic class DoCipherTest {\r\n\r\n    public static String encrypt(String key, String initVector, String value) {\r\n        try {\r\n            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(\"UTF-8\"));\r\n            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(\"UTF-8\"), \"AES\");\r\n\r\n            Cipher cipher = Cipher.getInstance(\"AES\/CBC\/PKCS5PADDING\");\r\n            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);\r\n\r\n            byte[] encrypted = cipher.doFinal(value.getBytes());\r\n            System.out.println(\"encrypted string: \" + Base64.encodeBase64String(encrypted));\r\n\r\n            return Base64.encodeBase64String(encrypted);\r\n        } catch (Exception ex) {\r\n            ex.printStackTrace();\r\n        }\r\n\r\n        return null;\r\n    }\r\n\r\n    public static String decrypt(String key, String initVector, String encrypted) {\r\n        try {\r\n            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(\"UTF-8\"));\r\n            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(\"UTF-8\"), \"AES\");\r\n\r\n            Cipher cipher = Cipher.getInstance(\"AES\/CBC\/PKCS5PADDING\");\r\n            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);\r\n\r\n            byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));\r\n\r\n            return new String(original);\r\n        } catch (Exception ex) {\r\n            ex.printStackTrace();\r\n        }\r\n\r\n        return null;\r\n    }\r\n\r\n    public static void main(String[] args) {\r\n        String key = \"Bar12345Bar12345\"; \/\/ 128 bit key (16 chars)\r\n        String initVector = \"RandomInitVector\"; \/\/ 16 bytes IV (16 chars)\r\n\r\n        System.out.println(decrypt(key, initVector,\r\n                encrypt(key, initVector, \"Hello World\")));\r\n    }\r\n}<\/pre>\n<p>\u5728\u591a\u7ebf\u7a0b\u9879\u76ee\u4e2d\u4f7f\u7528ThreadLocal\u8fdb\u884c\u4f18\u5316\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.utils;\r\n\r\nimport org.apache.commons.codec.binary.Base64;\r\n\r\nimport javax.crypto.Cipher;\r\nimport javax.crypto.spec.IvParameterSpec;\r\nimport javax.crypto.spec.SecretKeySpec;\r\n\r\n\/**\r\n * Created by ixyzero on 2018\/8\/16.\r\n *\/\r\npublic class ThreadLocalCipher {\r\n\r\n    private static String sKey=\"Bar12345Bar12345\";\r\n    private static String ivParameter=\"RandomInitVector\";\r\n\r\n    private static final ThreadLocal&lt;Cipher&gt; encryptCiphers = new ThreadLocal&lt;Cipher&gt;() {\r\n        @Override\r\n        protected Cipher initialValue() {\r\n            try {\r\n                byte[] raw = sKey.getBytes(\"ASCII\");\r\n                SecretKeySpec skeySpec = new SecretKeySpec(raw, \"AES\");\r\n                IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());\r\n\r\n                Cipher cipher = Cipher.getInstance(\"AES\/CBC\/PKCS5Padding\");\r\n                cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);\r\n                return cipher;\r\n            } catch (Exception e) {\r\n                \/\/ ugly but necessary\r\n                throw new RuntimeException(e);\r\n            }\r\n        }\r\n    };\r\n\r\n    private static final ThreadLocal&lt;Cipher&gt; decryptCiphers = new ThreadLocal&lt;Cipher&gt;() {\r\n        @Override\r\n        protected Cipher initialValue() {\r\n            try {\r\n                byte[] raw = sKey.getBytes(\"ASCII\");\r\n                SecretKeySpec skeySpec = new SecretKeySpec(raw, \"AES\");\r\n                IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());\r\n\r\n                Cipher cipher = Cipher.getInstance(\"AES\/CBC\/PKCS5Padding\");\r\n                cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);\r\n                return cipher;\r\n            } catch (Exception e) {\r\n                \/\/ ugly but necessary\r\n                throw new RuntimeException(e);\r\n            }\r\n        }\r\n    };\r\n\r\n    public static String encrypt(String data) {\r\n        try {\r\n            Cipher cipher = encryptCiphers.get();\r\n            byte[] encrypted = cipher.doFinal(data.getBytes());\r\n            \r\n            System.out.println(\"encrypted string: \" + Base64.encodeBase64String(encrypted));\r\n            return Base64.encodeBase64String(encrypted);\r\n        } catch (Exception ex) {\r\n            ex.printStackTrace();\r\n        }\r\n\r\n        return null;\r\n    }\r\n\r\n    public static String decrypt(String encrypted) {\r\n        try {\r\n            Cipher cipher = decryptCiphers.get();\r\n            byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));\r\n            return new String(original);\r\n        } catch (Exception ex) {\r\n            ex.printStackTrace();\r\n        }\r\n\r\n        return null;\r\n    }\r\n\r\n    public static void main(String[] args) {\r\n        String sourceData = \"Hello World\";\r\n        String beEncrypted = encrypt(sourceData);\r\n        String beDecrypted = decrypt(beEncrypted);\r\n\r\n        System.out.println(beEncrypted);\r\n\r\n        System.out.println(beDecrypted);\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/15554296\/simple-java-aes-encrypt-decrypt-example\">\u7528Java\u8fdb\u884cAES\u52a0\u89e3\u5bc6<\/a>#nice<\/li>\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/javax\/crypto\/Cipher.html\">Cipher (Java Platform SE 7 )<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/u011781521\/article\/details\/77932321\">JAVA AES\u52a0\u5bc6\u4e0e\u89e3\u5bc6<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/9496447\/encryption-of-video-files\/18892960#18892960\">\u52a0\u5bc6\u89c6\u9891\u6587\u4ef6<\/a><\/li>\n<li>==<\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/11942466\/create-and-maintain-multiple-ciphers\">\u521b\u5efa\u548c\u4fdd\u6301\u591a\u4e2aCipher<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/6957406\/is-cipher-thread-safe\">javax.crypto.Cipher \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u4e48\uff1f<\/a><\/li>\n<li><a href=\"https:\/\/blog.poxiao.me\/p\/advanced-encryption-standard-and-block-cipher-mode\/\">\u9ad8\u7ea7\u52a0\u5bc6\u6807\u51c6AES\u7684\u5de5\u4f5c\u6a21\u5f0f\uff08ECB\u3001CBC\u3001CFB\u3001OFB\uff09<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6574\u7406\u8bb0\u5f55\u4e00\u4e0b\u7528Java\u8fdb\u884cAES\u52a0\u89e3\u5bc6\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u65b9\u4fbf\u4ee5\u540e\u5feb\u901f\u68c0\u7d22\u548c\u4f7f\u7528\u3002 \u6b63\u6587\uff1a \u53c2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[527,1204,61,1203,1193],"class_list":["post-4070","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-aes","tag-cipher","tag-java","tag-thread-safe","tag-threadlocal"],"views":7800,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4070"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4070\/revisions"}],"predecessor-version":[{"id":4071,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4070\/revisions\/4071"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}