Fix the dwDataLen parameter of CryptImportKey(). According to the documentation (or rather, the example code) this parameter should specify the size of the entire structure, including the key material. sizeof(data) (4 or 8 bytes, depending on the architecture) is incorrect in any case however.