From 5311fde50da266fd0d3fa466fbe6fe7a52f9dae4 Mon Sep 17 00:00:00 2001 From: yuta Date: Wed, 31 Aug 2011 18:11:45 +0900 Subject: [PATCH] =?utf8?q?RV09:=E3=82=A8=E3=83=A9=E3=83=BC=E5=87=A6?= =?utf8?q?=E7=90=86=E3=82=92=E5=B0=91=E3=81=97=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/jp/gr/java_conf/ofnhwx/olib/utils/OCrypt.java | 371 ++++++++++++++-------- 1 file changed, 234 insertions(+), 137 deletions(-) rewrite src/jp/gr/java_conf/ofnhwx/olib/utils/OCrypt.java (70%) diff --git a/src/jp/gr/java_conf/ofnhwx/olib/utils/OCrypt.java b/src/jp/gr/java_conf/ofnhwx/olib/utils/OCrypt.java dissimilarity index 70% index b37941e..7682c33 100644 --- a/src/jp/gr/java_conf/ofnhwx/olib/utils/OCrypt.java +++ b/src/jp/gr/java_conf/ofnhwx/olib/utils/OCrypt.java @@ -1,137 +1,234 @@ - -package jp.gr.java_conf.ofnhwx.olib.utils; - -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; - -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; - -/** - * 暗号用ライブラリ. - * @author yuta - * @since 1.0.0 - * @version 1.0.0 - */ -public class OCrypt { - - private static final String ALGORITHM_AES = "AES"; - private static final String ALGORITHM_MD5 = "MD5"; - private static final String ALGORITHM_SHA = "SHA1PRNG"; - - private static final String AES_ECB_PKCS5 = "AES/ECB/PKCS5Padding"; - private static final String AES_CBC_PKCS5 = "AES/CBC/PKCS5Padding"; - - /** - * CBC形式で使用するデータ. - */ - public static class EncryptedData { - public byte[] iv; - public byte[] data; - } - - /** - * デフォルトコンストラクタ - インスタンスの生成を禁止する. - */ - private OCrypt() {} - - /** - * データの暗号化(ECB). - * @param key 暗号化に使用するキー - * @param data 暗号化するデータ - * @return 暗号化されたデータ - * @throws IllegalArgumentException - * @throws NoSuchAlgorithmException - * @throws NoSuchPaddingException - * @throws InvalidKeyException - * @throws IllegalBlockSizeException - * @throws BadPaddingException - */ - public static final byte[] encryptECB(final byte[] key, final byte[] data) throws IllegalArgumentException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { - final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM_AES); - final Cipher cipher = Cipher.getInstance(AES_ECB_PKCS5); - cipher.init(Cipher.ENCRYPT_MODE, keySpec); - return cipher.doFinal(data); - } - - /** - * データの復号化(ECB). - * @param key 復号化に使用するキー - * @param data 復号化するデータ - * @return 復号化されたデータ - * @throws IllegalArgumentException - * @throws NoSuchAlgorithmException - * @throws NoSuchPaddingException - * @throws InvalidKeyException - * @throws IllegalBlockSizeException - * @throws BadPaddingException - */ - public static final byte[] decryptECB(final byte[] key, final byte[] data) throws IllegalArgumentException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { - final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM_AES); - final Cipher cipher = Cipher.getInstance(AES_ECB_PKCS5); - cipher.init(Cipher.DECRYPT_MODE, keySpec); - return cipher.doFinal(data); - } - - /** - * データの暗号化(CBC). - * @param key 暗号化に使用するキー - * @param data 暗号化するデータ - * @return 暗号化されたデータ - * @throws IllegalArgumentException - * @throws NoSuchAlgorithmException - * @throws NoSuchPaddingException - * @throws IllegalBlockSizeException - * @throws BadPaddingException - * @throws InvalidKeyException - */ - public static final EncryptedData encryptCBC(final byte[] key, final byte[] data) throws IllegalArgumentException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException { - final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM_AES); - final Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5); - final SecureRandom random = SecureRandom.getInstance(ALGORITHM_SHA); - cipher.init(Cipher.ENCRYPT_MODE, keySpec, random); - final EncryptedData result = new EncryptedData(); - result.iv = cipher.getIV(); - result.data = cipher.doFinal(data); - return result; - } - - /** - * データの復号化(CBC). - * @param key 復号化に使用するキー - * @param data 復号化するデータ - * @return 復号化されたデータ - * @throws IllegalArgumentException - * @throws NoSuchAlgorithmException - * @throws NoSuchPaddingException - * @throws InvalidKeyException - * @throws InvalidAlgorithmParameterException - * @throws IllegalBlockSizeException - * @throws BadPaddingException - */ - public static final byte[] decryptCBC(final byte[] key, final EncryptedData data) throws IllegalArgumentException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { - final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM_AES); - final Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5); - cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(data.iv)); - return cipher.doFinal(data.data); - } - - /** - * MD5ダイジェストの取得. - * @param data MD5ダイジェストを取得するデータ - * @return MD5ダイジェスト - * @throws NoSuchAlgorithmException - */ - public static final byte[] digestMD5(final byte[] data) throws NoSuchAlgorithmException { - final MessageDigest digest = MessageDigest.getInstance(ALGORITHM_MD5); - return digest.digest(data); - } - -} + +package jp.gr.java_conf.ofnhwx.olib.utils; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +/** + * 暗号用ライブラリ. + * @author yuta + * @since 1.0.0 + * @version 1.0.1 + */ +public abstract class OCrypt { + + private static final String ALGORITHM_AES = "AES"; + private static final String ALGORITHM_MD5 = "MD5"; + private static final String ALGORITHM_SHA = "SHA1PRNG"; + + private static final String AES_ECB_PKCS5 = "AES/ECB/PKCS5Padding"; + private static final String AES_CBC_PKCS5 = "AES/CBC/PKCS5Padding"; + + /** + * CBC形式で使用するデータ. + */ + public static class EncryptedData { + public byte[] iv; + public byte[] data; + public EncryptedData(final byte[] iv, final byte[] data) { + this.iv = iv; + this.data = data; + } + } + + /** + * 暗号化失敗. + */ + @SuppressWarnings("serial") + public static class BadEncryptException extends Exception { + public BadEncryptException(final Throwable e) { + super(e); + } + } + + /** + * 復号失敗. + */ + @SuppressWarnings("serial") + public static class BadDecryptException extends Exception { + public BadDecryptException(final Throwable e) { + super(e); + } + } + + /** + * ダイジェスト生成失敗. + */ + @SuppressWarnings("serial") + public static class BadDigestException extends Exception { + public BadDigestException(final Throwable e) { + super(e); + } + } + + /** + * データの暗号化(ECB). + * @param key 暗号化に使用するキー + * @param data 暗号化するデータ + * @return 暗号化されたデータか`null' + */ + public static final byte[] encryptECB(final byte[] key, final byte[] data) { + try { + return encryptECBwithThrow(key, data); + } catch (final BadEncryptException e) { + return null; + } + } + + /** + * データの暗号化(ECB). + * @param key 暗号化に使用するキー + * @param data 暗号化するデータ + * @return 暗号化されたデータ + * @throws BadEncryptException 暗号化に失敗. + */ + public static final byte[] encryptECBwithThrow(final byte[] key, final byte[] data) throws BadEncryptException { + try { + final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM_AES); + final Cipher cipher = Cipher.getInstance(AES_ECB_PKCS5); + cipher.init(Cipher.ENCRYPT_MODE, keySpec); + return cipher.doFinal(data); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new BadEncryptException(e); + } + } + + /** + * データの復号(ECB). + * @param key 復号に使用するキー + * @param data 復号するデータ + * @return 復号されたデータか`null' + */ + public static final byte[] decryptECB(final byte[] key, final byte[] data) { + try { + return decryptECBwithThrow(key, data); + } catch (final BadDecryptException e) { + return null; + } + } + + /** + * データの復号(ECB). + * @param key 復号に使用するキー + * @param data 復号するデータ + * @return 復号されたデータ + * @throws BadDecryptException 復号に失敗 + */ + public static final byte[] decryptECBwithThrow(final byte[] key, final byte[] data) throws BadDecryptException { + try { + final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM_AES); + final Cipher cipher = Cipher.getInstance(AES_ECB_PKCS5); + cipher.init(Cipher.DECRYPT_MODE, keySpec); + return cipher.doFinal(data); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new BadDecryptException(e); + } + } + + /** + * データの暗号化(CBC). + * @param key 暗号化に使用するキー + * @param data 暗号化するデータ + * @return 暗号化されたデータか`null' + */ + public static final EncryptedData encryptCBC(final byte[] key, final byte[] data) { + try { + return encryptCBCwithThrow(key, data); + } catch (final BadEncryptException e) { + return null; + } + } + + /** + * データの暗号化(CBC). + * @param key 暗号化に使用するキー + * @param data 暗号化するデータ + * @return 暗号化されたデータ + * @throws BadEncryptException 暗号化に失敗 + */ + public static final EncryptedData encryptCBCwithThrow(final byte[] key, final byte[] data) throws BadEncryptException { + try { + final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM_AES); + final Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5); + final SecureRandom random = SecureRandom.getInstance(ALGORITHM_SHA); + cipher.init(Cipher.ENCRYPT_MODE, keySpec, random); + return new EncryptedData(cipher.getIV(), cipher.doFinal(data)); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new BadEncryptException(e); + } + } + + /** + * データの復号(CBC). + * @param key 復号に使用するキー + * @param data 復号するデータ + * @return 復号されたデータか`null' + */ + public static final byte[] decryptCBC(final byte[] key, final EncryptedData data) { + try { + return decryptCBCwithThrow(key, data); + } catch (final BadDecryptException e) { + return null; + } + } + + /** + * データの復号(CBC). + * @param key 復号に使用するキー + * @param data 復号するデータ + * @return 復号されたデータ + * @throws BadDecryptException 復号に失敗 + */ + public static final byte[] decryptCBCwithThrow(final byte[] key, final EncryptedData data) throws BadDecryptException { + try { + final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM_AES); + final Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5); + cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(data.iv)); + return cipher.doFinal(data.data); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new BadDecryptException(e); + } + } + + /** + * MD5ダイジェストの取得. + * @param data MD5ダイジェストを取得するデータ + * @return MD5ダイジェストか`null' + */ + public static final byte[] digestMD5(final byte[] data) { + try { + return digestMD5withThrow(data); + } catch (final BadDigestException e) { + return null; + } + } + + /** + * MD5ダイジェストの取得. + * @param data MD5ダイジェストを取得するデータ + * @return MD5ダイジェスト + * @throws BadDigestException + */ + public static final byte[] digestMD5withThrow(final byte[] data) throws BadDigestException { + try { + final MessageDigest digest = MessageDigest.getInstance(ALGORITHM_MD5); + return digest.digest(data); + } catch (final NoSuchAlgorithmException e) { + throw new BadDigestException(e); + } + } + +} -- 2.11.4.GIT