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

■ 暗号化/複合化する

 * 色々方法あるとは思うが、Apache Commonsを使って、暗号化/複合化を行う

Apache Commonsについて

Apache Commonsの使用用途

 * Base64エンコード・デコードのために使用

Apache Commonsのダウンロード先

http://commons.apache.org/proper/commons-codec/download_codec.cgi
 * ソース自体もダウンロードできるようなので「commons-codec-X.X-src.zip」を使用した
 * Eclipse の場合、プロジェクト名を右クリックし[Import]-[General]-[File System]で
  ダウンロードした「commons-codec-1.8-src\src\main\java」配下のソースをインポートする

■ サンプル

package com;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class HelloWorld {
   public static void main(String[] args) {
      try {
         String key = "1234567890123456"; // キーは16文字で
         String original = "This is a source of string!!"; // 元の文字列
         String algorithm = "AES"; // 暗号化方式
         
         String encrypedResult = encrypt(original, key, algorithm);
         System.out.println("暗号化文字列:" + encrypedResult);

         String decryptedResult = decrypt(encrypedResult, key, algorithm);
         System.out.println("復号化文字列:" + decryptedResult);

      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   /**
    * 文字列を16文字の秘密鍵でAES暗号化してBase64した文字列で返す
    */
   public static String encrypt(String originalSource, String secretKey, String algorithm)
         throws NoSuchAlgorithmException, NoSuchPaddingException,
         InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

      byte[] originalBytes = originalSource.getBytes();
      byte[] encryptBytes = cipher(
            Cipher.ENCRYPT_MODE, originalBytes, secretKey, algorithm);
      byte[] encryptBytesBase64 = Base64.encodeBase64(encryptBytes, false);
      return new String(encryptBytesBase64);
   }

   /**
    * Base64されたAES暗号化文字列を元の文字列に復元する
    */
   public static String decrypt(String encryptBytesBase64String,
         String secretKey, String algorithm) throws NoSuchAlgorithmException,
         NoSuchPaddingException, InvalidKeyException,
         IllegalBlockSizeException, BadPaddingException {

      byte[] encryptBytes = Base64.decodeBase64(encryptBytesBase64String);
      byte[] originalBytes = cipher(
            Cipher.DECRYPT_MODE, encryptBytes, secretKey, algorithm);
      return new String(originalBytes);
   }

   /**
    * 暗号化/複合化の共通部分
    */
   private static byte[] cipher(
         int mode, byte[] source, String secretKey, String algorithm)
               throws InvalidKeyException, NoSuchAlgorithmException,
               NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
      byte[] secretKeyBytes = secretKey.getBytes();

      SecretKeySpec secretKeySpec = new SecretKeySpec(secretKeyBytes, algorithm);
      Cipher cipher = Cipher.getInstance(algorithm);
      cipher.init(mode, secretKeySpec);
      return cipher.doFinal(source);
   }
}

出力結果

暗号化文字列:B/UZj3IF6F6tObxiTAqdDsDLxUKGCBuAYjVTQP1GSnI=
復号化文字列:This is a source of string!!


関連記事

Java

暗号化/複合化する ~Apache Commonsを使用した場合~
https://blogs.yahoo.co.jp/dk521123/32780473.html
Java で暗号化/複合化する ~Java1.8 標準を使用した場合~
https://blogs.yahoo.co.jp/dk521123/34330480.html
Java で暗号化/複合化する ~Java1.8 標準を使用した場合 / IV使用編~
https://blogs.yahoo.co.jp/dk521123/36419973.html
【トラブル】Java で初回の暗号化/複合化処理に時間が掛かる
https://blogs.yahoo.co.jp/dk521123/36783396.html
BouncyCastleライブラリ ~Java暗号ライブラリ~
https://blogs.yahoo.co.jp/dk521123/33256866.html
Java】 セキュアなランダム文字列生成を考える
https://blogs.yahoo.co.jp/dk521123/36415526.html

C#

共有キー暗号方式 ~暗号化編~
https://blogs.yahoo.co.jp/dk521123/30818470.html

その他

暗号化アルゴリズム
https://blogs.yahoo.co.jp/dk521123/36417953.html
暗号に関わる用語
https://blogs.yahoo.co.jp/dk521123/37269757.html