SOL9 2.0 Sample: MsgEncDec

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * MsgEncDec.cpp 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/03/18


#include <sol/crypt/CryptServiceProvider.h>
#include <sol/crypt/CertSystemStore.h>
#include <sol/crypt/CertContext.h>
#include <sol/crypt/CertSelectionDialog.h>
#include <sol/crypt/CryptMsg.h>

void _tmain(int argc, TCHAR** argv)
{
    try {
        const TCHAR* orgFileName = _T(".\\sample_org.txt");    //This shall exist in this currentfolder.

        const TCHAR* encFileName = _T(".\\sample_enc.bin");
        const TCHAR* decFileName = _T(".\\sample_dec.txt");

        if (GetFileAttributes(orgFileName) == 0xffffffff) {
            _tprintf(_T("Error: Not found a sample data file %s\n"), orgFileName);
            return;
        }
        //A file orgFileName is encoded to a file of encFileName by CrytMsg.encodeStream()
        //The the of encFileName is decoded to a file of decFileName by CryptMsg.decodeStream().

        CryptServiceProvider csp;

        const TCHAR* CA = _T("CA");
        CertSystemStore ca(csp, CA);

        _tprintf(_T("1 Try to select a certificate of %s\n"), CA);

        CertSelectionDialog certSelDlg(ca);
    
        //Select a certificate to use encode the file of orgFileName.
        //PCCERT_CONTEXT pContext = certSelDlg.popup();
        PCCERT_CONTEXT pContext = certSelDlg.show();

        if (pContext) {

            _tprintf(_T("2 OK, selected a certificate\n"));
            CertContext cert(pContext);
            TCHAR name[MAX_PATH];
            cert.getName(name, sizeof(name));
            _tprintf(_T("Name:%s\n"), name);

            _tprintf(_T("3 Try to msgEncoer.encodeStream\n"));

            CryptMsg msgEncoder;
            if (msgEncoder.encodeStream(pContext, orgFileName, encFileName) == NO_ERROR) {
                _tprintf(_T("3 OK, msgEncoder.encodeStream() %s -> %s\n"),
                    orgFileName, encFileName);

                _tprintf(_T("4 Try to msgDecoder.decodeStream()\n"));
                CryptMsg msgDecoder;
            
                if (msgDecoder.decodeStream(encFileName, decFileName) == NO_ERROR) {
                    _tprintf(_T("5 OK, msgDecoder.decodeStream() %s -> %s \n"),
                        encFileName, decFileName);
                } else {
                    _tprintf(_T("6 Failed to msgDecoder.decodeStream\n"));
                }
            } else {
                _tprintf(_T("7 Failed to msgEncoder.encodeStream\n"));
            }

        } else {
            _tprintf(_T("8 NG, Canceled a selection\n"));
        }
        
    } catch (...) {
        printf("9 Excption: %x\n", GetLastError());
    }
}

Last modified: 11 Nov 2009

Copyright (c) 2009 Antillia.com ALL RIGHTS RESERVED.