Example #1
0
        /// <inheritdoc />
        public IECDiffieHellmanPublicKey FromByteArray(byte[] publicKey)
        {
            SafeAlgorithmHandle algorithm;
            var keyBlob = new EccKeyBlob(publicKey);

            switch (keyBlob.Magic)
            {
            case EccKeyBlobMagicNumbers.BCRYPT_ECDH_PUBLIC_P256_MAGIC:
                algorithm = ECDiffieHellmanFactory.BCryptOpenAlgorithmProvider(256);
                break;

            case EccKeyBlobMagicNumbers.BCRYPT_ECDH_PUBLIC_P384_MAGIC:
                algorithm = ECDiffieHellmanFactory.BCryptOpenAlgorithmProvider(384);
                break;

            case EccKeyBlobMagicNumbers.BCRYPT_ECDH_PUBLIC_P521_MAGIC:
                algorithm = ECDiffieHellmanFactory.BCryptOpenAlgorithmProvider(521);
                break;

            default:
                throw new ArgumentException("Unexpected type of key blob.");
            }

            SafeKeyHandle keyHandle;

            keyHandle = BCryptImportKeyPair(
                algorithm,
                AsymmetricKeyBlobTypes.BCRYPT_ECCPUBLIC_BLOB,
                publicKey,
                BCryptImportKeyPairFlags.None);
            algorithm.Dispose();

            return(new ECDiffieHellmanPublicKey(keyHandle));
        }
        /// <inheritdoc />
        public IECDiffieHellmanPublicKey FromByteArray(byte[] publicKey)
        {
            SafeAlgorithmHandle algorithm;
            var keyBlob = new EccKeyBlob(publicKey);
            switch (keyBlob.Magic)
            {
                case EccKeyBlobMagicNumbers.BCRYPT_ECDH_PUBLIC_P256_MAGIC:
                    algorithm = ECDiffieHellmanFactory.BCryptOpenAlgorithmProvider(256);
                    break;
                case EccKeyBlobMagicNumbers.BCRYPT_ECDH_PUBLIC_P384_MAGIC:
                    algorithm = ECDiffieHellmanFactory.BCryptOpenAlgorithmProvider(384);
                    break;
                case EccKeyBlobMagicNumbers.BCRYPT_ECDH_PUBLIC_P521_MAGIC:
                    algorithm = ECDiffieHellmanFactory.BCryptOpenAlgorithmProvider(521);
                    break;
                default:
                    throw new ArgumentException("Unexpected type of key blob.");
            }

            SafeKeyHandle keyHandle;
            keyHandle = BCryptImportKeyPair(
                algorithm,
                AsymmetricKeyBlobTypes.BCRYPT_ECCPUBLIC_BLOB,
                publicKey,
                BCryptImportKeyPairFlags.None);
            algorithm.Dispose();

            return new ECDiffieHellmanPublicKey(keyHandle);
        }