我需要在win32中实现类似的解密功能,但是我确实知道如何在UWP中导入会话密钥。
CryptAcquireContext(&hProv, L"DecryptContainer", MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_NEWKEYSET) ;
// private key blob in pbPrvBlob
BYTE *pbPrvBlob;
DWORD cbPrvBlob;
// Convert the private key file bytes into an HCRYPTKEY.
HCRYPTKEY hKey;
hr = CryptImportKey(hProv, pbPrvBlob, cbPrvBlob, 0, 0, &hKey);
// Convert the encrypted session key file bytes into an HCRYPTKEY.
BYTE *pbSymBlob; // contain encrypted session key blob
DWORD cbSymBlob;
HCRYPTKEY hSymKey;
CryptImportKey(hProv, pbSymBlob, cbSymBlob, hKey, 0, &hSymKey);
我在UWP中尝试此代码,这些API不支持RSA公钥交换算法blob文件。因此,我得到了加密的数据,并尝试使用RSA私有密钥解密。解密功能失败。如何导入RSA公钥交换算法blob文件?
void test_load_keys()
{
byte[] PrvBlob = await ReadFileToByteArray("prvkey.blob");
byte[] Cipher = await ReadFileToByteArray("userdata.blob");
byte[] SymBlob = await ReadFileToByteArray("sessionkey.blob");
IBuffer PrvBlobBuffer = PrvBlob.AsBuffer();
AsymmetricKeyAlgorithmProvider asymmAlg = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
CryptographicKey Prvkey = asymmAlg.ImportKeyPair(PrvBlobBuffer, CryptographicPrivateKeyBlobType.Capi1PrivateKey);
IBuffer sessionblob = SymBlob.AsBuffer();
int offset_start = 12;
int encryed_length = 128;
int count = (SymBlob.Length - offset_start) / encryed_length;
IBuffer EnSessionKey = SymBlob.AsBuffer(offset_start, encryed_length);
Debug.WriteLine(BitConverter.ToString(EnSessionKey.ToArray()));
// Failed nex line with "Value does not fall within the expected range"
IBuffer keydeBlobSlic = CryptographicEngine.Decrypt(Prvkey, EnSessionKey, null);
Debug.WriteLine(BitConverter.ToString(keydeBlobSlic.ToArray()));
SymmetricKeyAlgorithmProvider symAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbc);
CryptographicKey key = symAlg.CreateSymmetricKey(keydeBlobSlic);
// ...
}