Encrypt Data with C# AesCryptoServiceProvider crypted with BouncyCastle AesFastEngine

落花浮王杯 提交于 2019-12-07 16:05:38

问题


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...


回答1:


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.

EDIT 2:

As noted, AesManaged cannot be used in CFB mode, but RijndaelManaged can be. Note that the AES algorithm is just the Rijndael algorithm restricted to the 128 bit blocksize and either the 128, 192, or 256 bit key size. See my answer to a similar question for an example.



来源:https://stackoverflow.com/questions/3142279/encrypt-data-with-c-sharp-aescryptoserviceprovider-crypted-with-bouncycastle-aes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!