-package hayashi.yuu.tools.properties;
-
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.security.spec.InvalidKeySpecException;
-
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
-import javax.crypto.SecretKey;
-import javax.crypto.SecretKeyFactory;
-import javax.crypto.spec.PBEKeySpec;
-import javax.crypto.spec.PBEParameterSpec;
-
-/**
- * 暗号化モドキ
- * @author Hayashi,Yuu
- * @version 2010/02/07
- * @since 2009/01/27
- */
-public class Encrypt
-{
- /**
- * @param args
- */
- public static void main(String[] args) {
- String source = "deister07";
- System.out.println("source \"" + source + "\"");
- try {
- Encrypt.PASSWORD_KEY = "hayashihimitukagi";
-
- // 暗号化
- String encStr = encrypt(source);
- System.out.println("encStr \"" + encStr + "\"");
-
- // 復号
- String decStr = decode(encStr);
- System.out.println("decodeStr \"" + decStr + "\"");
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- // ソルト
- private static final byte[] SALT = {(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99};
- private static final int NUM = 128; // 繰り返し回数
-
- /**
- * 暗号化のための秘密鍵
- * 心配性の人は、この値を変えてください。
- */
- public static String PASSWORD_KEY = "himitukagi"; // 秘密鍵
-
- /**
- * 指定された文字列を暗号化する。
- * @param source 暗号化したい文字列
- * @return 暗号化された文字列
- * @throws NoSuchAlgorithmException
- * @throws NoSuchPaddingException
- * @throws InvalidKeyException
- * @throws BadPaddingException
- * @throws IllegalBlockSizeException
- * @throws InvalidKeySpecException
- * @throws InvalidAlgorithmParameterException
- */
- public static String encrypt(String source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {
- //16進数化
- return toHexString(encrypt(source.getBytes()));
- }
-
- /**
- * 指定されたバイト配列を暗号化する。
- * @param source 暗号化したいバイト配列
- * @return 暗号化されたバイト配列
- * @throws NoSuchAlgorithmException
- * @throws NoSuchPaddingException
- * @throws InvalidKeyException
- * @throws IllegalBlockSizeException
- * @throws BadPaddingException
- * @throws InvalidKeySpecException
- * @throws InvalidAlgorithmParameterException
- */
- public static byte[] encrypt(byte[] source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {
- PBEParameterSpec pbeParam = new PBEParameterSpec(SALT, NUM);
-
- char[] pass = PASSWORD_KEY.toCharArray();
- PBEKeySpec pbeKey = new PBEKeySpec(pass);
- SecretKeyFactory secKeys = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
- SecretKey secKey = secKeys.generateSecret(pbeKey);
-
- Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
- cipher.init(Cipher.ENCRYPT_MODE, secKey, pbeParam);
-
- //暗号化
- return cipher.doFinal(source);
- }
-
- /**
- * 復号
- * @param source 暗号化された文字列
- * @return 復号された文字列
- * @throws NoSuchAlgorithmException
- * @throws InvalidKeySpecException
- * @throws NoSuchPaddingException
- * @throws InvalidAlgorithmParameterException
- * @throws InvalidKeyException
- * @throws BadPaddingException
- * @throws IllegalBlockSizeException
- */
- public static String decode(String source) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
- if (source == null) {
- return null;
- }
- byte[] bytepass = toHexBin(source);
- return new String(decode(bytepass));
- }
-
- /**
- * 復号
- * @param source 暗号化されたバイト配列
- * @return 復号されたバイト配列
- * @throws NoSuchAlgorithmException
- * @throws InvalidKeySpecException
- * @throws NoSuchPaddingException
- * @throws InvalidKeyException
- * @throws InvalidAlgorithmParameterException
- * @throws IllegalBlockSizeException
- * @throws BadPaddingException
- */
- public static byte[] decode(byte[] source) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
- // PBE を新たに生成
- PBEParameterSpec pbeParamSpecDec = new PBEParameterSpec(SALT, NUM);
-
- PBEKeySpec pbeKeySpecDec = new PBEKeySpec(PASSWORD_KEY.toCharArray());
- SecretKeyFactory keyFacDec = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
- SecretKey pbeKeyDec = keyFacDec.generateSecret(pbeKeySpecDec);
-
- Cipher cDec = Cipher.getInstance("PBEWithMD5AndDES");
- cDec.init(Cipher.DECRYPT_MODE, pbeKeyDec, pbeParamSpecDec);
-
- return cDec.doFinal(source);
- }
-
-
- /**
- * バイナリ配列を文字列に変換する。
- *
- * @param bs バイト配列(バイナリ)
- * @return 16進表現文字列
- */
- private static String toHexString(byte[] binStr) {
- StringBuffer buffer = new StringBuffer(binStr.length * 2);
- for (int i = 0; i < binStr.length; i++) {
- if ((binStr[i] >= 0) && (binStr[i] < 0x10)) {
- buffer.append('0');
- }
- buffer.append(Integer.toHexString(0xff & binStr[i]));
- }
- return buffer.toString();
- }
-
- /**
- * バイナリ文字列をバイナリ配列に変換する。
- *
- * @param bs 16進表現文字列
- * @return バイナリ配列
- */
- private static byte[] toHexBin(String binStr) {
- byte[] bin = new byte[binStr.length() / 2];
-
- StringBuffer sbuf = new StringBuffer(binStr);
- for (int i = 0; i < bin.length; i++) {
- String s = sbuf.substring(i*2, i*2+2).toUpperCase();
- bin[i] = (byte)Integer.parseInt(s, 16);
- }
- return bin;
- }
-}
+package hayashi.yuu.tools.properties;\r
+\r
+import java.security.InvalidAlgorithmParameterException;\r
+import java.security.InvalidKeyException;\r
+import java.security.NoSuchAlgorithmException;\r
+import java.security.spec.InvalidKeySpecException;\r
+\r
+import javax.crypto.BadPaddingException;\r
+import javax.crypto.Cipher;\r
+import javax.crypto.IllegalBlockSizeException;\r
+import javax.crypto.NoSuchPaddingException;\r
+import javax.crypto.SecretKey;\r
+import javax.crypto.SecretKeyFactory;\r
+import javax.crypto.spec.PBEKeySpec;\r
+import javax.crypto.spec.PBEParameterSpec;\r
+\r
+/**\r
+ * 暗号化モドキ\r
+ * @author Hayashi,Yuu\r
+ * @version 2010/02/07\r
+ * @since 2009/01/27\r
+ */\r
+public class Encrypt\r
+{\r
+ /**\r
+ * @param args\r
+ */\r
+ public static void main(String[] args) {\r
+ String source = "deister07";\r
+ System.out.println("source \"" + source + "\"");\r
+ try {\r
+ Encrypt.PASSWORD_KEY = "hayashihimitukagi";\r
+ \r
+ // 暗号化\r
+ String encStr = encrypt(source);\r
+ System.out.println("encStr \"" + encStr + "\"");\r
+\r
+ // 復号\r
+ String decStr = decode(encStr);\r
+ System.out.println("decodeStr \"" + decStr + "\"");\r
+ }\r
+ catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ // ソルト\r
+ private static final byte[] SALT = {(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99};\r
+ private static final int NUM = 128; // 繰り返し回数\r
+ \r
+ /**\r
+ * 暗号化のための秘密鍵\r
+ * 心配性の人は、この値を変えてください。\r
+ */\r
+ public static String PASSWORD_KEY = "himitukagi"; // 秘密鍵\r
+\r
+ /**\r
+ * 指定された文字列を暗号化する。\r
+ * @param source 暗号化したい文字列\r
+ * @return 暗号化された文字列\r
+ * @throws NoSuchAlgorithmException \r
+ * @throws NoSuchPaddingException\r
+ * @throws InvalidKeyException \r
+ * @throws BadPaddingException \r
+ * @throws IllegalBlockSizeException \r
+ * @throws InvalidKeySpecException \r
+ * @throws InvalidAlgorithmParameterException \r
+ */\r
+ public static String encrypt(String source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {\r
+ //16進数化\r
+ return toHexString(encrypt(source.getBytes()));\r
+ }\r
+ \r
+ /**\r
+ * 指定されたバイト配列を暗号化する。\r
+ * @param source 暗号化したいバイト配列\r
+ * @return 暗号化されたバイト配列\r
+ * @throws NoSuchAlgorithmException\r
+ * @throws NoSuchPaddingException\r
+ * @throws InvalidKeyException\r
+ * @throws IllegalBlockSizeException\r
+ * @throws BadPaddingException\r
+ * @throws InvalidKeySpecException\r
+ * @throws InvalidAlgorithmParameterException\r
+ */\r
+ public static byte[] encrypt(byte[] source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {\r
+ PBEParameterSpec pbeParam = new PBEParameterSpec(SALT, NUM);\r
+ \r
+ char[] pass = PASSWORD_KEY.toCharArray();\r
+ PBEKeySpec pbeKey = new PBEKeySpec(pass);\r
+ SecretKeyFactory secKeys = SecretKeyFactory.getInstance("PBEWithMD5AndDES");\r
+ SecretKey secKey = secKeys.generateSecret(pbeKey);\r
+ \r
+ Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");\r
+ cipher.init(Cipher.ENCRYPT_MODE, secKey, pbeParam);\r
+ \r
+ //暗号化\r
+ return cipher.doFinal(source);\r
+ }\r
+ \r
+ /**\r
+ * 復号\r
+ * @param source 暗号化された文字列\r
+ * @return 復号された文字列\r
+ * @throws NoSuchAlgorithmException \r
+ * @throws InvalidKeySpecException \r
+ * @throws NoSuchPaddingException \r
+ * @throws InvalidAlgorithmParameterException\r
+ * @throws InvalidKeyException \r
+ * @throws BadPaddingException\r
+ * @throws IllegalBlockSizeException \r
+ */\r
+ public static String decode(String source) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {\r
+ if (source == null) {\r
+ return null;\r
+ }\r
+ byte[] bytepass = toHexBin(source);\r
+ return new String(decode(bytepass));\r
+ }\r
+\r
+ /**\r
+ * 復号\r
+ * @param source 暗号化されたバイト配列\r
+ * @return 復号されたバイト配列\r
+ * @throws NoSuchAlgorithmException\r
+ * @throws InvalidKeySpecException\r
+ * @throws NoSuchPaddingException\r
+ * @throws InvalidKeyException\r
+ * @throws InvalidAlgorithmParameterException\r
+ * @throws IllegalBlockSizeException\r
+ * @throws BadPaddingException\r
+ */\r
+ public static byte[] decode(byte[] source) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {\r
+ // PBE を新たに生成\r
+ PBEParameterSpec pbeParamSpecDec = new PBEParameterSpec(SALT, NUM);\r
+ \r
+ PBEKeySpec pbeKeySpecDec = new PBEKeySpec(PASSWORD_KEY.toCharArray());\r
+ SecretKeyFactory keyFacDec = SecretKeyFactory.getInstance("PBEWithMD5AndDES");\r
+ SecretKey pbeKeyDec = keyFacDec.generateSecret(pbeKeySpecDec);\r
+\r
+ Cipher cDec = Cipher.getInstance("PBEWithMD5AndDES");\r
+ cDec.init(Cipher.DECRYPT_MODE, pbeKeyDec, pbeParamSpecDec);\r
+ \r
+ return cDec.doFinal(source);\r
+ }\r
+\r
+\r
+ /**\r
+ * バイナリ配列を文字列に変換する。\r
+ * \r
+ * @param bs バイト配列(バイナリ)\r
+ * @return 16進表現文字列\r
+ */\r
+ private static String toHexString(byte[] binStr) {\r
+ StringBuffer buffer = new StringBuffer(binStr.length * 2);\r
+ for (int i = 0; i < binStr.length; i++) {\r
+ if ((binStr[i] >= 0) && (binStr[i] < 0x10)) {\r
+ buffer.append('0');\r
+ }\r
+ buffer.append(Integer.toHexString(0xff & binStr[i]));\r
+ }\r
+ return buffer.toString();\r
+ }\r
+\r
+ /**\r
+ * バイナリ文字列をバイナリ配列に変換する。\r
+ * \r
+ * @param bs 16進表現文字列\r
+ * @return バイナリ配列\r
+ */\r
+ private static byte[] toHexBin(String binStr) {\r
+ byte[] bin = new byte[binStr.length() / 2];\r
+ \r
+ StringBuffer sbuf = new StringBuffer(binStr);\r
+ for (int i = 0; i < bin.length; i++) {\r
+ String s = sbuf.substring(i*2, i*2+2).toUpperCase();\r
+ bin[i] = (byte)Integer.parseInt(s, 16);\r
+ }\r
+ return bin;\r
+ }\r
+}\r