【Java】XORを使用し、データの簡易暗号化・復号化する処理

■概要

XOR(排他的論理和)での簡易的な暗号・復号化処理を実装する。

暗号化と復号の処理が同じなのが特徴。

注意:XOR は学習・軽量加工向け。機密性が必要な用途には使用しない

実装概要

  • 1ビット同士のXORは「同じ→0、違う→1」
    例)0^0=0, 0^1=1, 1^0=1, 1^1=0
  • 同じキーで2回 XOR すると元に戻る:(M ^ K) ^ K = M

■サンプルコード

以下に本処理のサンプルコードを示す。

public class XorEncryptDecryptMain {

  /**
   * XOR暗号処理
   *
   * @param data: 暗号・復号化対象
   * @param key:  暗号・復号化キー
   * @return: 暗号・復号済みデータ
   */
  private static byte[] xorProcess(byte[] data, byte key) {
    // 暗号化対象データ桁分のバイト配列をセット
    byte[] result = new byte[data.length];

    // データの桁数分繰り返し
    for (int i = 0; i < data.length; i++) {
      // XOR
      result[i] = (byte) (data[i] ^ key);
    }
    return result;
  }

  public static void main(String[] args) {
    // 暗号化対象文字列をセット
    String message = "SecretMessage";

    // 鍵 (XOR 用): 範囲は -128 ~ 127
    byte key = 0x5A;

    // 暗号化
    byte[] encrypted = xorProcess(message.getBytes(), key);

    // 暗号化データをバイト表示
    System.out.println("暗号化データ:");
    for (byte b : encrypted) {
      System.out.print((b & 0xFF) + " ");
    }
    System.out.println();

    // 暗号化バイトデータを文字列変換し表示
    String resultEncrypted = new String(encrypted);
    System.out.println("暗号化結果: " + resultEncrypted);

    // 復号化(同じ処理をもう一度)
    byte[] decrypted = xorProcess(encrypted, key);

    // 復号化結果を文字列変換し表示
    String result = new String(decrypted);
    System.out.println("復号化結果: " + result);
  }

}

■実行結果

暗号化データ:
9 63 57 40 63 46 23 63 41 41 59 61 63 
暗号化結果: 	?9(?.?));=?
復号化結果: SecretMessage

コメント

タイトルとURLをコピーしました