OSDN Git Service

51ca7a44d25b04dfac386da949fe5dbb78d55467
[hayashilib/hayashi.git] / src / hayashi / yuu / tools / properties / Encrypt.java
1 package hayashi.yuu.tools.properties;
2
3 import java.security.InvalidAlgorithmParameterException;
4 import java.security.InvalidKeyException;
5 import java.security.NoSuchAlgorithmException;
6 import java.security.spec.InvalidKeySpecException;
7
8 import javax.crypto.BadPaddingException;
9 import javax.crypto.Cipher;
10 import javax.crypto.IllegalBlockSizeException;
11 import javax.crypto.NoSuchPaddingException;
12 import javax.crypto.SecretKey;
13 import javax.crypto.SecretKeyFactory;
14 import javax.crypto.spec.PBEKeySpec;
15 import javax.crypto.spec.PBEParameterSpec;
16
17 /**
18  * 暗号化モドキ
19  * @author Hayashi,Yuu
20  * @version 2010/02/07
21  * @since 2009/01/27
22  */
23 public class Encrypt
24 {
25         /**
26          * @param args
27          */
28     public static void main(String[] args) {
29         String source = "deister07";
30         System.out.println("source \"" + source + "\"");
31         try {
32                 Encrypt.PASSWORD_KEY = "hayashihimitukagi";
33                 
34             // 暗号化
35                 String encStr = encrypt(source);
36             System.out.println("encStr \"" + encStr + "\"");
37
38             // 復号
39             String decStr = decode(encStr);
40             System.out.println("decodeStr \"" + decStr + "\"");
41         }
42         catch (Exception e) {
43             e.printStackTrace();
44         }
45     }
46     
47         // ソルト
48         private static final byte[] SALT = {(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99};
49         private static final int NUM = 128;             // 繰り返し回数
50         
51         /**
52          * 暗号化のための秘密鍵
53          * 心配性の人は、この値を変えてください。
54          */
55         public static String PASSWORD_KEY = "himitukagi";               // 秘密鍵
56
57         /**
58      * 指定された文字列を暗号化する。
59      * @param source    暗号化したい文字列
60      * @return  暗号化された文字列
61      * @throws NoSuchAlgorithmException 
62      * @throws NoSuchPaddingException
63      * @throws InvalidKeyException 
64      * @throws BadPaddingException 
65      * @throws IllegalBlockSizeException 
66          * @throws InvalidKeySpecException 
67          * @throws InvalidAlgorithmParameterException 
68      */
69     public static String encrypt(String source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {
70                 //16進数化
71                 return toHexString(encrypt(source.getBytes()));
72     }
73     
74     /**
75      * 指定されたバイト配列を暗号化する。
76      * @param source    暗号化したいバイト配列
77      * @return  暗号化されたバイト配列
78      * @throws NoSuchAlgorithmException
79      * @throws NoSuchPaddingException
80      * @throws InvalidKeyException
81      * @throws IllegalBlockSizeException
82      * @throws BadPaddingException
83      * @throws InvalidKeySpecException
84      * @throws InvalidAlgorithmParameterException
85      */
86     public static byte[] encrypt(byte[] source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {
87                 PBEParameterSpec pbeParam = new PBEParameterSpec(SALT, NUM);
88                     
89                 char[] pass = PASSWORD_KEY.toCharArray();
90                 PBEKeySpec pbeKey = new PBEKeySpec(pass);
91                 SecretKeyFactory secKeys = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
92                 SecretKey secKey = secKeys.generateSecret(pbeKey);
93                 
94                 Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
95                 cipher.init(Cipher.ENCRYPT_MODE, secKey, pbeParam);
96                 
97                 //暗号化
98                 return cipher.doFinal(source);
99     }
100     
101     /**
102      * 復号
103      * @param source    暗号化された文字列
104      * @return  復号された文字列
105      * @throws NoSuchAlgorithmException 
106      * @throws InvalidKeySpecException 
107      * @throws NoSuchPaddingException 
108      * @throws InvalidAlgorithmParameterException
109      * @throws InvalidKeyException 
110      * @throws BadPaddingException
111      * @throws IllegalBlockSizeException 
112      */
113     public static String decode(String source) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
114         if (source == null) {
115                 return null;
116         }
117                 byte[] bytepass = toHexBin(source);
118                 return new String(decode(bytepass));
119     }
120
121     /**
122      * 復号
123      * @param source    暗号化されたバイト配列
124      * @return  復号されたバイト配列
125      * @throws NoSuchAlgorithmException
126      * @throws InvalidKeySpecException
127      * @throws NoSuchPaddingException
128      * @throws InvalidKeyException
129      * @throws InvalidAlgorithmParameterException
130      * @throws IllegalBlockSizeException
131      * @throws BadPaddingException
132      */
133     public static byte[] decode(byte[] source) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
134                 // PBE を新たに生成
135         PBEParameterSpec pbeParamSpecDec = new PBEParameterSpec(SALT, NUM);
136         
137         PBEKeySpec pbeKeySpecDec = new PBEKeySpec(PASSWORD_KEY.toCharArray());
138         SecretKeyFactory keyFacDec = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
139         SecretKey pbeKeyDec = keyFacDec.generateSecret(pbeKeySpecDec);
140
141         Cipher cDec = Cipher.getInstance("PBEWithMD5AndDES");
142         cDec.init(Cipher.DECRYPT_MODE, pbeKeyDec, pbeParamSpecDec);
143         
144         return cDec.doFinal(source);
145     }
146
147
148     /**
149      * バイナリ配列を文字列に変換する。
150      * 
151      * @param bs        バイト配列(バイナリ)
152      * @return  16進表現文字列
153      */
154         private static String toHexString(byte[] binStr) {
155                 StringBuffer buffer = new StringBuffer(binStr.length * 2);
156                 for (int i = 0; i < binStr.length; i++) {
157                         if ((binStr[i] >= 0) && (binStr[i] < 0x10)) {
158                                 buffer.append('0');
159                         }
160                         buffer.append(Integer.toHexString(0xff & binStr[i]));
161                 }
162                 return buffer.toString();
163         }
164
165     /**
166      * バイナリ文字列をバイナリ配列に変換する。
167      * 
168      * @param bs        16進表現文字列
169      * @return  バイナリ配列
170      */
171         private static byte[] toHexBin(String binStr) {
172                 byte[] bin = new byte[binStr.length() / 2];
173                 
174                 StringBuffer sbuf = new StringBuffer(binStr);
175                 for (int i = 0; i < bin.length; i++) {
176                         String s = sbuf.substring(i*2, i*2+2).toUpperCase();
177                         bin[i] = (byte)Integer.parseInt(s, 16);
178                 }
179                 return bin;
180         }
181 }