75.5. 初期化ベクトルの指定

一部の暗号化アルゴリズム、特にブロックアルゴリズムでは、初期化ベクトルと呼ばれるデータの初期ブロックを使用して設定する必要があります。JCE では、これは Cipher が初期化されるときに AlgorithmParameterSpec として渡されます。このようなベクトルを CryptoDataFormat で使用するには、必要なデータを含む byte[] で設定できます。

KeyGenerator generator = KeyGenerator.getInstance("DES");
byte[] initializationVector = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES/CBC/PKCS5Padding", generator.generateKey());
cryptoFormat.setInitializationVector(initializationVector);

from("direct:init-vector")
    .marshal(cryptoFormat)
    .to("mock:encrypted")
    .unmarshal(cryptoFormat)
    .to("mock:unencrypted");

または Spring を使用して、byte[] への参照を提供します

<crypto id="initvector" algorithm="DES/CBC/PKCS5Padding" keyRef="desKey" initVectorRef="initializationVector" />

暗号化フェーズと復号化フェーズの両方で同じベクトルが必要です。IV を秘密にしておく必要がないため、DataFormat を使用すると、IV を暗号化されたデータにインライン化し、その後復号化フェーズで読み取って Cipher を初期化できます。IV をインライン化するには、/oinline フラグを設定します。

KeyGenerator generator = KeyGenerator.getInstance("DES");
byte[] initializationVector = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
SecretKey key = generator.generateKey();

CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES/CBC/PKCS5Padding", key);
cryptoFormat.setInitializationVector(initializationVector);
cryptoFormat.setShouldInlineInitializationVector(true);
CryptoDataFormat decryptFormat = new CryptoDataFormat("DES/CBC/PKCS5Padding", key);
decryptFormat.setShouldInlineInitializationVector(true);

from("direct:inline")
    .marshal(cryptoFormat)
    .to("mock:encrypted")
    .unmarshal(decryptFormat)
    .to("mock:unencrypted");

または Spring 付き。

<crypto id="inline" algorithm="DES/CBC/PKCS5Padding" keyRef="desKey" initVectorRef="initializationVector"
  inline="true" />
<crypto id="inline-decrypt" algorithm="DES/CBC/PKCS5Padding" keyRef="desKey" inline="true" />

初期化ベクトルの使用の詳細については、