使用 bouncycastle 生成经过身份验证的 CMSEnvelopedData 消息

发布于 2024-07-30 12:54:05 字数 1361 浏览 1 评论 0原文

我正在尝试使用密码加密数据并将其存储在 ASN.1 编码的 CMS 消息中(使用 C# 和 BouncyCastle 1.4)

我的代码似乎有两个问题:

  • 数据似乎没有用HMAC,所以当我篡改encodedData(通过启用注释掉的行)时,解密仍然成功。

  • 当我解密我篡改的数据时,我得到的是损坏的纯文本。 然而只有两个明文数据块被损坏。 这似乎表明加密实际上并未使用 CBC 模式。

    编辑:忽略第二点,

这就是我正在测试的内容:

public void TestMethod1()
{
    byte[] data = new byte[1024]; // plaintext: a list of zeroes

    CmsEnvelopedDataGenerator generator = new CmsEnvelopedDataGenerator();
    CmsPbeKey encryptionKey = new Pkcs5Scheme2PbeKey("foo", new byte[] { 1, 2, 3 }, 2048);
    generator.AddPasswordRecipient(encryptionKey, CmsEnvelopedDataGenerator.Aes256Cbc);
    CmsProcessableByteArray cmsByteArray = new CmsProcessableByteArray(data);
    CmsEnvelopedData envelopeData = generator.Generate(cmsByteArray, CmsEnvelopedDataGenerator.Aes256Cbc);

    byte[] encodedData = envelopeData.GetEncoded();

    // encodedData[500] = 10; // tamper with the data

    RecipientID recipientID = new RecipientID();
    CmsEnvelopedData decodedEnvelopeData = new CmsEnvelopedData(encodedData);
    RecipientInformation recipient = decodedEnvelopeData.GetRecipientInfos().GetFirstRecipient(recipientID);

    byte[] data2 = recipient.GetContent(encryptionKey);

    CollectionAssert.AreEqual(data, data2);
}

我做错了什么? 正确的写法是什么?

I am trying to encrypt data with a password and store it inside a ASN.1 encoded CMS message (using C# and BouncyCastle 1.4)

The code I have seems to have two problems:

  • the data does not seem to be signed with a HMAC, so when I tamper with the encodedData (by enabling the commented out line), the decryption still succeeds.

  • when I decrypt the data I have tampered with, I get beck corrupted plain text. However only a two blocks of plaintext data are corrupted. This seems to suggest that the encryption does not actually use CBC mode.

    (edit: disregard the second point, this is exactly how CBC is supposed to work)

This is what I am testing with:

public void TestMethod1()
{
    byte[] data = new byte[1024]; // plaintext: a list of zeroes

    CmsEnvelopedDataGenerator generator = new CmsEnvelopedDataGenerator();
    CmsPbeKey encryptionKey = new Pkcs5Scheme2PbeKey("foo", new byte[] { 1, 2, 3 }, 2048);
    generator.AddPasswordRecipient(encryptionKey, CmsEnvelopedDataGenerator.Aes256Cbc);
    CmsProcessableByteArray cmsByteArray = new CmsProcessableByteArray(data);
    CmsEnvelopedData envelopeData = generator.Generate(cmsByteArray, CmsEnvelopedDataGenerator.Aes256Cbc);

    byte[] encodedData = envelopeData.GetEncoded();

    // encodedData[500] = 10; // tamper with the data

    RecipientID recipientID = new RecipientID();
    CmsEnvelopedData decodedEnvelopeData = new CmsEnvelopedData(encodedData);
    RecipientInformation recipient = decodedEnvelopeData.GetRecipientInfos().GetFirstRecipient(recipientID);

    byte[] data2 = recipient.GetContent(encryptionKey);

    CollectionAssert.AreEqual(data, data2);
}

What am I doing wrong? What would be the correct way to write this?

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

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

发布评论

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

评论(1

白云悠悠 2024-08-06 12:54:06

要将 HMAC 添加到 CMS 消息,您必须使用 AuthenticatedData-结构。

我对 Bouncy Castle 不是特别熟悉,但粗略地看一下 API,我会说它不支持 AuthenticatedData。 事实上,看起来它只支持SignedData进行身份验证。

所以你的选择似乎是:

  1. 使用另一个库(或编写你自己的代码)来处理 AuthenticatedData 结构。
  2. 计算 HMAC 并以非标准方式提供(以专有属性或带外)。
  3. 请改用带有 RSA 密钥对的 SignedData。

To add an HMAC to a CMS message, you would have to use a AuthenticatedData-structure.

I am not especially familiar with Bouncy Castle, but from a cursory look at the API, I would say that it does not support AuthenticatedData. In fact, it looks like it only supports SignedData for authentication.

So your options seems to be:

  1. Use another library (or write your own code) to handle the AuthenticatedData-structure.
  2. Calculate the HMAC and provide it in a non-standard way (in a proprietary Attribute or out-of-band).
  3. Use SignedData with an RSA key pair instead.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文