public static void ExportKey(string Path, ECDiffieHellmanPublicKey Key)
 {
     using (BinaryWriter Writer = new BinaryWriter(File.Create(Path)))
     {
         Writer.Write((byte)Key.ToByteArray().Length);
         Writer.Write(Key.ToByteArray());
     }
 }
Пример #2
0
        private ECPublicKeyParameters GetPublicKeyParameters(ECDiffieHellmanPublicKey publicKey)
        {
            byte[] ecdhBlob = publicKey.ToByteArray();

            int keySize;
            string magic = Encoding.ASCII.GetString(ecdhBlob, 0, 4);
            if (magic.Equals("ECK1")) {
                keySize = 256;
            } else if (magic.Equals("ECK3")) {
                keySize = 384;
            } else if (magic.Equals("ECK5")) {
                keySize = 521;
            } else {
                throw new Exception("Unknown public key type");
            }
            if (keySize != _keySize) {
                throw new Exception("Public key size doesn't match our key size");
            }

            byte[] encoded = new byte[1 + ecdhBlob.Length - 8];
            encoded[0] = 0x04;
            Buffer.BlockCopy(ecdhBlob, 8, encoded, 1, ecdhBlob.Length - 8);
            Org.BouncyCastle.Math.EC.ECPoint ecPoint = _domainParameters.Curve.DecodePoint(encoded);
            return new ECPublicKeyParameters(ecPoint, _domainParameters);
        }