用C#AesCryptoServiceProvider加密数据与BouncyCastle AesFastEngine [英] Encrypt Data with C# AesCryptoServiceProvider crypted with BouncyCastle AesFastEngine

查看:561
本文介绍了用C#AesCryptoServiceProvider加密数据与BouncyCastle AesFastEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用标准C#AesCryptoServiceProvider解密数据,该文件在Java端使用Bouncy Castle AesFastEngine加密。 (要解密数据使用c#实现Bounca城堡是没有问题的)

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)

有没有办法这样做?

我没有找到在Bouncy Castle实施中使用的IV ...有没有?

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

任何帮助都会很好!
Markus

Any help would be really fine! Markus

编辑:

以下代码用于初始化AesFastEngine:

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);

编辑:

你好Grec,谢谢为您的答案,但它仍然不工作...
我有一个示例解决方案下载这里

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

如果你点击两个按钮,你已经有一个不同的加密数组?
解密使用bouncy城​​堡生成的Array导致一个例外,表示加密的数据的长度无效...

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

这是我为解密:

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();

编辑:

Rijndael管理的管理工作,但它给我一个字节更多的加密数据。所有其他字节是相同的...我尝试了很多,但我不知道如何获得最后一个字节与bouncy城​​堡...没有这最后一个字节,不可能使用RijndaelManaged ...解密数据...

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

推荐答案

您正在使用的IV是默认的IV,全零。您应该可以通过创建一个 AesManaged 对象,将模式设置为 CipherMode.CFB 和将FeedbackSize设置为8.然后使用 CreateEncryptor 方法创建一个 ICryptoTransform ,然后依次使用它来创建一个 CryptoStream的此示例应该帮助最后几个步骤。

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.

编辑:

在你发布的新代码中,第二行是错误的。您需要指定 CFB 模式,而不是 CBC 。第二行应该是

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;

另外,看起来你正在解密 readedBytes 数据字节,但只将 buffer [0] 添加到明文中,忽略其余的。

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

编辑2:

如上所述, AesManaged 不能在CFB模式下使用,但RijndaelManaged可以。请注意,AES算法只是限于128位块大小的Rijndael算法以及128,192或256位密钥大小。请参阅我的回答以类似的问题为例。

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.

这篇关于用C#AesCryptoServiceProvider加密数据与BouncyCastle AesFastEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆