{"id":1447,"date":"2009-08-01T10:53:53","date_gmt":"2009-08-01T01:53:53","guid":{"rendered":"http:\/\/fukata.org\/?p=1447"},"modified":"2017-08-11T22:59:08","modified_gmt":"2017-08-11T22:59:08","slug":"java-util-des","status":"publish","type":"post","link":"https:\/\/blog.fukata.org\/archives\/1447\/","title":{"rendered":"Java\u306b\u3088\u308bDES\u306e\u6697\u53f7\u5316\u30fb\u5fa9\u53f7\u5316\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3"},"content":{"rendered":"
\u4eca\u65e5\u306f\u3001\u6697\u53f7\u5316\u30fb\u5fa9\u53f7\u5316\u306b\u95a2\u3059\u308b\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u3092\u6b8b\u3057\u3066\u304a\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u4f5c\u696d\u30ed\u30b0\u7684\u306a\u3082\u306e\u3067\u3002<\/p>\n
\r\npackage org.fukata.jc_common.utils;\r\n\r\nimport java.io.IOException;\r\nimport java.security.InvalidAlgorithmParameterException;\r\nimport java.security.InvalidKeyException;\r\nimport java.security.NoSuchAlgorithmException;\r\nimport java.security.spec.InvalidKeySpecException;\r\n\r\nimport javax.crypto.BadPaddingException;\r\nimport javax.crypto.Cipher;\r\nimport javax.crypto.IllegalBlockSizeException;\r\nimport javax.crypto.NoSuchPaddingException;\r\nimport javax.crypto.SecretKey;\r\nimport javax.crypto.SecretKeyFactory;\r\nimport javax.crypto.spec.DESKeySpec;\r\n\r\nimport sun.misc.BASE64Decoder;\r\nimport sun.misc.BASE64Encoder;\r\n\r\n\/**\r\n * \u6697\u53f7\u5316\u306b\u95a2\u3059\u308b\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\r\n * @author tatsuya\r\n *\r\n *\/\r\npublic class CryptUtils {\r\n\r\n\tprivate static BASE64Encoder base64Encoder = new BASE64Encoder();\r\n\tprivate static BASE64Decoder base64Decoder = new BASE64Decoder();\r\n\t\r\n\t\r\n\tprivate CryptUtils() {\r\n\t}\r\n\t\r\n\t\/**\r\n\t * \u6697\u53f7\u5316\u3059\u308b\u3002\r\n\t * @param decrypt \u6697\u53f7\u5143\u306e\u6587\u5b57\u5217\r\n\t * @return\r\n\t * @throws NoSuchAlgorithmException \r\n\t * @throws NoSuchPaddingException \r\n\t * @throws InvalidKeyException \r\n\t * @throws BadPaddingException \r\n\t * @throws IllegalBlockSizeException \r\n\t * @throws InvalidKeySpecException \r\n\t *\/\r\n\tpublic static String encrypt(String decrypt) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {\r\n\t\t\/\/ \u6697\u53f7\u5316\r\n Cipher c = Cipher.getInstance(\"DES\");\r\n c.init(Cipher.ENCRYPT_MODE, generateSecretKey());\r\n byte input[] = decrypt.getBytes();\r\n byte encrypted[] = c.doFinal(input); \r\n \r\n\t\treturn base64Encoder.encode(encrypted);\r\n\t}\r\n\t\r\n\t\/**\r\n\t * \u5fa9\u53f7\u5316\u3059\u308b\u3002\r\n\t * @param encrypt \u6697\u53f7\u5316\u3055\u308c\u305f\u6587\u5b57\u5217\r\n\t * @return\r\n\t * @throws BadPaddingException \r\n\t * @throws IllegalBlockSizeException \r\n\t * @throws NoSuchAlgorithmException \r\n\t * @throws NoSuchPaddingException \r\n\t * @throws InvalidAlgorithmParameterException \r\n\t * @throws InvalidKeyException \r\n\t * @throws InvalidKeySpecException \r\n\t * @throws IOException \r\n\t *\/\r\n\tpublic static String decrypt(String encrypt) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException, IOException {\r\n\t\t\r\n\t\tCipher c = Cipher.getInstance(\"DES\");\r\n c.init(Cipher.DECRYPT_MODE, generateSecretKey());\r\n byte output[] = c.doFinal(base64Decoder.decodeBuffer(encrypt));\r\n\r\n return new String(output);\r\n\t}\r\n\r\n\t\/**\r\n\t * \u9375\u3092\u751f\u6210\u3059\u308b\u3002\r\n\t * @return\r\n\t * @throws InvalidKeyException\r\n\t * @throws NoSuchAlgorithmException\r\n\t * @throws InvalidKeySpecException\r\n\t *\/\r\n\tprivate static SecretKey generateSecretKey() throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {\r\n DESKeySpec dk = new DESKeySpec(Config.ENCRYPT_KEY.getBytes());\r\n SecretKeyFactory kf = SecretKeyFactory.getInstance(\"DES\");\r\n SecretKey sk = kf.generateSecret(dk);\r\n return sk;\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"\u4eca\u65e5\u306f\u3001\u6697\u53f7\u5316\u30fb\u5fa9\u53f7\u5316\u306b\u95a2\u3059\u308b\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u3092\u6b8b\u3057\u3066\u304a\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u4f5c\u696d\u30ed\u30b0\u7684\u306a\u3082\u306e\u3067\u3002 package org.fukata.jc_common.utils; import java.io.IO … \u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":9223372036854775807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1980],"tags":[361,360,359],"_links":{"self":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/1447"}],"collection":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/comments?post=1447"}],"version-history":[{"count":0,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/1447\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media\/9223372036854775807"}],"wp:attachment":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media?parent=1447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/categories?post=1447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/tags?post=1447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}