views:

212

answers:

1

Hello,

I need to decrypt Data with Standard C# AesCryptoServiceProvider which was encrypted with Bouncy Castle AesFastEngine on the Java side. (To decrypt the Data using the c# implementation of Bounca Castle is no problem)

Is there a way to do this?

I don't find the IV used in the Bouncy Castle implementation... Is there any?

Any help would be really fine! Markus

EDIT:

The following code is used to initialize the AesFastEngine:

BlockCipher coder = new AESFastEngine();
CFBBlockCipher cfbCipher = new CFBBlockCipher(coder, 8);
StreamCipher streamCipher = new StreamBlockCipher(cfbCipher);
streamCipher.Init(true, keyParameter);
streamCipher.ProcessBytes(data, 0, data.Length, encodedMessageBytes, 0);

EDIT:

Hello Grec, thanks for your answer, but it is still not working... I have a sample solution to download here.

If you click the two Buttons you get already a different crypted array...??? Decrypting the Array produced with bouncy castle is leading to an exception saying that the crypted data has an invalid length...

Here is the Code I wrote for decryption:

AesManagedAlg = new AesManaged();
AesManagedAlg.Mode = CipherMode.CBC;
AesManagedAlg.FeedbackSize = 8;
AesManagedAlg.Key = key;
// Use Test
AesManagedAlg.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 

// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = AesManagedAlg.CreateDecryptor(AesManagedAlg.Key, AesManagedAlg.IV);

// Create the streams used for decryption.
msDecrypt = new MemoryStream(cipherText);
csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

// Read the decrypted bytes from the decrypting stream
var decryptedData = new List<byte>();
var buffer = new byte[1];
while (true) {
    var readedBytes = csDecrypt.Read(buffer, 0, buffer.Length);
    if(readedBytes == 0) break;
    decryptedData.Add(buffer[0]);
}
ret = decryptedData.ToArray();

Edit:

Getting close! RijndaelManaged managed is working but it gives me one byte more of crypted data. All the other bytes are the same... I tryed a lot but I don't know how to get the last byte with bouncy castle... Without this last byte it is not possible to decrypte the data with RijndaelManaged...

A: 

The IV you are using is the the default IV, all zeros. You should be able to do this in .NET by creating an AesManaged object, setting the mode to CipherMode.CFB and setting the FeedbackSize to 8. Then use the CreateEncryptor method to create an ICryptoTransform, and use this in turn to create a CryptoStream. This example should help with the last few steps.

EDIT:

Looking at the new code you posted, the second line is wrong. You need to specify CFB mode, not CBC. The second line should be

AesManagedAlg.Mode = CipherMode.CFB;

Also, it looks like you are decrypting readedBytes bytes of data but only adding buffer[0] to the plaintext and ignoring the rest.

GregS
Hello Greg, thanks a lot for your quick answers. I will give it a try and post the results...
Mahop
Hello Greg, thanks a lot for following this issue even with the timegap from germany to you!!! If I try to set CipherMode.CFB I get the Exception: (tranlated from German) The specified encryption Mode is not valid for this algorithm. ... ??? (The buffer is only 1 byte long - so buffer[0] should be fine)
Mahop
Yeah, I get the same error. But it works with RijndaelManaged, which is the same algorithm.
GregS
Thanks for this hint!!! RijndaelManaged managed is working but it gives me one byte more of crypted data. All the other bytes are the same... I tryed a lot but I don't know how to get the last byte with bouncy castle...
Mahop