1 package jp
.gr
.java_conf
.ofnhwx
.olib
.utils
;
3 import java
.security
.MessageDigest
;
4 import java
.security
.NoSuchAlgorithmException
;
5 import java
.security
.SecureRandom
;
7 import javax
.crypto
.Cipher
;
8 import javax
.crypto
.spec
.IvParameterSpec
;
9 import javax
.crypto
.spec
.SecretKeySpec
;
15 public final class OCrypt
{
17 private static final String ALGORITHM_AES
= "AES";
18 private static final String ALGORITHM_MD5
= "MD5";
19 private static final String ALGORITHM_SHA
= "SHA1PRNG";
21 private static final String AES_ECB_PKCS5
= "AES/ECB/PKCS5Padding";
22 private static final String AES_CBC_PKCS5
= "AES/CBC/PKCS5Padding";
29 public static class EncryptedData
{
32 public EncryptedData(byte[] iv
, byte[] data
) {
41 @SuppressWarnings("serial")
42 public static class BadEncryptException
extends Exception
{
43 public BadEncryptException(Throwable e
) {
51 @SuppressWarnings("serial")
52 public static class BadDecryptException
extends Exception
{
53 public BadDecryptException(Throwable e
) {
61 @SuppressWarnings("serial")
62 public static class BadDigestException
extends Exception
{
63 public BadDigestException(Throwable e
) {
70 * @param key 暗号化に使用するキー
71 * @param data 暗号化するデータ
74 public static final byte[] encryptECB(byte[] key
, byte[] data
) {
76 return encryptECBwithThrow(key
, data
);
77 } catch (BadEncryptException e
) {
84 * @param key 暗号化に使用するキー
85 * @param data 暗号化するデータ
87 * @throws BadEncryptException 暗号化に失敗.
89 public static final byte[] encryptECBwithThrow(byte[] key
, byte[] data
) throws BadEncryptException
{
91 SecretKeySpec keySpec
= new SecretKeySpec(key
, ALGORITHM_AES
);
92 Cipher cipher
= Cipher
.getInstance(AES_ECB_PKCS5
);
93 cipher
.init(Cipher
.ENCRYPT_MODE
, keySpec
);
94 return cipher
.doFinal(data
);
95 } catch (RuntimeException e
) {
97 } catch (Exception e
) {
98 throw new BadEncryptException(e
);
104 * @param key 復号に使用するキー
105 * @param data 復号するデータ
108 public static final byte[] decryptECB(byte[] key
, byte[] data
) {
110 return decryptECBwithThrow(key
, data
);
111 } catch (BadDecryptException e
) {
118 * @param key 復号に使用するキー
119 * @param data 復号するデータ
121 * @throws BadDecryptException 復号に失敗
123 public static final byte[] decryptECBwithThrow(byte[] key
, byte[] data
) throws BadDecryptException
{
125 SecretKeySpec keySpec
= new SecretKeySpec(key
, ALGORITHM_AES
);
126 Cipher cipher
= Cipher
.getInstance(AES_ECB_PKCS5
);
127 cipher
.init(Cipher
.DECRYPT_MODE
, keySpec
);
128 return cipher
.doFinal(data
);
129 } catch (RuntimeException e
) {
131 } catch (Exception e
) {
132 throw new BadDecryptException(e
);
138 * @param key 暗号化に使用するキー
139 * @param data 暗号化するデータ
142 public static final EncryptedData
encryptCBC(byte[] key
, byte[] data
) {
144 return encryptCBCwithThrow(key
, data
);
145 } catch (BadEncryptException e
) {
152 * @param key 暗号化に使用するキー
153 * @param data 暗号化するデータ
155 * @throws BadEncryptException 暗号化に失敗
157 public static final EncryptedData
encryptCBCwithThrow(byte[] key
, byte[] data
) throws BadEncryptException
{
159 SecretKeySpec keySpec
= new SecretKeySpec(key
, ALGORITHM_AES
);
160 Cipher cipher
= Cipher
.getInstance(AES_CBC_PKCS5
);
161 SecureRandom random
= SecureRandom
.getInstance(ALGORITHM_SHA
);
162 cipher
.init(Cipher
.ENCRYPT_MODE
, keySpec
, random
);
163 return new EncryptedData(cipher
.getIV(), cipher
.doFinal(data
));
164 } catch (RuntimeException e
) {
166 } catch (Exception e
) {
167 throw new BadEncryptException(e
);
173 * @param key 復号に使用するキー
174 * @param data 復号するデータ
177 public static final byte[] decryptCBC(byte[] key
, EncryptedData data
) {
179 return decryptCBCwithThrow(key
, data
);
180 } catch (BadDecryptException e
) {
187 * @param key 復号に使用するキー
188 * @param data 復号するデータ
190 * @throws BadDecryptException 復号に失敗
192 public static final byte[] decryptCBCwithThrow(byte[] key
, EncryptedData data
) throws BadDecryptException
{
194 SecretKeySpec keySpec
= new SecretKeySpec(key
, ALGORITHM_AES
);
195 Cipher cipher
= Cipher
.getInstance(AES_CBC_PKCS5
);
196 cipher
.init(Cipher
.DECRYPT_MODE
, keySpec
, new IvParameterSpec(data
.iv
));
197 return cipher
.doFinal(data
.data
);
198 } catch (RuntimeException e
) {
200 } catch (Exception e
) {
201 throw new BadDecryptException(e
);
207 * @param data MD5ダイジェストを取得するデータ
210 public static final byte[] digestMD5(byte[] data
) {
212 return digestMD5withThrow(data
);
213 } catch (BadDigestException e
) {
220 * @param data MD5ダイジェストを取得するデータ
222 * @throws BadDigestException
224 public static final byte[] digestMD5withThrow(byte[] data
) throws BadDigestException
{
226 MessageDigest digest
= MessageDigest
.getInstance(ALGORITHM_MD5
);
227 return digest
.digest(data
);
228 } catch (RuntimeException e
) {
230 } catch (NoSuchAlgorithmException e
) {
231 throw new BadDigestException(e
);