diff --git a/Crypto_Win/Crypto_Win_vs71.sln b/Crypto_Win/Crypto_Win_vs71.sln
deleted file mode 100644
index f899b19ed..000000000
--- a/Crypto_Win/Crypto_Win_vs71.sln
+++ /dev/null
@@ -1,50 +0,0 @@
-Microsoft Visual Studio Solution File, Format Version 8.00
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Crypto_Win", "Crypto_Win_vs71.vcproj", "{ACE069C0-B8FB-49C2-8D8F-410136C7D332}"
- ProjectSection(ProjectDependencies) = postProject
- EndProjectSection
-EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestSuite", "testsuite\TestSuite_vs71.vcproj", "{C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}"
- ProjectSection(ProjectDependencies) = postProject
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332} = {ACE069C0-B8FB-49C2-8D8F-410136C7D332}
- EndProjectSection
-EndProject
-Global
- GlobalSection(SolutionConfiguration) = preSolution
- debug_shared = debug_shared
- release_shared = release_shared
- debug_static_mt = debug_static_mt
- release_static_mt = release_static_mt
- debug_static_md = debug_static_md
- release_static_md = release_static_md
- EndGlobalSection
- GlobalSection(ProjectConfiguration) = postSolution
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_shared.ActiveCfg = debug_shared|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_shared.Build.0 = debug_shared|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_shared.ActiveCfg = release_shared|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_shared.Build.0 = release_shared|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_static_mt.ActiveCfg = debug_static_mt|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_static_mt.Build.0 = debug_static_mt|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_static_mt.ActiveCfg = release_static_mt|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_static_mt.Build.0 = release_static_mt|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_static_md.ActiveCfg = debug_static_md|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_static_md.Build.0 = debug_static_md|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_static_md.ActiveCfg = release_static_md|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_static_md.Build.0 = release_static_md|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_shared.ActiveCfg = debug_shared|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_shared.Build.0 = debug_shared|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_shared.ActiveCfg = release_shared|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_shared.Build.0 = release_shared|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_static_mt.ActiveCfg = debug_static_mt|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_static_mt.Build.0 = debug_static_mt|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_static_mt.ActiveCfg = release_static_mt|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_static_mt.Build.0 = release_static_mt|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_static_md.ActiveCfg = debug_static_md|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_static_md.Build.0 = debug_static_md|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_static_md.ActiveCfg = release_static_md|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_static_md.Build.0 = release_static_md|Win32
- EndGlobalSection
- GlobalSection(ExtensibilityGlobals) = postSolution
- EndGlobalSection
- GlobalSection(ExtensibilityAddIns) = postSolution
- EndGlobalSection
-EndGlobal
diff --git a/Crypto_Win/Crypto_Win_vs71.vcproj b/Crypto_Win/Crypto_Win_vs71.vcproj
deleted file mode 100644
index 64db4b7df..000000000
--- a/Crypto_Win/Crypto_Win_vs71.vcproj
+++ /dev/null
@@ -1,515 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/Crypto_Win/Crypto_Win_vs80.sln b/Crypto_Win/Crypto_Win_vs80.sln
deleted file mode 100644
index d88131f5e..000000000
--- a/Crypto_Win/Crypto_Win_vs80.sln
+++ /dev/null
@@ -1,60 +0,0 @@
-Microsoft Visual Studio Solution File, Format Version 9.00
-# Visual Studio 2005
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Crypto_Win", "Crypto_Win_vs80.vcproj", "{ACE069C0-B8FB-49C2-8D8F-410136C7D332}"
-EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestSuite", "testsuite\TestSuite_vs80.vcproj", "{C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}"
- ProjectSection(ProjectDependencies) = postProject
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332} = {ACE069C0-B8FB-49C2-8D8F-410136C7D332}
- EndProjectSection
-EndProject
-Global
- GlobalSection(SolutionConfigurationPlatforms) = preSolution
- debug_shared|Win32 = debug_shared|Win32
- release_shared|Win32 = release_shared|Win32
- debug_static_mt|Win32 = debug_static_mt|Win32
- release_static_mt|Win32 = release_static_mt|Win32
- debug_static_md|Win32 = debug_static_md|Win32
- release_static_md|Win32 = release_static_md|Win32
- EndGlobalSection
- GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_shared|Win32.ActiveCfg = debug_shared|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_shared|Win32.Build.0 = debug_shared|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_shared|Win32.Deploy.0 = debug_shared|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_shared|Win32.ActiveCfg = release_shared|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_shared|Win32.Build.0 = release_shared|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_shared|Win32.Deploy.0 = release_shared|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_static_mt|Win32.ActiveCfg = debug_static_mt|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_static_mt|Win32.Build.0 = debug_static_mt|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_static_mt|Win32.Deploy.0 = debug_static_mt|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_static_mt|Win32.ActiveCfg = release_static_mt|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_static_mt|Win32.Build.0 = release_static_mt|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_static_mt|Win32.Deploy.0 = release_static_mt|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_static_md|Win32.ActiveCfg = debug_static_md|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_static_md|Win32.Build.0 = debug_static_md|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.debug_static_md|Win32.Deploy.0 = debug_static_md|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_static_md|Win32.ActiveCfg = release_static_md|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_static_md|Win32.Build.0 = release_static_md|Win32
- {ACE069C0-B8FB-49C2-8D8F-410136C7D332}.release_static_md|Win32.Deploy.0 = release_static_md|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_shared|Win32.ActiveCfg = debug_shared|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_shared|Win32.Build.0 = debug_shared|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_shared|Win32.Deploy.0 = debug_shared|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_shared|Win32.ActiveCfg = release_shared|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_shared|Win32.Build.0 = release_shared|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_shared|Win32.Deploy.0 = release_shared|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_static_mt|Win32.ActiveCfg = debug_static_mt|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_static_mt|Win32.Build.0 = debug_static_mt|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_static_mt|Win32.Deploy.0 = debug_static_mt|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_static_mt|Win32.ActiveCfg = release_static_mt|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_static_mt|Win32.Build.0 = release_static_mt|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_static_mt|Win32.Deploy.0 = release_static_mt|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_static_md|Win32.ActiveCfg = debug_static_md|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_static_md|Win32.Build.0 = debug_static_md|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.debug_static_md|Win32.Deploy.0 = debug_static_md|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_static_md|Win32.ActiveCfg = release_static_md|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_static_md|Win32.Build.0 = release_static_md|Win32
- {C1B1BB96-5198-48EB-AB48-9A0A0B54FB15}.release_static_md|Win32.Deploy.0 = release_static_md|Win32
- EndGlobalSection
- GlobalSection(SolutionProperties) = preSolution
- HideSolutionNode = FALSE
- EndGlobalSection
-EndGlobal
diff --git a/Crypto_Win/Crypto_Win_vs80.vcproj b/Crypto_Win/Crypto_Win_vs80.vcproj
deleted file mode 100644
index 84ebba084..000000000
--- a/Crypto_Win/Crypto_Win_vs80.vcproj
+++ /dev/null
@@ -1,536 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/Crypto_Win/src/RSACipherImpl.cpp b/Crypto_Win/src/RSACipherImpl.cpp
index 6cfc7377e..ff6ab5c99 100644
--- a/Crypto_Win/src/RSACipherImpl.cpp
+++ b/Crypto_Win/src/RSACipherImpl.cpp
@@ -114,15 +114,14 @@ namespace
DWORD RSAEncryptImpl::encrypt(unsigned char* output, std::streamsize outputLength, BOOL isFinal)
{
DWORD flags = _paddingMode == RSA_PADDING_PKCS1_OAEP ? CRYPT_OAEP : 0;
- DWORD n = static_cast(_pos + 1);
+ DWORD n = static_cast(_pos);
if (!CryptEncrypt(_pKey->publicKey(), NULL, isFinal, flags, NULL, &n, 0))
error("RSACipher cannot obtain length of encrypted data");
poco_assert(n > _pos);
- //poco_assert_dbg(n <= maxDataSize());
std::vector data(n);
- n = static_cast(_pos + 1);
+ n = static_cast(_pos /*+ 1*/);
std::memcpy(&data[0], _pBuf, n);
- if (!CryptEncrypt(_pKey->publicKey(), NULL, isFinal, flags, &data[0], &n, data.size()))
+ if (!CryptEncrypt(_pKey->publicKey(), NULL, isFinal, flags, &data[0], &n, static_cast(data.size())))
error("RSACipher cannot encrypt data");
poco_assert(n <= outputLength);
std::memcpy(output, &data[0], n);
@@ -235,7 +234,7 @@ namespace
DWORD RSADecryptImpl::decrypt(unsigned char* output, std::streamsize outputLength, BOOL isFinal)
{
- DWORD n = static_cast(_pos + 1);
+ DWORD n = static_cast(_pos);
DWORD flags = _paddingMode == RSA_PADDING_PKCS1_OAEP ? CRYPT_OAEP : 0;
std::vector data(n);
std::memcpy(&data[0], _pBuf, n);
diff --git a/Crypto_Win/src/RSAKeyImpl.cpp b/Crypto_Win/src/RSAKeyImpl.cpp
index 43b47c1c2..1701e899b 100644
--- a/Crypto_Win/src/RSAKeyImpl.cpp
+++ b/Crypto_Win/src/RSAKeyImpl.cpp
@@ -56,7 +56,7 @@ RSAKeyImpl::RSAKeyImpl(int keyLength, unsigned long):
{
DWORD flags = keyLength << 16;
flags |= CRYPT_EXPORTABLE;
- if (!CryptGenKey(_sp.handle(), AT_SIGNATURE, flags, &_hPrivateKey))
+ if (!CryptGenKey(_sp.handle(), AT_KEYEXCHANGE, flags, &_hPrivateKey))
{
error("Cannot generate RSA key pair");
}
@@ -121,13 +121,13 @@ void RSAKeyImpl::extractPublicKey()
DWORD size = 0;
DWORD rc = CryptExportPublicKeyInfo(_sp.handle(),
- AT_SIGNATURE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
+ AT_KEYEXCHANGE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
NULL, &size);
if (rc)
{
keyBuffer.resize(size);
rc = CryptExportPublicKeyInfo(_sp.handle(),
- AT_SIGNATURE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
+ AT_KEYEXCHANGE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
reinterpret_cast(keyBuffer.begin()), &size);
if (rc)
{
diff --git a/Crypto_Win/testsuite/src/RSATest.cpp b/Crypto_Win/testsuite/src/RSATest.cpp
index 28a9c589f..63356fa1f 100644
--- a/Crypto_Win/testsuite/src/RSATest.cpp
+++ b/Crypto_Win/testsuite/src/RSATest.cpp
@@ -199,7 +199,6 @@ void RSATest::testRSACipher()
void RSATest::testRSACipherLarge()
{
- /*TODO
std::vector sizes;
sizes.push_back (2047);
sizes.push_back (2048);
@@ -222,7 +221,6 @@ void RSATest::testRSACipherLarge()
std::string dec = pCipher->decryptString(enc);
assert (dec == val);
}
- */
}
@@ -264,13 +262,14 @@ CppUnit::Test* RSATest::suite()
CppUnit_addTest(pSuite, RSATest, testRSACipher);
CppUnit_addTest(pSuite, RSATest, testRSACipherLarge);
CppUnit_addTest(pSuite, RSATest, testCertificate);
- CppUnit_addTest(pSuite, RSATest, testCryptoAPI);
+ //CppUnit_addTest(pSuite, RSATest, testCAPIEncryptDecrypt);
+ //CppUnit_addTest(pSuite, RSATest, testCAPIKeys);
return pSuite;
}
-void RSATest::testCryptoAPI()
+void RSATest::testCAPIKeys()
{
HCRYPTPROV hProv = 0;
BOOL rc = FALSE;
@@ -307,21 +306,15 @@ void RSATest::testCryptoAPI()
rc = CryptGenKey(hProv, AT_SIGNATURE, flags, &hPrivateKey);
if (!rc) goto bad;
- /* extract public key */
- rc = CryptExportPublicKeyInfo(hProv,
- AT_SIGNATURE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
- NULL, &size);
+ /* extport public key */
+ rc = CryptExportKey(hPrivateKey, NULL, PUBLICKEYBLOB, 0, NULL, &size);
if (!rc) goto bad;
- pKeyInfo = (PCERT_PUBLIC_KEY_INFO)LocalAlloc(0, size);
- rc = CryptExportPublicKeyInfo(hProv,
- AT_SIGNATURE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
- pKeyInfo, &size);
+ pPubKeyBLOB = (LPBYTE)LocalAlloc(0, size);
+ rc = CryptExportKey(hPrivateKey, NULL, PUBLICKEYBLOB, 0, pPubKeyBLOB, &size);
if (!rc) goto bad;
- rc = CryptImportPublicKeyInfo(hProv,
- X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
- pKeyInfo, &hPublicKey);
+ rc = CryptImportKey(hProv, pPubKeyBLOB, size, 0, CRYPT_EXPORTABLE, &hPublicKey);
if (!rc) goto bad;
/* export private key */
@@ -395,7 +388,6 @@ void RSATest::testCryptoAPI()
rc = CryptBinaryToStringA(pDER, encSize,
CRYPT_STRING_BASE64HEADER, pPEM, &pemSize);
if (!rc) goto bad;
- printf("\r\n%s\r\n", pPEM);
/*************************************************************/
/* start fresh and import both keys from the private key PEM */
@@ -471,25 +463,23 @@ void RSATest::testCryptoAPI()
if (!rc) goto bad;
/* import public key */
- /* Error: "Key does not exist"
rc = CryptExportPublicKeyInfo(hProv,
- AT_SIGNATURE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
- NULL, &size);
+ AT_KEYEXCHANGE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
+ NULL, &size);
if (!rc) goto bad;
LocalFree(pKeyInfo);
pKeyInfo = (PCERT_PUBLIC_KEY_INFO)LocalAlloc(0, size);
rc = CryptExportPublicKeyInfo(hProv,
- AT_SIGNATURE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
- pKeyInfo, &size);
+ AT_KEYEXCHANGE, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
+ pKeyInfo, &size);
if (!rc) goto bad;
hPublicKey = 0;
rc = CryptImportPublicKeyInfo(hProv,
- X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
- pKeyInfo, &hPublicKey);
+ X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
+ pKeyInfo, &hPublicKey);
if (!rc) goto bad;
- */
/* clean up */
if (hPublicKey) CryptDestroyKey(hPublicKey);
@@ -526,4 +516,97 @@ bad:
LocalFree(pPubKeyBLOB);
LocalFree(pKeyInfo);
LocalFree(pIV);
+}
+
+
+void RSATest::testCAPIEncryptDecrypt()
+{
+ HCRYPTPROV hProv = 0;
+ BOOL rc = FALSE;
+ LPBYTE pPrivKeyBLOB = 0;
+ LPBYTE pPubKeyBLOB = 0;
+ HCRYPTKEY hSessionKey = 0;
+ HCRYPTKEY hPrivateKey = 0;
+ DWORD flags = 0;
+ DWORD size = 0;
+ PCERT_PUBLIC_KEY_INFO pKeyInfo = 0;
+ HCRYPTKEY hPublicKey = 0;
+
+ Poco::Buffer data(1);
+ data[0] = 'x';
+
+ /* get provider */
+ rc = CryptAcquireContext(&hProv,
+ NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES,
+ CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
+ if (!rc) goto bad;
+
+ /* generate private key */
+ flags = 1024 /*key length*/ << 16;
+ flags |= CRYPT_EXPORTABLE;
+ rc = CryptGenKey(hProv, AT_KEYEXCHANGE/*AT_SIGNATURE*/, flags, &hSessionKey);
+ if (!rc) goto bad;
+
+ /* extract public key */
+ rc = CryptExportKey(hSessionKey, NULL, PUBLICKEYBLOB, 0, NULL, &size);
+ if (!rc) goto bad;
+
+ pPubKeyBLOB = (LPBYTE)LocalAlloc(0, size);
+ rc = CryptExportKey(hSessionKey, NULL, PUBLICKEYBLOB, 0, pPubKeyBLOB, &size);
+ if (!rc) goto bad;
+
+ rc = CryptImportKey(hProv, pPubKeyBLOB, size, 0, CRYPT_EXPORTABLE, &hPublicKey);
+ if (!rc) goto bad;
+
+ /* extract private key */
+ rc = CryptExportKey(hSessionKey, NULL, PRIVATEKEYBLOB, 0, NULL, &size);
+ if (!rc) goto bad;
+
+ pPrivKeyBLOB = (LPBYTE)LocalAlloc(0, size);
+ rc = CryptExportKey(hSessionKey, NULL, PRIVATEKEYBLOB, 0, pPrivKeyBLOB, &size);
+ if (!rc) goto bad;
+
+ rc = CryptImportKey(hProv, pPrivKeyBLOB, size, 0, CRYPT_EXPORTABLE, &hPrivateKey);
+ if (!rc) goto bad;
+
+ DWORD dataSize = static_cast(data.size());
+ DWORD n = dataSize;
+ rc = CryptEncrypt(hPublicKey, NULL, TRUE, 0, NULL, &n, 0);
+ if (!rc) goto bad;
+
+ data.resize(n, true);
+ n = dataSize;
+ rc = CryptEncrypt(hPublicKey, NULL, TRUE, 0, data.begin(), &n, static_cast(data.size()));
+ if (!rc) goto bad;
+
+ n = static_cast(data.size());
+ rc = CryptDecrypt(hPrivateKey, NULL, TRUE, 0, data.begin(), &n);
+ if (!rc) goto bad;
+
+ CryptDestroyKey(hPublicKey);
+ CryptDestroyKey(hPrivateKey);
+ CryptReleaseContext(hProv, 0);
+
+ LocalFree(pPubKeyBLOB);
+ LocalFree(pPrivKeyBLOB);
+
+ return;
+
+bad:
+ DWORD errorCode = GetLastError();
+ DWORD dwFlg = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS;
+ LPTSTR lpMsgBuf = 0;
+ FormatMessageA(dwFlg, 0, errorCode, 0, (LPTSTR)& lpMsgBuf, 0, NULL);
+ printf("%s", lpMsgBuf);
+ LocalFree(lpMsgBuf);
+
+ if (hPublicKey) CryptDestroyKey(hPublicKey);
+ if (hPrivateKey) CryptDestroyKey(hPrivateKey);
+ if (hProv) CryptReleaseContext(hProv, 0);
+
+ LocalFree(pPubKeyBLOB);
+ LocalFree(pPrivKeyBLOB);
+
+ return;
}
\ No newline at end of file
diff --git a/Crypto_Win/testsuite/src/RSATest.h b/Crypto_Win/testsuite/src/RSATest.h
index e308e9063..4830761a8 100644
--- a/Crypto_Win/testsuite/src/RSATest.h
+++ b/Crypto_Win/testsuite/src/RSATest.h
@@ -37,7 +37,8 @@ public:
void setUp();
void tearDown();
- void testCryptoAPI();
+ void testCAPIKeys();
+ void testCAPIEncryptDecrypt();
static CppUnit::Test* suite();