SOL9 Sample: ExchangeKey

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * ExchangeKey.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2009/03/05 

#include <sol/crypt/CryptServiceProvider.h>
#include <sol/crypt/ExchangeKey.h>

void cryptError() 
{ 
  _tprintf(_T("%s\n"), CryptError::getMessage(GetLastError()));
  exit(1);
}


int _tmain(int argc, TCHAR** argv)
{
  int rc = 0;

  try {
    CryptServiceProvider csp;

    {
      _tprintf(_T("\nExchangeKey:\n"));

      ExchangeKey exchangeKey(csp);

      const size_t size = 64;
      size_t dsize = size;

      const size_t bsize = size+ 128;
      unsigned char buffer[bsize];
      memset(buffer, 0, sizeof(buffer));

      strcpy_s((char*)buffer, SizeOf(buffer), "This is a sample text.Hello world!");

      printf("\n1. Try to encrypt SIZE=%d DATA=[%s]\n", dsize, (char*)buffer);    
      if (exchangeKey.encrypt(buffer, &dsize, bsize, TRUE) ==NO_ERROR) {
        _tprintf(_T("2. OK, exchangeKey.encrpt()\n"));
        Bytes enc(buffer, dsize);
        enc.dump();
        _tprintf(_T("\n3. Try to decrypt\n"));
        if (exchangeKey.decrypt(buffer, &dsize, TRUE) ==NO_ERROR) {
          printf("4. OK, exchangeKey.decrypt()\n:SIZE=%d DATA=[%s]\n", dsize, (char*)buffer);    
        } else {
          _tprintf(_T("5. Failed in exchangeKey.decrypt().\nThis is right, because DSASignaturKey doesn't have a decryption feature\n"));
          cryptError();
        }

      } else {
        _tprintf(_T("6. Failed in exchangeKey.encrypt().\nThis is right, because DSASignaturKey doesn't have an encryption feature.\n"));
        cryptError();
      }

      // 
      _tprintf(_T("\n7. Try to export publicKey from exchangeKey\n"));    

      Bytes pubKey;
      if (exchangeKey.getPublicKey(pubKey) == NO_ERROR) {
  
        _tprintf(_T("8. OK, exchangeKey.getPublicKey()\n"));
        pubKey.dump();
      } else {
        _tprintf(_T("9. Failed in exchangeKey.getPublicKey()\n"));
        cryptError();
      }

      _tprintf(_T("\n10. Try to export privateKey from exchangeKey\n"));    

      // 
      Bytes priKey;
      if (exchangeKey.getPrivateKey(priKey) == NO_ERROR) {
        _tprintf(_T("11. OK exchangeKey.getPrivateKey()\n"));
        priKey.dump();
      }  else {
        _tprintf(_T("12. Failed in exchangeKey.getPrivateKey()\n"));
        cryptError();
      }
    }
  } catch (Exception& ex) {
    printf("Exception = %s\n", ex.getErrorMessage());
  }

  rc = GetLastError();

  return rc;
}

Last modified: 2 May 2016

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