- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
因此,当我尝试在 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/
我正在使用 JWS 库来取消签署一些 token ,因此当 token 无效时,我得到了异常 InvalidArgumentException ({ "name": "Exception", "mes
我正在使用 JWS 库来取消签署一些 token ,因此当 token 无效时,我得到了异常 InvalidArgumentException ({ "name": "Exception", "mes
我正在使用 Behat 进行一些自动化测试,并且添加了 Mink 及其 Goutte 驱动程序。我使用的是最新版本的 Behat 和 Mink。 我已将 Mink 扩展添加到功能上下文文件中,当我运行
我正在研究 Restful API,当我想在数据库中播种假数据时出现异常消息。 我更新数据库。 php artisan migrate:fresh 我在数据库上播种。 php artisan db:s
我正在尝试创建一个 Doctrine 实体: php app/console doctrine:generate:entity --entity="AWStudio:Category" --field
请说明如何从下面的函数创建测试用例以测试异常和消息是否正确抛出。我正在使用 Symfony 2。 public function validateParams(Graph $graph, $start
我正在尝试使用以下命令从 composer.json 文件安装 symfony 2.4.1: php composer.phar install 我从包含 composer.json 文件 (var/
我将项目从本地主机上传到我的专用服务器,在遇到很多问题之后,终于有一些页面可以在 domain.com 上运行 |域名.com/home | domain.com/allsites 等.. 但是现在,
目标 像在 Java 或类似语言中那样在 JavaScript 方法中抛出 InvalidArgumentException。 背景 我一直在尝试熟悉 JavaSctipt 错误处理,我知道我可以使用
我在 packagist 中为 Composer 准备的第一个包。我实际上是为我自己的项目编写的,但我不能在任何地方使用它! 当我这样做 composer require wearede/tbcpay
在我正在构建的框架中,我正在努力使我的代码更具可测试性,因为我之前沉迷于 MVC+Singleton 模式并且拥有大量的静态类。从那时起,我开始更多地了解单元测试和 TDD,这促使我重构了很多代码。这
我应该什么时候使用 InvalidArgumentException什么时候UnexpectedValueException ?它们在我看来是一样的。 Note一个扩展了 LogicException
我正在尝试获取 HTTP 状态码 用 Python 用 selenium webdriver 打开一个网站后,我看到了 HTTP 状态码 不可能,所以唯一的方法是获取网络日志并获取 HTTP 状态码
我正在开发一个需要与驱动器/电子表格交互的 android 应用程序。我正在使用 google drive for android API,但它不仅太复杂而无法弄清楚,而且它从来没有正常工作。所以我切
嗨,我尝试了在 Google 上找到的所有内容,但没有任何效果,当我尝试在数据库中插入日期时,我总是出现错误跟踪数据。我希望有一个人可以帮助我。非常感谢,并对我的英语感到抱歉。 //这是我的模型位置
我正在使用 VisualStudio 12 开发一个应用程序。在那个应用程序中我需要做一些 mySql 调用,我有代码: sql::Statement *stmt = db->global->crea
我刚刚从 bitbucket 克隆了一个 laravel 项目,我正在为我的本地开发人员使用 nginx 服务器。但是当我导航到我的项目的 url(本地)时,我收到了这个错误。 请帮忙! 最佳答案 在
我试图在 symfony 2 应用程序中创建我的第一个服务,但我收到此错误: InvalidArgumentException: There is no extension able to load
我正在使用 official documentation for symfony 2.1用于覆盖默认的 FOSUserBundle 表单。但是当我试图覆盖表单处理程序时,我遇到了这个错误: Inval
我正在尝试解决 Laravel 日程安排的一个奇怪问题: ERROR: exception 'InvalidArgumentException' with message 'Invalid sched
我是一名优秀的程序员,十分优秀!