1 package hayashi.yuu.tools.properties;
3 import java.security.InvalidAlgorithmParameterException;
4 import java.security.InvalidKeyException;
5 import java.security.NoSuchAlgorithmException;
6 import java.security.spec.InvalidKeySpecException;
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;
28 public static void main(String[] args) {
29 String source = "deister07";
30 System.out.println("source \"" + source + "\"");
32 Encrypt.PASSWORD_KEY = "hayashihimitukagi";
35 String encStr = encrypt(source);
36 System.out.println("encStr \"" + encStr + "\"");
39 String decStr = decode(encStr);
40 System.out.println("decodeStr \"" + decStr + "\"");
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; // 繰り返し回数
55 public static String PASSWORD_KEY = "himitukagi"; // 秘密鍵
59 * @param source 暗号化したい文字列
61 * @throws NoSuchAlgorithmException
62 * @throws NoSuchPaddingException
63 * @throws InvalidKeyException
64 * @throws BadPaddingException
65 * @throws IllegalBlockSizeException
66 * @throws InvalidKeySpecException
67 * @throws InvalidAlgorithmParameterException
69 public static String encrypt(String source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {
71 return toHexString(encrypt(source.getBytes()));
76 * @param source 暗号化したいバイト配列
78 * @throws NoSuchAlgorithmException
79 * @throws NoSuchPaddingException
80 * @throws InvalidKeyException
81 * @throws IllegalBlockSizeException
82 * @throws BadPaddingException
83 * @throws InvalidKeySpecException
84 * @throws InvalidAlgorithmParameterException
86 public static byte[] encrypt(byte[] source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {
87 PBEParameterSpec pbeParam = new PBEParameterSpec(SALT, NUM);
89 char[] pass = PASSWORD_KEY.toCharArray();
90 PBEKeySpec pbeKey = new PBEKeySpec(pass);
91 SecretKeyFactory secKeys = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
92 SecretKey secKey = secKeys.generateSecret(pbeKey);
94 Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
95 cipher.init(Cipher.ENCRYPT_MODE, secKey, pbeParam);
98 return cipher.doFinal(source);
103 * @param source 暗号化された文字列
105 * @throws NoSuchAlgorithmException
106 * @throws InvalidKeySpecException
107 * @throws NoSuchPaddingException
108 * @throws InvalidAlgorithmParameterException
109 * @throws InvalidKeyException
110 * @throws BadPaddingException
111 * @throws IllegalBlockSizeException
113 public static String decode(String source) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
114 if (source == null) {
117 byte[] bytepass = toHexBin(source);
118 return new String(decode(bytepass));
123 * @param source 暗号化されたバイト配列
125 * @throws NoSuchAlgorithmException
126 * @throws InvalidKeySpecException
127 * @throws NoSuchPaddingException
128 * @throws InvalidKeyException
129 * @throws InvalidAlgorithmParameterException
130 * @throws IllegalBlockSizeException
131 * @throws BadPaddingException
133 public static byte[] decode(byte[] source) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
135 PBEParameterSpec pbeParamSpecDec = new PBEParameterSpec(SALT, NUM);
137 PBEKeySpec pbeKeySpecDec = new PBEKeySpec(PASSWORD_KEY.toCharArray());
138 SecretKeyFactory keyFacDec = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
139 SecretKey pbeKeyDec = keyFacDec.generateSecret(pbeKeySpecDec);
141 Cipher cDec = Cipher.getInstance("PBEWithMD5AndDES");
142 cDec.init(Cipher.DECRYPT_MODE, pbeKeyDec, pbeParamSpecDec);
144 return cDec.doFinal(source);
151 * @param bs バイト配列(バイナリ)
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)) {
160 buffer.append(Integer.toHexString(0xff & binStr[i]));
162 return buffer.toString();
166 * バイナリ文字列をバイナリ配列に変換する。
171 private static byte[] toHexBin(String binStr) {
172 byte[] bin = new byte[binStr.length() / 2];
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);