ダウンロード先
* 以下のサイトで「 bcprov-jdkXXon-XXX.jar(例 bcprov-jdk15on-151.jar)」を落としてくるhttp://www.bouncycastle.org/latest_releases.html
サンプル
SampleBouncyCastle.java
import org.bouncycastle.crypto.CryptoException; import org.bouncycastle.crypto.engines.BlowfishEngine; import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.util.encoders.Base64; public class SampleBouncyCastle { public static void main(String[] args) { String originalString = "Hello World!!"; String keyString = "secretkey"; try { String encryptedString = encrypt(originalString, keyString); System.out.println("Original String: " + originalString); System.out.println("Encrypted String: " + encryptedString); String decryptedString = decrypt(encryptedString, keyString); System.out.println("Decrypted String: " + decryptedString); } catch (Exception e) { e.printStackTrace(); } } public static String decrypt(String value, String keyString) throws Exception { BlowfishEngine engine = new BlowfishEngine(); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine); StringBuffer returnValue = new StringBuffer(); KeyParameter key = new KeyParameter(keyString.getBytes()); cipher.init(false, key); byte decodedValues[] = Base64.decode(value); byte outputs[] = new byte[cipher.getOutputSize(decodedValues.length)]; int length = cipher.processBytes(decodedValues, 0, decodedValues.length, outputs, 0); cipher.doFinal(outputs, length); String decryptedValue = new String(outputs); for (int i = 0; i < decryptedValue.length(); i++) { char character = decryptedValue.charAt(i); if (character != 0) { returnValue.append(character); } } return returnValue.toString(); } public static String encrypt(String value, String keyString) throws Exception { BlowfishEngine engine = new BlowfishEngine(); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine); KeyParameter key = new KeyParameter(keyString.getBytes()); cipher.init(true, key); byte in[] = value.getBytes(); byte out[] = new byte[cipher.getOutputSize(in.length)]; int length = cipher.processBytes(in, 0, in.length, out, 0); try { cipher.doFinal(out, length); } catch (CryptoException e) { e.printStackTrace(); throw new Exception(e.getMessage()); } return new String(Base64.encode(out)); } }
出力結果
Original String: Hello World!! Encrypted String: wYYxpOLlcLaA2SQcU8zXkA== Decrypted String: Hello World!!
参考文献
始めるに当たって参考になりそうなサイト
http://d.hatena.ne.jp/kiki114/20101105/1288947474http://www.langedge.jp/blog/index.php?itemid=150
サンプル
http://javaprogramming.language-tutorial.com/2012/10/encryption-using-bouncy-castle-api.htmlhttp://www.java-tips.org/java-me-tips/midp/encrypting-decrypting-text-messages-with-bouncy-castle-crypt.html
http://blog.zaq.ne.jp/oboe2uran/article/624/
http://siosio.hatenablog.com/entry/2012/08/31/090359
その他
* CSRhttp://senthadev.com/generating-csr-using-java-and-bouncycastle-api.html
関連記事
Java暗号化/複合化する ~Apache Commonsを使用した場合~
http://blogs.yahoo.co.jp/dk521123/32780473.htmlJava で暗号化/複合化する ~Java1.8 標準を使用した場合~
http://blogs.yahoo.co.jp/dk521123/34330480.htmlJava で暗号化/複合化する ~Java1.8 標準を使用した場合 / IV使用編~
http://blogs.yahoo.co.jp/dk521123/36419973.htmlBouncyCastleライブラリ ~Java暗号ライブラリ~
http://blogs.yahoo.co.jp/dk521123/33256866.htmlC#