Microsoft CryptoAPI を使用して SHA-1 を計算するコードを書き、コンパイルされた exe が Windows 7、Win Server 2008、Win Server 2003 で動作するようにしました。ただし、Windows XP SP3 で実行すると動作しません。
CryptAcquireContext()
通話の失敗を絞り込みました。
以前の投稿で、XP の " … (Prototype) "という誤った名前について言及していたことに気付きました。これは、WinXP 固有のマクロ MS_ENH_RSA_AES_PROV_XP を使用して説明する必要があります。
XP 固有のコードを変更しましたが、まだ機能しません。( bResult
Win XP では 0 false が返され、他のすべてのプラットフォームでbResult
は 1 true が返されます。)
MS_ENH_RSA_AES_PROV_XP を regedit.exe に表示される実際のキー + 文字列の値でチェックしたので、すべてが機能するように設定されているように見えますが、成功していません。
Windows XP で動作させるために何か見落としていませんか?
問題を説明するために、可能な限り短い例を貼り付けました。VS2010 C++ を使用しました。
// based on examples from http://msdn.microsoft.com/en-us/library/ms867086.aspx
#include "windows.h"
#include "wincrypt.h"
#include <iostream>
#include <iomanip> // for setw()
void main()
{
BOOL bResult;
HCRYPTPROV hProv;
// Attempt to acquire a handle to the default key container.
bResult = CryptAcquireContext(
&hProv, // Variable to hold returned handle.
NULL, // Use default key container.
MS_DEF_PROV, // Use default CSP.
PROV_RSA_FULL, // Type of provider to acquire.
0); // No special action.
std::cout << "line: " << std::setw(4) << __LINE__ << "; " << "bResult = " << bResult << std::endl;
if (! bResult) { // try Windows XP provider name
bResult = CryptAcquireContext(
&hProv, // Variable to hold returned handle.
NULL, // Use default key container.
MS_ENH_RSA_AES_PROV_XP, // Windows XP specific instead of using default CSP.
PROV_RSA_AES, // Type of provider to acquire.
0); // No special action.
std::cout << "line: " << std::setw(4) << __LINE__ << "; " << "bResult = " << bResult << std::endl;
}
if (bResult)
CryptReleaseContext(hProv, 0);
}
Windows 7 の成功:
Windows XP の障害: