Bouncycastle:AddKeyTransRecipient 的 subKeyID 参数有什么作用?

发布于 2024-08-01 14:01:32 字数 914 浏览 0 评论 0原文

我正在尝试使用 bouncycastle 对任意长度的消息进行非对称加密。 (1.4+ 使用 C#)

这是我现在的代码。 它应该(但没有)生成一条 CMS 消息,其中数据本身使用随机密钥使用 AES256 进行加密,并且该密钥使用 keyPair 中的公钥进行加密。

keyPair 是一个 RSA 密钥 (RsaKeyParameters)

public static byte[] Encrypt(byte[] input, AsymmetricCipherKeyPair keyPair)
{
    CmsEnvelopedDataGenerator generator = new CmsEnvelopedDataGenerator();

    // those two lines are certainly wrong.
    // I have no idea what the subKeyID parameter does
    byte[] subKeyId = new byte[] {};
    generator.AddKeyTransRecipient(keyPair.Public, subKeyId);

    CmsProcessableByteArray cmsByteArray = new CmsProcessableByteArray(input);
    CmsEnvelopedData envelopeData = 
      generator.Generate(cmsByteArray, CmsEnvelopedDataGenerator.Aes256Cbc);

    return envelopeData.GetEncoded();
}

Encrypt 方法中的 subKeyId 参数有何作用以及它需要具有什么值?

I'm trying to asymetrically encrypt a message of arbitrary length with bouncycastle. (1.4+ with C#)

This is the code I have right now. It is supposed to (but doesn't) generate a CMS message where the data itself is encrypted with AES256 with a random key and the key is encrypted with the public key from keyPair.

keyPair is an RSA-Key (RsaKeyParameters)

public static byte[] Encrypt(byte[] input, AsymmetricCipherKeyPair keyPair)
{
    CmsEnvelopedDataGenerator generator = new CmsEnvelopedDataGenerator();

    // those two lines are certainly wrong.
    // I have no idea what the subKeyID parameter does
    byte[] subKeyId = new byte[] {};
    generator.AddKeyTransRecipient(keyPair.Public, subKeyId);

    CmsProcessableByteArray cmsByteArray = new CmsProcessableByteArray(input);
    CmsEnvelopedData envelopeData = 
      generator.Generate(cmsByteArray, CmsEnvelopedDataGenerator.Aes256Cbc);

    return envelopeData.GetEncoded();
}

What is the subKeyId parameter in the Encrypt method for and what value does it need to have?

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

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

发布评论

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

评论(3

白龙吟 2024-08-08 14:01:32

要使用 AES,仅使用非对称密码密钥对是不够的。

您应该使用 X509 证书,其中公钥由证书颁发机构 (CA) 签名。

subKeyId是证书的一个属性,主题密钥标识符:

       (X509Certificate) cert.getSubjectUniqueID()

要加密任意长度的消息,您应该仅使用AES来交换对称密钥密码并使用此密钥进行对称加密。

To use AES, it is not enough to use a AsymmetricCipherKeyPair.

You should use a X509 certificate, where the public key is signed by an certificate authority (CA).

the subKeyId is an attribute of the certificate, the subject Key Identifier:

       (X509Certificate) cert.getSubjectUniqueID()

To encrypt a message of artrary length, you should use AES only to exchange a symmetric Keypassword and use this key for symmetric encryption.

蔚蓝源自深海 2024-08-08 14:01:32

aaronls 对“Beginning cryptography with Java”的作者有点不公平,毕竟他自己首先编写了所有单元测试......

正如其他评论者指出的那样,CMS 使用证书,你不能仅仅传递公钥; 必须可以通过“SubjectKeyIdentifier”或“IssuerAndSerialNumber”引用密钥。 AddKeyTransRecipient 的两个替代方案允许这样做。 如果这些术语对您来说没有任何意义,您可能需要阅读一些有关 X.509 的背景知识。

aaronls is being a little unfair to the author of "Beginning cryptography with Java", who after all wrote all the unit tests himself in the first place...

As other commenters have pointed out, CMS works with certificates, you can't just pass a public key; it must be possible to refer to the key either by "SubjectKeyIdentifier" or by "IssuerAndSerialNumber". The two alternatives of AddKeyTransRecipient allow this. If these terms don't mean anything to you, you probably need to do some background reading on X.509.

自此以后,行同陌路 2024-08-08 14:01:32

查看 BouncyCastle 源的 EnvelopedDataTest.cs 文件中的函数 TryKekAlgorithm。 他们没有执行 AddKeyTransRecipient,而是执行 AddKekRecipient。

    public static byte[] Encrypt(byte[] input, AsymmetricCipherKeyPair keyPair)
    {
        CmsEnvelopedDataGenerator generator = new CmsEnvelopedDataGenerator();
        DerObjectIdentifier algOid = //initialize

        //Still trying to figure out kekId here.
        byte[] kekId = new byte[] { 1, 2, 3, 4, 5 };
        string keyAlgorithm = ParameterUtilities.GetCanonicalAlgorithmName("AES256");

        generator.AddKekRecipient(keyAlgorithm, keyPair.Public, kekId);

        CmsProcessableByteArray cmsByteArray = new CmsProcessableByteArray(input);
        CmsEnvelopedData envelopeData =
          generator.Generate(cmsByteArray, CmsEnvelopedDataGenerator.Aes256Cbc);

        return envelopeData.GetEncoded();
    }

编辑:我认为 kekId 只是用于引用密钥的唯一标识符。 只是“命名”密钥的一种方法。 所以你可以拥有一本钥匙簿,每个钥匙都有一个标识符。 当您发送加密消息时,未加密密钥标识符会告诉您使用哪个密钥来加密该消息。

下面是第 140 页上对关键标识符的很好的解释:
[http://books.google.com/books?id=Pgg-Es2j3UEC&pg=PA140&lpg=PA140&dq=understanding+key+identifiers+encrypt&source=bl&ots=nFg0BzM2ht&sig=Ux5sreXMKyuEEZu0uaxE7cXC1VI&hl =en&ei=JKKJStbHGJivtgffsNznDA&sa=X&oi=book_result&ct=result&resnum=6#v=onepage&q=&f=false][1]

这是另一本书正在使用BouncyCastleCrypto,但看起来他们只是窃取了单元测试源代码。 他们对此做了一些解释:
[http ://books.google.com/books?id=WLLAD2FKH3IC&pg=PA343&lpg=PA343&dq=CmsEnvelopedDataGenerator+AddKekRecipient&source=bl&ots=O9HinJm3yB&sig=K5Z99DIVWW4-0abPIFR7x4lzBhU&hl=en& ;ei=g6aJSrjeDuHktgennNjnDA& ;sa=X&oi=book_result&ct=result&resnum=6#v=onepage&q=CmsEnvelopedDataGenerator%20AddKekRecipient&f=false][2]

Look at the function TryKekAlgorithm in the EnvelopedDataTest.cs file of the BouncyCastle source. Instead of doing AddKeyTransRecipient, they are doing AddKekRecipient.

    public static byte[] Encrypt(byte[] input, AsymmetricCipherKeyPair keyPair)
    {
        CmsEnvelopedDataGenerator generator = new CmsEnvelopedDataGenerator();
        DerObjectIdentifier algOid = //initialize

        //Still trying to figure out kekId here.
        byte[] kekId = new byte[] { 1, 2, 3, 4, 5 };
        string keyAlgorithm = ParameterUtilities.GetCanonicalAlgorithmName("AES256");

        generator.AddKekRecipient(keyAlgorithm, keyPair.Public, kekId);

        CmsProcessableByteArray cmsByteArray = new CmsProcessableByteArray(input);
        CmsEnvelopedData envelopeData =
          generator.Generate(cmsByteArray, CmsEnvelopedDataGenerator.Aes256Cbc);

        return envelopeData.GetEncoded();
    }

Edit: I think the kekId is just a unique identifier used to reference the key. Just a way to "name" the key. So you can have a book of keys, and each one has an identifier. When you send an encrypted message, the unencrypted key identifier tells you which of the keys was used to encrypt the message.

Here is a pretty good explanation of key identifiers on page 140:
[http://books.google.com/books?id=Pgg-Es2j3UEC&pg=PA140&lpg=PA140&dq=understanding+key+identifiers+encrypt&source=bl&ots=nFg0BzM2ht&sig=Ux5sreXMKyuEEZu0uaxE7cXC1VI&hl=en&ei=JKKJStbHGJivtgffsNznDA&sa=X&oi=book_result&ct=result&resnum=6#v=onepage&q=&f=false][1]

And here is another book that is using BouncyCastleCrypto, but it looks like they did little more than rip off the unit test source code. They have explained it a little:
[http://books.google.com/books?id=WLLAD2FKH3IC&pg=PA343&lpg=PA343&dq=CmsEnvelopedDataGenerator+AddKekRecipient&source=bl&ots=O9HinJm3yB&sig=K5Z99DIVWW4-0abPIFR7x4lzBhU&hl=en&ei=g6aJSrjeDuHktgennNjnDA&sa=X&oi=book_result&ct=result&resnum=6#v=onepage&q=CmsEnvelopedDataGenerator%20AddKekRecipient&f=false][2]

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