Creating crypto Add/Decrypt components using Visual C + +

Source: Internet
Author: User
Tags decrypt
Brief introduction
This article will teach you how to build a component that can add/decrypt data using Visual C + + and the ATL tools and Microsoft CryptoAPI.
Crypto 101
This article uses microsoft® Cryptographic application Programming Interface (CryptoAPI) hides the bitter and difficult logic algorithm operations, and if you want to know more, see MSDN Library. If you want to know more about cryptography, I recommend you look at this book. Bruce schneier ' s applied cryptography:protocols, Algorithms, and Source code in C
Building components
First, create a new project with ATL COM AppWizard. In this case, I'll name it "Cryptoproj". Select Dynamic Link Library (DLL) in the server type and click Finish to continue.
Defining the interface
Click New ATL Object ... in the Insert menu, select Simple Object, and then press Next.
In the "Names" column, set short name to "Crypto", and other items are filled in below.
In the Attributes column, make sure the Apartment Threading Model is selected, Support isupporterrorinfo option on the hook, and then press OK.
Right-click ICrypto, point "Add Method" plus a way.
Name the method "Encrypt", enter "[in] BSTR bstrplaintext in the parameter bar, [in] BSTR bstrpassword, [out, retval] VARIANT *vciphertext".
Add a method, named "Decrypt", the parameter is "[in] VARIANT vciphertext, [in] BSTR bstrpassword, [out, retval] BSTR *bstrplaintext"
Implementing encryption methods
Need to include CryptoAPI library, in Crypto.cpp head add a sentence: #include <wincrypt.h>
Now it's time to define the various variables we need. STDMETHODIMP Ccrypto::encrypt (BSTR bstrplaintext,
BSTR Bstrpassword,
VARIANT *vciphertext)
{
BYTE *pbdata;
BYTE *pbpassword;
safearray* PSA;
Hcryptprov Hprov = 0;
Hcrypthash Hhash = 0;
Hcryptkey hkey = 0;
DWORD Dwcryptdatalen = 0;
DWORD Dwdatalen = 0;
DWORD dwerror = 0;
Char buffer[200];
Uses_conversion;
Because many CryptoAPI calls are to use the registry, you need to execute a sentence of RevertToSelf (). RevertToSelf ();
Next, we need to convert the input variables into a format that we can use. Dwdatalen = Sysstringlen (Bstrplaintext);
Pbdata = (byte*) ole2a (Bstrplaintext);
Pbpassword = (byte*) ole2a (bstrpassword);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.