gpt4 book ai didi

c - 使用 System.security.cryptography 将使用 OpenSSL 进行公钥身份验证的 C 代码转换为 VisualBasic.NET?

转载 作者:太空宇宙 更新时间:2023-11-03 23:36:35 25 4
gpt4 key购买 nike

我正在尝试使用 .net 3.5 世界中的 system.security.crytography 库来模仿一些使用 OpenSSL 库的 C 代码的结果,但我似乎无法做到正确。我需要一些帮助...部分问题是我对密码学的总体理解。

这是应该发生的事情:

  1. 我向设备发送身份验证请求。
  2. 它返回一个挑战摘要,然后我需要使用已知 key 对其进行签名并返回
  3. 设备返回“成功”或“失败”消息。

我有以下要“复制”的代码片段:

  //Seed the PRNG
//Cheating here - the PRNG will be seeded when we create a key pair
//The key pair is discarded only doing this to seed the PRNG.
DSA *temp_dsa = DSA_new();
if(!temp_dsa)
{
printf("Error: The client had an error with the DSA API\n");
exit(0);
}

unsigned char seed[20] = "Our Super Secret Key";
temp_dsa = DSA_generate_parameters(128, seed, sizeof(seed), NULL, NULL, NULL, NULL);
DSA_free(temp_dsa);


//A pointer to the private key.
p = (unsigned char *)&priv_key;

//Create and allocate a DSA structure from the private key.
DSA *priv_dsa = NULL;
priv_dsa = d2i_DSAPrivateKey(NULL, &p, sizeof(priv_key));
if(!priv_dsa)
{
printf("Error: The client had an error with the DSA API\n");
exit(0);
}


//Allocate memory for the to be computed signature.
sigret = OPENSSL_malloc(DSA_size(priv_dsa));

//Sign the challenge digest recieved from the ISC.
retval = DSA_sign(0, pResp->data, pResp->data_length, sigret, &siglen, priv_dsa);

更多信息:

  1. priv_key 是一个包含 252 个元素的十六进制字符数组。

  2. 最终结果是一个 512(或更少)字符数组发送回设备进行验证。

Rasmus 要求查看 key 数组。在这里:

unsigned char priv_key[] = {0x30, 0x81, 0xf9, 0x02, 0x01, 0x00,
0x02, 0x41, 0x00, 0xfe, 0xca,
0x97, 0x55, 0x1f, 0xc0, 0xb7,
0x1f, 0xad, 0xf0, 0x93, 0xec,
0x4b, 0x31, 0x94, 0x78, 0x86,
0x82, 0x1b, 0x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x10, 0x88,
0x0d, 0x2f, 0x15, 0x4b, 0xbe,
0xba, 0x13, 0x23, 0xa1, 0x73,
0xa3, 0x04, 0x37, 0xc9, 0x02,
0x14, 0x06, 0x8e, 0xc1, 0x41,
0x40, 0xf1, 0xf6, 0xe1, 0xfa,
0xfb, 0x64, 0x28, 0x02, 0x15,
0xce, 0x47, 0xaa, 0xce, 0x6e,
0xfe};

谁能帮我把这段代码翻译成它的 VB.net 加密等价物?

最佳答案

抱歉,下面的代码不起作用。 .NET 需要 DSAParameters 中的附加值。我添加了关于 BouncyCaSTLe.NET 的另一个建议的评论。


抱歉,我不会写 VB.NET,但您或许可以翻译以下 C# 代码:

DSAParameters keyValue = new DSAParameters();
keyValue.P = new byte[]{0xfe, 0xca, 0x97, 0x55, 0x1f, 0xc0, 0xb7, 0x1f, 0xad, 0xf0, 0x93, 0xec, 0x4b, 0x31, 0x94, 0x78, 0x86, 0x82, 0x1b, 0xab, 0xc4, 0x9e, 0x5c, 0x40, 0xd9, 0x89, 0x7d, 0xde, 0x43, 0x38, 0x06, 0x4f, 0x1b, 0x2b, 0xef, 0x5c, 0xb7, 0xff, 0x21, 0xb1, 0x11, 0xe6, 0x9a, 0x81, 0x9a, 0x2b, 0xef, 0x3a, 0xbb, 0x5c, 0xea, 0x76, 0xae, 0x3a, 0x8b, 0x92, 0xd2, 0x7c, 0xf1, 0x89, 0x8e, 0x4d, 0x3f, 0x0d};
keyValue.Q = new byte[]{0x88, 0x16, 0x1b, 0xf5, 0xda, 0x43, 0xee, 0x4b, 0x58, 0xbb, 0x93, 0xea, 0x4e, 0x2b, 0xda, 0xb9, 0x17, 0xd1, 0xff, 0x21};
keyValue.G = new byte[]{0xf6, 0xbb, 0x45, 0xea, 0xda, 0x72, 0x39, 0x4f, 0xc1, 0xdd, 0x02, 0xb4, 0xf3, 0xaa, 0xe5, 0xe2, 0x76, 0xc7, 0xdc, 0x34, 0xb2, 0x0a, 0xd8, 0x69, 0x63, 0xc3, 0x40, 0x2c, 0x58, 0xea, 0xa6, 0xbd, 0x24, 0x8b, 0x6b, 0xaa, 0x4b, 0x41, 0xfc, 0x5f, 0x21, 0x02, 0x3c, 0x27, 0xa9, 0xc7, 0x7a, 0xc8, 0x59, 0xcd, 0x5b, 0xdd, 0x6c, 0x44, 0x48, 0x86, 0xd1, 0x34, 0x46, 0xb0, 0x89, 0x55, 0x50, 0x87};
keyValue.X = new byte[]{0x80, 0x29, 0xc6, 0x4a, 0x08, 0x3e, 0x30, 0x54, 0x71, 0x9b, 0x95, 0x49, 0x55, 0x17, 0x70, 0xc7, 0x96, 0x65, 0xc8, 0xc2, 0xe2, 0x8a, 0xe0, 0x5d, 0x9f, 0xe4, 0xb2, 0x1f, 0x20, 0x83, 0x70, 0xbc, 0x88, 0x36, 0x03, 0x29, 0x59, 0xcd, 0xc7, 0xcd, 0xd9, 0x4a, 0xa8, 0x65, 0x24, 0x6a, 0x77, 0x8a, 0x10, 0x88, 0x0d, 0x2f, 0x15, 0x4b, 0xbe, 0xba, 0x13, 0x23, 0xa1, 0x73, 0xa3, 0x04, 0x37, 0xc9};
keyValue.Y = new byte[]{0x06, 0x8e, 0xc1, 0x41, 0x40, 0xf1, 0xf6, 0xe1, 0xfa, 0xfb, 0x64, 0x28, 0x02, 0x15, 0xce, 0x47, 0xaa, 0xce, 0x6e, 0xfe};

using (DSACryptoServiceProvider key = new DSACryptoServiceProvider())
{
key.ImportParameters(keyValue);
byte[] res = key.SignData(data);
}

您的 priv_key 十六进制字符数组是一个 ASN.1 DER 编码的 DSA 私钥(采用 OpenSSL 的非标准格式)。这是一个转储,您可能会看到我从哪里获得值:

   0 30  249: SEQUENCE {
3 02 1: INTEGER 0
6 02 65: INTEGER
: 00 FE CA 97 55 1F C0 B7 1F AD F0 93 EC 4B 31 94
: 78 86 82 1B AB C4 9E 5C 40 D9 89 7D DE 43 38 06
: 4F 1B 2B EF 5C B7 FF 21 B1 11 E6 9A 81 9A 2B EF
: 3A BB 5C EA 76 AE 3A 8B 92 D2 7C F1 89 8E 4D 3F
: 0D
73 02 21: INTEGER
: 00 88 16 1B F5 DA 43 EE 4B 58 BB 93 EA 4E 2B DA
: B9 17 D1 FF 21
96 02 65: INTEGER
: 00 F6 BB 45 EA DA 72 39 4F C1 DD 02 B4 F3 AA E5
: E2 76 C7 DC 34 B2 0A D8 69 63 C3 40 2C 58 EA A6
: BD 24 8B 6B AA 4B 41 FC 5F 21 02 3C 27 A9 C7 7A
: C8 59 CD 5B DD 6C 44 48 86 D1 34 46 B0 89 55 50
: 87
163 02 65: INTEGER
: 00 80 29 C6 4A 08 3E 30 54 71 9B 95 49 55 17 70
: C7 96 65 C8 C2 E2 8A E0 5D 9F E4 B2 1F 20 83 70
: BC 88 36 03 29 59 CD C7 CD D9 4A A8 65 24 6A 77
: 8A 10 88 0D 2F 15 4B BE BA 13 23 A1 73 A3 04 37
: C9
230 02 20: INTEGER
: 06 8E C1 41 40 F1 F6 E1 FA FB 64 28 02 15 CE 47
: AA CE 6E FE
: }

同样,SignData 的输出可能不是您期望的格式。我认为 OpenSSL 将结果放在 ASN.1 DER 容器中。它看起来像这样:

3082[2 bytes for length of remaining data]
0281[1 byte for length of this data][some bytes with first half of signature]
0281[1 byte for length of this data][some bytes with second half of signature]

.NET 只是连接签名的两半。

关于c - 使用 System.security.cryptography 将使用 OpenSSL 进行公钥身份验证的 C 代码转换为 VisualBasic.NET?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1596723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com