How do I use bouncyCastle's CfbBlockCipher?

841 Views Asked by At

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?

0

There are 0 best solutions below