使用 C# AesCryptoServiceProvider 加密数据,并使用 BouncyCastle AesFastEngine 加密

发布于 2024-09-07 11:48:55 字数 1834 浏览 2 评论 0原文

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

有办法做到这一点吗?

我没有找到 Bouncy Castle 实现中使用的 IV...有吗?

任何帮助都会很好! Markus

编辑:

以下代码用于初始化 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,感谢您的回答,但它仍然无法正常工作... 我有一个示例解决方案可以在此处下载。

如果你点击这两个按钮,你已经得到了一个不同的加密数组......??? 解密用充气城堡生成的数组会导致异常,说明加密数据的长度无效...

这是我为解密编写的代码:

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

编辑:

接近了! RijndaelManaged 托管正在运行,但它为我提供了多一个字节的加密数据。所有其他字节都是相同的...我尝试了很多,但我不知道如何使用充气城堡获取最后一个字节...没有最后一个字节,就不可能使用 RijndaelManaged 解密数据...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2024-09-14 11:48:55

您使用的 IV 是默认 IV,全为零。您应该能够在 .NET 中执行此操作,方法是创建一个 AesManaged 对象,将模式设置为 CipherMode.CFB 并将 FeedbackSize 设置为 8。然后使用 CreateEncryptor 方法创建一个 ICryptoTransform,并依次使用它创建一个 CryptoStream此示例 应该有助于完成最后几个步骤。

编辑:

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

AesManagedAlg.Mode = CipherMode.CFB;

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

编辑2:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文