void acquirePublicKey(PublicKey publicKey) { // do not load public key again if it is already loaded if (checkPublicKeyIsLoaded()) { return; } switch (publicKey.Oid.Value) { case AlgorithmOids.ECC: keyType = KeyType.EcDsa; break; case AlgorithmOids.RSA: keyType = KeyType.Rsa; break; case AlgorithmOids.DSA: keyType = KeyType.Dsa; break; default: throw new NotSupportedException("Public key algorithm is not supported"); } // regardless of public key algorithm and provider, load public keys to CNG provider for unification // and wider signature formats and padding support. Int32 hresult = NCrypt.NCryptOpenStorageProvider(out SafeNCryptProviderHandle phProvider, MSFT_KSP_NAME, 0); if (hresult != 0) { throw new CryptographicException(hresult); } Byte[] blob = publicKey.GetCryptBlob(); hresult = NCrypt.NCryptImportKey(phProvider, IntPtr.Zero, "PUBLICBLOB", IntPtr.Zero, out phPubKey, blob, blob.Length, 0); if (hresult != 0) { throw new CryptographicException(hresult); } }
public static Int32 GetKeyLength(this PublicKey publicKey) { if (publicKey == null) { throw new ArgumentNullException(nameof(publicKey)); } switch (publicKey.Oid.Value) { case AlgorithmOid.RSA: case AlgorithmOid.DSA: return(publicKey.Key.KeySize); case AlgorithmOid.ECC: var cryptBlob = publicKey.GetCryptBlob(); using (CngKey cngKey = CngKey.Import(cryptBlob, CngKeyBlobFormat.EccPublicBlob)) { return(cngKey.KeySize); } default: return(0); } }