c++ - WinRT CryptographicEngine::加密/解密抛出 InvalidArgumentException

标签 c++ encryption windows-runtime

因此,当我尝试在 WinRT 中使用新的加密命名空间时遇到了一个有趣的问题 (Windows::Security::Cryptography)。也就是说,当我尝试使用 CryptographicEngine::Encrypt()CryptographicEngine::Decrypt() 方法时,它们会抛出 InvalidArgumentException。我绝不是这方面的专家,但我觉得我已经将其缩小到一个相当基本的场景,但仍然失败:

//------------------------------------------------------------------------------
// TestEncryptDecrypt
// Simple test that encrypts a string, then decrypts it and compares the result.
void TestEncryptDecrypt()
{
    // Select asymmetric algorithm
    Platform::String^ strAlgorithm = Windows::Security::Cryptography::Core::AsymmetricAlgorithmNames::RsaOaepSha512;
    Windows::Security::Cryptography::Core::AsymmetricKeyAlgorithmProvider^ spAlgorithm = Windows::Security::Cryptography::Core::AsymmetricKeyAlgorithmProvider::OpenAlgorithm(strAlgorithm);

    // Create public/private keys
    unsigned int nKeySize = 512;
    Windows::Security::Cryptography::Core::CryptographicKey^ spKeyPair = spAlgorithm->CreateKeyPair(nKeySize);

    // Message to encrypt/decrypt
    Platform::String^ strMessage = L"Test Message";
    Windows::Storage::Streams::IBuffer^ spMessageBuffer = Windows::Security::Cryptography::CryptographicBuffer::ConvertStringToBinary(strMessage, Windows::Security::Cryptography::BinaryStringEncoding::Utf8);

    // Encrypt the data
    // *** InvalidArgumentException throw here ***
    Windows::Storage::Streams::IBuffer^ spEncryptedBuffer = Windows::Security::Cryptography::Core::CryptographicEngine::Encrypt(spKeyPair, spMessageBuffer, nullptr /*Initialization vector not used with asymmetric algorithms.*/);

    // Decrypt the data
    Windows::Storage::Streams::IBuffer^ spUnencryptedBuffer = Windows::Security::Cryptography::Core::CryptographicEngine::Decrypt(spKeyPair, spEncryptedBuffer, nullptr /*Initialization vector not used with asymmetric algorithms.*/);

    // Retrieve the original message
    Platform::String^ strUnencryptedMessage = Windows::Security::Cryptography::CryptographicBuffer::ConvertBinaryToString(Windows::Security::Cryptography::BinaryStringEncoding::Utf8, spUnencryptedBuffer);
    Assert(strUnencryptedMessage == strMessage);
}

我可能仍然在做一些愚蠢的事情(而且可能是),但不幸的是我没有看到它......有什么想法吗?

提前致谢! :)

最佳答案

和往常一样,我在发布后大约半小时偶然发现了答案......:)

事实证明,RSA_OAEP_SHA512 不支持我使用的 key 大小。您需要使用更大的 key (我实际上在切换到 512 之前尝试过 1024,但似乎也太小了)。不过,使用 2048 或 4096 的 key 大小可以正常工作。

无论如何,我通过使用 WinRT Crypto 示例解决了这个问题,可以在 http://code.msdn.microsoft.com/windowsapps/CryptoWinRT-54ff3d9f 找到该示例.它也会抛出异常,但附近有一些错误处理代码,指示在捕获相当无用的异常后发生了什么。该示例写得非常好,所以如果您遇到类似问题,我建议您检查一下。

另外,我确实发现在创建 key 时没有抛出异常有点烦人,因为获取 InvalidArgumentException 已经足够神秘了,而不是在一个有趣的地方抛出它。似乎可以在 key 创建期间抛出它,因为此时已经选择了算法。

无论如何,问题已解决,希望这篇文章能帮助其他人!

关于c++ - WinRT CryptographicEngine::加密/解密抛出 InvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12852352/

相关文章:

c++ - double C++

c++ - QListWidget 自动移动到列表末尾

c++ - 在链接之前修改 visual studio 生成的程序集

c# - backgroundTaskHost.exe 退出,代码为 1 (0x1)

c++ - 是什么导致了这种奇怪的行为?

C# 中的 Java AES/CBC/PKCS5Padding

encryption - 禁用弱 TLS 密码套件

javascript - 在JavaScript中使用加密技术有哪些合理的方案?

c# - 什么可以解释通过 WinRT 在 UDP 多播中的这种行为?

winforms - Windows 8 密码框占位符