Hi @B D, Anup ,
As documented for NCryptCreatePersistedKey and NCryptOpenKey, those two APIs are checking different things:
-
NTE_EXISTSmeans a persisted container with that name already exists in that scope. -
NTE_BAD_KEYSETmeans the provider could not open it as a usable key in the current context.
So the most likely explanation is that the named key container is still present, but the private key material behind it is no longer usable on the target machine.
For the Microsoft software KSP, persisted keys are file-backed. Microsoft documents the storage locations in Key Storage and Retrieval, including %APPDATA%\Microsoft\Crypto\Keys for user keys and %ProgramData%\Microsoft\Crypto\Keys for shared/machine keys. Microsoft also documents in Key Storage Property Identifiers that NCRYPT_UNIQUE_NAME_PROPERTY returns the provider's backing file name.
This can happen after VM -> physical migration if the key was created in a different user scope, or if the private material is effectively bound to the original machine environment. That risk is even higher if TPM, vTPM, or VBS-backed protection is involved. In other words, the file can still be there even though the target machine cannot use the key.
The first thing to verify is scope. Make sure create/open/import all use the same provider, the same key name, and the same NCRYPT_MACHINE_KEY_FLAG. The Microsoft docs for NCryptCreatePersistedKey and NCryptOpenKey are explicit that if that flag is omitted, the key is user-scoped.
If you need portability, the supported path is export/import, not raw image or file copying. See NCryptExportKey and NCryptImportKey. If the key is non-exportable or hardware-bound, you should treat it as non-portable and generate a new key on the target machine.
About your last observation, physical -> physical can sometimes appear to work simply because the migrated environment stays compatible enough for the provider to keep using the key. It is not a portability guarantee.
Hope this helps! If my answer was helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.