3

I am trying to decrypt data that is encrypted with AES in CFB mode using bouncy castle. I am having a hard time.

I have some Java code that initalizes a StreamBlockCipher with a CFBBlockCipher that contains the AESEngine that I use as a reference.

But it is impossible in C# since the StreamBlockCipher class checks that the given IBlockCipher has a block size of 1 byte.

I tried manually running the data through the CfbBlockCipher with this code:

var cfb = new CfbBlockCipher(new AesEngine(), 128);
f.Init(false, new KeyParameter(key));
byte[] decrypted = new byte[data.Length];
for (int i = 0; i < data.Length; i += 16)
{
    cfb.DecryptBlock(data, i, decrypted, i);
}

But when I got to the final block (which is not divisible by 16 bytes) the code crashes with an exception.

I have tried initializing a StreamCipherBlock with this code:

var f = new StreamBlockCipher(new CfbBlockCipher(new AesEngine(), 128));

But the ctor for StreamBlockCipher throws an exception from here.

How do I decrypt my blob of data?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Ziv
  • 2,755
  • 5
  • 30
  • 42
  • *"But when I got to the final block (which is not divisible by 16 bytes) the code crashes with an exception."* - How about padding the last block with any bytes up to the next block size, encrypting and removing the same number of bytes after decryption? CFB is a streaming mode, so any bytes that come after the actual ciphertext may be thrown away after decryption. This is probably a crude workaround, but it should work. – Artjom B. Dec 14 '15 at 18:18

0 Answers0