1 package hayashi.yuu.tools.properties;
\r
3 import java.security.InvalidAlgorithmParameterException;
\r
4 import java.security.InvalidKeyException;
\r
5 import java.security.NoSuchAlgorithmException;
\r
6 import java.security.spec.InvalidKeySpecException;
\r
8 import javax.crypto.BadPaddingException;
\r
9 import javax.crypto.Cipher;
\r
10 import javax.crypto.IllegalBlockSizeException;
\r
11 import javax.crypto.NoSuchPaddingException;
\r
12 import javax.crypto.SecretKey;
\r
13 import javax.crypto.SecretKeyFactory;
\r
14 import javax.crypto.spec.PBEKeySpec;
\r
15 import javax.crypto.spec.PBEParameterSpec;
\r
19 * @author Hayashi,Yuu
\r
20 * @version 2010/02/07
\r
23 public class Encrypt
\r
26 * @param args コマンドパラメータ
\r
28 public static void main(String[] args) {
\r
29 String source = "deister07";
\r
30 System.out.println("source \"" + source + "\"");
\r
32 Encrypt.PASSWORD_KEY = "hayashihimitukagi";
\r
35 String encStr = encrypt(source);
\r
36 System.out.println("encStr \"" + encStr + "\"");
\r
39 String decStr = decode(encStr);
\r
40 System.out.println("decodeStr \"" + decStr + "\"");
\r
42 catch (Exception e) {
\r
43 e.printStackTrace();
\r
48 private static final byte[] SALT = {(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99};
\r
49 private static final int NUM = 128; // 繰り返し回数
\r
53 * 心配性の人は、この値を変えてください。
\r
55 public static String PASSWORD_KEY = "himitukagi"; // 秘密鍵
\r
59 * @param source 暗号化したい文字列
\r
61 * @throws NoSuchAlgorithmException 例外
\r
62 * @throws NoSuchPaddingException 例外
\r
63 * @throws InvalidKeyException 例外
\r
64 * @throws BadPaddingException 例外
\r
65 * @throws IllegalBlockSizeException 例外
\r
66 * @throws InvalidKeySpecException 例外
\r
67 * @throws InvalidAlgorithmParameterException 例外
\r
69 public static String encrypt(String source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {
\r
71 return toHexString(encrypt(source.getBytes()));
\r
76 * @param source 暗号化したいバイト配列
\r
77 * @return 暗号化されたバイト配列
\r
78 * @throws NoSuchAlgorithmException 例外
\r
79 * @throws NoSuchPaddingException 例外
\r
80 * @throws InvalidKeyException 例外
\r
81 * @throws IllegalBlockSizeException 例外
\r
82 * @throws BadPaddingException 例外
\r
83 * @throws InvalidKeySpecException 例外
\r
84 * @throws InvalidAlgorithmParameterException 例外
\r
86 public static byte[] encrypt(byte[] source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {
\r
87 PBEParameterSpec pbeParam = new PBEParameterSpec(SALT, NUM);
\r
89 char[] pass = PASSWORD_KEY.toCharArray();
\r
90 PBEKeySpec pbeKey = new PBEKeySpec(pass);
\r
91 SecretKeyFactory secKeys = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
\r
92 SecretKey secKey = secKeys.generateSecret(pbeKey);
\r
94 Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
\r
95 cipher.init(Cipher.ENCRYPT_MODE, secKey, pbeParam);
\r
98 return cipher.doFinal(source);
\r
103 * @param source 暗号化された文字列
\r
105 * @throws NoSuchAlgorithmException 例外
\r
106 * @throws InvalidKeySpecException 例外
\r
107 * @throws NoSuchPaddingException 例外
\r
108 * @throws InvalidAlgorithmParameterException 例外
\r
109 * @throws InvalidKeyException 例外
\r
110 * @throws BadPaddingException 例外
\r
111 * @throws IllegalBlockSizeException 例外
\r
113 public static String decode(String source) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
\r
114 if (source == null) {
\r
117 byte[] bytepass = toHexBin(source);
\r
118 return new String(decode(bytepass));
\r
123 * @param source 暗号化されたバイト配列
\r
124 * @return 復号されたバイト配列
\r
125 * @throws NoSuchAlgorithmException 例外
\r
126 * @throws InvalidKeySpecException 例外
\r
127 * @throws NoSuchPaddingException 例外
\r
128 * @throws InvalidKeyException 例外
\r
129 * @throws InvalidAlgorithmParameterException 例外
\r
130 * @throws IllegalBlockSizeException 例外
\r
131 * @throws BadPaddingException 例外
\r
133 public static byte[] decode(byte[] source) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
\r
135 PBEParameterSpec pbeParamSpecDec = new PBEParameterSpec(SALT, NUM);
\r
137 PBEKeySpec pbeKeySpecDec = new PBEKeySpec(PASSWORD_KEY.toCharArray());
\r
138 SecretKeyFactory keyFacDec = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
\r
139 SecretKey pbeKeyDec = keyFacDec.generateSecret(pbeKeySpecDec);
\r
141 Cipher cDec = Cipher.getInstance("PBEWithMD5AndDES");
\r
142 cDec.init(Cipher.DECRYPT_MODE, pbeKeyDec, pbeParamSpecDec);
\r
144 return cDec.doFinal(source);
\r
151 * @param bs バイト配列(バイナリ)
\r
154 private static String toHexString(byte[] binStr) {
\r
155 StringBuffer buffer = new StringBuffer(binStr.length * 2);
\r
156 for (int i = 0; i < binStr.length; i++) {
\r
157 if ((binStr[i] >= 0) && (binStr[i] < 0x10)) {
\r
158 buffer.append('0');
\r
160 buffer.append(Integer.toHexString(0xff & binStr[i]));
\r
162 return buffer.toString();
\r
166 * バイナリ文字列をバイナリ配列に変換する。
\r
168 * @param bs 16進表現文字列
\r
171 private static byte[] toHexBin(String binStr) {
\r
172 byte[] bin = new byte[binStr.length() / 2];
\r
174 StringBuffer sbuf = new StringBuffer(binStr);
\r
175 for (int i = 0; i < bin.length; i++) {
\r
176 String s = sbuf.substring(i*2, i*2+2).toUpperCase();
\r
177 bin[i] = (byte)Integer.parseInt(s, 16);
\r