import java.security.SecureRandom; import javax.crypto.spec.SecretKeySpec; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Hex; public final class CipherFinal { //暗号化 public static String encrypt(String key,String text) throws Exception{ //初期化(Blowfish方式で) SecretKeySpec skSpec=new SecretKeySpec(key.getBytes(),"Blowfish"); Cipher cipher=Cipher.getInstance("Blowfish"); cipher.init(Cipher.ENCRYPT_MODE, skSpec); //暗号化を実行(バイト配列が得られる) byte[] encrypted=cipher.doFinal(text.getBytes()); //暗号化されたバイト配列を文字列化(こうせねば一部の情報が失われるらしい) return new String(Hex.encodeHex(encrypted)); //外部ライブラリーを使用 } //復号化 public static String decrypt(String key,String text) throws Exception{ //暗号化された文字列をバイト配列化 byte[] encrypted=Hex.decodeHex(text.toCharArray()); //外部ライブラリーを使用 //初期化(Blowfish方式で) SecretKeySpec skSpec=new SecretKeySpec(key.getBytes(),"Blowfish"); Cipher cipher=Cipher.getInstance("Blowfish"); cipher.init(Cipher.DECRYPT_MODE, skSpec); //復号化を実行 byte[] decrypted=cipher.doFinal(encrypted); //文字列へ return new String(decrypted); } //Key自動作成 public static String makeKeyString(int size){ SecureRandom random=new SecureRandom(); // char[] key=new char[size]; // for(int k=0;k