【Java】BouncyCastleライブラリ ~Java暗号化ライブラリ~

ダウンロード先

 * 以下のサイトで「 bcprov-jdkXXon-XXX.jar(例  bcprov-jdk15on-151.jar)」を落としてくる
http://www.bouncycastle.org/latest_releases.html

動画

 * ダウンロードや設定が見れる
https://www.youtube.com/watch?v=TuYx2ms1jgw

サンプル

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!!


関連記事

Java

暗号化/複合化する ~Apache Commonsを使用した場合~

http://blogs.yahoo.co.jp/dk521123/32780473.html

Java で暗号化/複合化する ~Java1.8 標準を使用した場合~

http://blogs.yahoo.co.jp/dk521123/34330480.html

Java で暗号化/複合化する ~Java1.8 標準を使用した場合 / IV使用編~

http://blogs.yahoo.co.jp/dk521123/36419973.html

BouncyCastleライブラリ ~Java暗号ライブラリ~

http://blogs.yahoo.co.jp/dk521123/33256866.html
C#

共有キー暗号方式 ~暗号化編~

http://blogs.yahoo.co.jp/dk521123/30818470.html