BlowFishで暗号化・復号化
Exception握りつぶしてたり作り途中ですけど、BlowFishで暗号化・復号化できました。かなり簡単!余計なメソッドたくさんありますけど、暗号化・復号化してるのはdoCipherメソッドです。
import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.apache.log4j.Logger; public class BlowFishUtil { private Logger logger = Logger.getLogger(BlowFishUtil.class); private final String ALGORITHM = "Blowfish"; private String passPhrase; public BlowFishUtil(String passPhrase) { this.passPhrase = passPhrase; } public String decrypt(String value) { return this.decrypt(value.getBytes()); } public String decrypt(byte value) { String result = null; try { byte decrypted = this.doCipher(Cipher.DECRYPT_MODE, value); result = new String(decrypted); } catch (Exception e) { this.logger.fatal("", e); } return result; } public byte encrypt(String value) { return encrypt(value.getBytes()); } public byte encrypt(byte value) { byte result = null; try { result = this.doCipher(Cipher.ENCRYPT_MODE, value); } catch (Exception e) { this.logger.fatal("", e); } return result; } private byte doCipher(int mode, byte value) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(this.passPhrase.getBytes(), this.ALGORITHM); Cipher cipher = Cipher.getInstance(this.ALGORITHM); cipher.init(mode, keySpec); byte[] result = cipher.doFinal(value); return result; } }