私は自分でCBCモードを実装しています。そして、各 CBC ブロックの E 関数として AES を使用します。
これが私の暗号化コードです:
public static List<Byte> encrypt(List<Byte> bytes, byte[] key) throws Exception {
byte[] bytesArray = BytesConverter.toByteArray(bytes);
SecretKey secretKey = new SecretKeySpec(key, AES);
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return BytesConverter.toByteList(cipher.update(bytesArray));
}
update
AESパッドを追加したくないので使用します。CBCアルゴリズムの最初の最後のブロックは自分でやってます。
暗号文ブロックを復号化する場合は、Cipher.DECRYPTION_MODE で同じ関数を使用します。
public static List<Byte> decrypt(List<Byte> bytes, byte[] key) throws Exception {
byte[] bytesArray = BytesConverter.toByteArray(bytes);
SecretKey secretKey = new SecretKeySpec(key, AES);
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return BytesConverter.toByteList(cipher.update(bytesArray));
}
問題はCipher.update
、復号化モードでは、encrypt
メソッドによって暗号化された入力に対して空のバイト配列が返されることです。
よくわかりません。どうしたの?