简体   繁体   English

使用 C# 的 ECIES 跨平台实施

[英]ECIES cross-platform implementation with C#

I need to use ECIES in my C# project but the only thing I can use is the NuGet Inferno package https://securitydriven.net/inferno/ which uses CngKey which according to https://docs.microsoft.com/en-US/dotnet/api/system.security.cryptography.cngkey?View=net-6.0 only works on Windows systems. I need to use ECIES in my C# project but the only thing I can use is the NuGet Inferno package https://securitydriven.net/inferno/ which uses CngKey which according to https://docs.microsoft.com/en-US /dotnet/api/system.security.cryptography.cngkey?View=net-6.0仅适用于 Windows 系统。 What can I do in this case to be able to use ECIES on different platforms and not waste time writing my own implementation?在这种情况下,我能做些什么才能在不同的平台上使用 ECIES 而不会浪费时间编写自己的实现? I also looked for an implementation with BouncyCastle but found nothing.我还寻找了 BouncyCastle 的实现,但一无所获。

Here is the UnitTest from BouncyCastle with ECIES implementation这是来自 BouncyCastle 的带有 ECIES 实现的 UnitTest

https://github.com/kazuki/opencrypto.net/blob/master/UnitTests/ECIESTest.cs https://github.com/kazuki/opencrypto.net/blob/master/UnitTests/ECIESTest.cs

Code sample代码示例

            ECDomainNames domainName = ECDomainNames.secp160r1;
            ECDomainParameters domain = ECDomains.GetDomainParameter (domainName);
            ECIES ecies = new ECIES (domainName);
            Number V_Private = Number.Parse ("45FB58A92A17AD4B15101C66E74F277E2B460866", 16);
            ECKeyPair pair = new ECKeyPair (V_Private, null, domain);
            pair.CreatePublicKeyFromPrivateKey ();
            ecies.Parameters._Q = pair._Q;
            byte[] M = System.Text.Encoding.ASCII.GetBytes ("abcdefghijklmnopqrst");
            byte[] k = Number.Parse ("702232148019446860144825009548118511996283736794", 10).ToByteArray (20, false);
            byte[] C = ecies.Encrypt (M, k);
            byte[] expectedC = new byte[] {0x02, 0xCE, 0x28, 0x73, 0xE5, 0xBE, 0x44, 0x95, 0x63, 0x39, 0x1F, 0xEB, 0x47, 0xDD, 0xCB, 0xA2, 0xDC, 0x16, 0x37, 0x91, 0x91, 0x71, 0x23, 0xC8, 0x70, 0xA3, 0x1A, 0x81, 0xEA, 0x75, 0x83, 0x29, 0x0D, 0x1B, 0xA1, 0x7B, 0xC8, 0x75, 0x94, 0x35, 0xED, 0x1C, 0xCD, 0xA9, 0xEB, 0x4E, 0xD2, 0x73, 0x60, 0xBE, 0x89, 0x67, 0x29, 0xAD, 0x18, 0x54, 0x93, 0x62, 0x25, 0x91, 0xE5};
            Assert.AreEqual (expectedC, C, "Encryption");

            ecies = new ECIES (domainName);
            ecies.Parameters._d = V_Private;
            byte[] M2 = ecies.Decrypt (C);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM