#include "stdafx.h"
#include <string>
#include <atlbase.h>
#include <comdef.h>
#import "XMLCrypto.dll" no_namespace//raw_interfaces_only
// this is our sample document
const TCHAR* szPaymentDocument = "<PaymentInfo xmlns='http://sqldata.com/xmlcrypto' Id='PayId'>\
<Name>John Smith</Name>\
<CreditCard Currency='USD' Limit='5,000' Id='CC'>\
<Number>4019 2445 0277 5567</Number>\
<Issuer>Bank of the Internet</Issuer>\
<Expiration Time='04/02'/>\
</CreditCard>\
<Account Id='ACC'>\
<AccountId>John Doe</AccountId>\
<Password>My Secret Password</Password>\
</Account>\
</PaymentInfo>";
void SignXMLDocument()
{
HRESULT hr;
IXMLCryptoPtr pXMLCrypto;
if(SUCCEEDED(hr=pXMLCrypto.CreateInstance("SQLData.XMLCrypto")))
{
// use CryptoTester's private key. The key is in the KEYS directory
pXMLCrypto->SetKeyInfo("CryptoTester", "", "");
// sign the whole payment document.
_bstr_t bstrSigned = pXMLCrypto->SignData(szPaymentDocument, "#PayId", "");
if(bstrSigned.length()>0)
{
printf("The signed document is : \n%s\n", (const TCHAR*)bstrSigned);
// clear the object state
pXMLCrypto->Clear();
// now we can verify the signature
if(pXMLCrypto->VerifyData(bstrSigned)==VARIANT_TRUE)
printf("The Signature is valid.\n");
else
printf("The Signature is invalid.\n");
}
}
}
void EncryptXMLDocument()
{
HRESULT hr;
IXMLCryptoPtr pXMLCrypto;
if(SUCCEEDED(hr=pXMLCrypto.CreateInstance("SQLData.XMLCrypto")))
{
// use CryptoTester's public key.
pXMLCrypto->SetKeyInfo("CryptoTester", "", "");
// encrypt the whole document
_bstr_t bstrEncrypted = pXMLCrypto->EncryptData(szPaymentDocument, "", "", "");
if(bstrEncrypted.length()>0)
{
printf("The encrypted document is : \n%s\n", (const TCHAR*)bstrEncrypted);
// clear the object state
pXMLCrypto->Clear();
// now we can verify the signature
_bstr_t bstrDecrypted = pXMLCrypto->DecryptData(bstrEncrypted, "");
printf("The decrypted document is\n%s.\n", (const TCHAR*)bstrDecrypted);
}
}
}
int main(int argc, char* argv[])
{
CoInitialize(NULL);
SignXMLDocument();
EncryptXMLDocument();
CoUninitialize();
return 0;
}