【问题标题】:Generate authenticated CMSEnvelopedData Messages with bouncycastle使用 bouncycastle 生成经过身份验证的 CMSEnvelopedData 消息
【发布时间】:2010-11-17 10:30:56
【问题描述】:

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

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

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

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

    编辑:忽略第二点,this is exactly how CBC is supposed to work

这是我正在测试的:

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

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

【问题讨论】:

标签: c# encryption cryptography bouncycastle


【解决方案1】:

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

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

所以你的选择似乎是:

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

【讨论】:

  • 谢谢。 BouncyCastle C# 的 CVS 版本似乎支持 AuthenticatedData 并且库的 Java 版本似乎也实现了这一点。这看起来像是要走的路。正在调查。
  • 我选择推出自己的产品,并在将其放入 CMS 数据结构之前向消息添加校验和。 Bouncycastle C#(开发版)具有 AuthenticatedData 结构的解析器,但目前缺少易于使用的生成器。
猜你喜欢
  • 2021-12-03
  • 1970-01-01
  • 2021-05-26
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多