ECIES cross-platform implementation with C#

781 Views Asked by At

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://learn.microsoft.com/en-US/dotnet/api/system.security.cryptography.cngkey?View=net-6.0 only works on Windows systems. What can I do in this case to be able to use ECIES on different platforms and not waste time writing my own implementation? I also looked for an implementation with BouncyCastle but found nothing.

1

There are 1 best solutions below

2
On BEST ANSWER

Here is the UnitTest from BouncyCastle with ECIES implementation

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