为什么 BouncyCastle 生成的密钥小于 .Net 的 ECDiffieHellmanCng

Why Does BouncyCastle Generate Keys Smaller Than .Net's ECDiffieHellmanCng

我正在尝试使用 BouncyCastle 生成一个 public 密钥(因为我使用的是 Unity 并且无法访问 ECDiffieHellmanCng),然后我将 public 密钥传输到服务器它使用 ECDiffieHellmanCng 进行密钥处理。 服务器拒绝我的密钥,似乎是因为它的长度太短。 ECDiffieHellmanCng 生成一个 public 密钥,与 Bouncy castle 生成的密钥相比,它的大小要大得多。

有没有办法在充气城堡中生成更大的密钥?

我尝试更改密钥位大小,但收到一条错误消息:InvalidParameterException:未知密钥大小。

BouncyCastle 生成的密钥:

3059301306072A8648CE3D020106082A8648CE3D03010703420004272F71C1D8B3DC0A7FCB1E9650EEF64EA8F639BEC97D49F8848455C2F5869F7324332D188129C84727F834EE7EE7D8EB7DFC8D40CD4ED219A4FBCEF6C15200F3

ECDiffieHellmanCng 生成的密钥:

45434B35420000000055CC8665A66A7CDF2E9BF7C69A25B322C72CDBDB1EA8F348050B0A7CF32F9AAD890EA513583367977D5157B2F7FBF55661C9AE2DBAF09B1DC1EA8F193688C3C09501BEE326867ABCB41CA1029F66AF888649F0A6C0674D19670CF32461BA7B3867C1623D68829A7A9A7F1CFC6F5DB99E13C8D960AEF6F5CDAB5B3B62ED6CBEC7222C9F

这是生成充气城堡密钥的代码:

    const string Algorithm = "ECDH";
    const int KeyBitSize = 256;
    const int NonceBitSize = 128;
    const int MacBitSize = 128;
    const int DefaultPrimeProbability = 30;

    IAsymmetricCipherKeyPairGenerator aliceKeyGen = GeneratorUtilities.GetKeyPairGenerator(Algorithm);
    DHParametersGenerator aliceGenerator = new DHParametersGenerator();
    aliceGenerator.Init(KeyBitSize, DefaultPrimeProbability, new SecureRandom());
    DHParameters aliceParameters = aliceGenerator.GenerateParameters();
    KeyGenerationParameters aliceKGP = new DHKeyGenerationParameters(new SecureRandom(), aliceParameters);
    aliceKeyGen.Init(aliceKGP);

    AsymmetricCipherKeyPair aliceKeyPair = aliceKeyGen.GenerateKeyPair();
    IBasicAgreement aliceKeyAgree = AgreementUtilities.GetBasicAgreement(Algorithm);
    aliceKeyAgree.Init(aliceKeyPair.Private);

    SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(aliceKeyPair.Public);
    byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
    string serializedPublic = AsString(serializedPublicBytes);

public static string AsString(byte[] bytes, bool keepDashes = false)
    {
        string hex = BitConverter.ToString(bytes);
        return (keepDashes ? hex : hex.Replace("-", ""));
    }

我也尝试了 Mentalis.org DH 库,它给了我一个更大的密钥,但还是太短了。

// create a new DH instance
DiffieHellman dh1 = new DiffieHellmanManaged();
// generate the public key of the first DH instance        
byte[] ke1 = dh1.CreateKeyExchange();
string publicKeyString = AsString(ke1);

来自 mentalis.org 库的密钥:

5F4542F9A8F5636ECCBBAC38238C97ABE757B8F65E25B181BCF41C58985E699EFD6B9606B99F7074717E83F7AC1B5E97DFF6DBA94876F74645F25F0D7FAA1528898C1BD0BB568DF15A98724093766B213769893A05B47E40410B0F395C834F68F57B2EE01852895D912C1D56675A7D8C5367B5E06DE08AAA18CBB4C69F3AE142

如果您要decode BouncyCastle 版本,您会发现它是

30 59
SEQUENCE
  30 13
  SEQUENCE
    06 07 2A 86 48 CE 3D 02 01
    OBJECT IDENTIFIER 1.2.840.10045.2.1 (id-ecPublicKey)
    06 08 2A 86 48 CE 3D 03 01 07
    OBJECT IDENTIFIER 1.2.840.10045.3.1.7 (id-secp256r1)
  03 42 00
  BIT STRING
    04 27 2F 71 C1 D8 B3 DC 0A 7F CB 1E 96 50 EE F6
    4E A8 F6 39 BE C9 7D 49 F8 84 84 55 C2 F5 86 9F
    73 24 33 2D 18 81 29 C8 47 27 F8 34 EE 7E E7 D8
    EB 7D FC 8D 40 CD 4E D2 19 A4 FB CE F6 C1 52 00
    F3

BIT STRING的有效载荷是一个ecPublicKey的编码值,其曲线是secp256r1。

然后,根据 SEC-1 paper 的 2.3.3 Elliptic-Curve-Point-to-Octet-String Conversion 我们看到它被编码为

04
Uncompressed Point
  X = 27 2F 71 C1 D8 B3 DC 0A 7F CB 1E 96 50 EE F6 4E
      A8 F6 39 BE C9 7D 49 F8 84 84 55 C2 F5 86 9F 73
  Y = 24 33 2D 18 81 29 C8 47 27 F8 34 EE 7E E7 D8 EB
      7D FC 8D 40 CD 4E D2 19 A4 FB CE F6 C1 52 00 F3

遵循 .NET Core import/export ECC feature we see that the equivalent CNG blob 的逻辑是

// BCRYPT_ECDH_PUBLIC_P256_MAGIC (little-endian)
45 43 B4 31
// cbKey=(DWORD)32 (little-endian)
20 00 00 00
// The X bytes (big-endian):
27 2F 71 C1 D8 B3 DC 0A 7F CB 1E 96 50 EE F6 4E
A8 F6 39 BE C9 7D 49 F8 84 84 55 C2 F5 86 9F 73
// The Y bytes (big-endian):
27 2F 71 C1 D8 B3 DC 0A 7F CB 1E 96 50 EE F6 4E
A8 F6 39 BE C9 7D 49 F8 84 84 55 C2 F5 86 9F 73