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

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

问题描述

我需要用标准C#AesCryptoServiceProvider这与充气城堡AesFastEngine在Java端加密,解密数据。 (要使用Bounca城堡的C#实现是没有问题的解密数据)

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?

我不觉得在充气城堡实现使用的IV ...有什么?

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

任何帮助将是非常好!
马库斯

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.

如果您单击两个Button你已经是一个不同的加密的阵列...?
解密与充气城堡生产的阵列是导致异常说,加密的数据有一个无效的长度...

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



编辑:

亲近! RijndaelManaged的管理工作,但它给了我一个字节加密的更多数据。所有其它字节都是一样的......我tryed了很多,但我不知道怎么去用充气城堡的最后一个字节......没有它不可能decrypte与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 对象要做到这一点在.NET中,模式设置为 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 字节的数据,但只有加入缓冲[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算法仅仅是Rijndael算法限制在128位的块大小和任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天全站免登陆