SOL9 Sample: RSA

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// 2009/03/03 

#include <sol/crypt/RSACryptServiceProvider.h>
#include <sol/crypt/RSAExchangeKey.h>
#include <sol/crypt/RSASignatureKey.h>

void cryptError() 
{ 
  const TCHAR* msg = CryptError::getMessage(GetLastError());
  _tprintf(_T("%s\n"), msg);
}


void _tmain(int argc, TCHAR** argv)
{

  try {

    RSACryptServiceProvider csp;

    {
      printf("\nRSAExchangeKey:\n");

      RSAExchangeKey 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) {
        printf("2. OK, exchangeKey.encrpt()\n");
        Bytes enc(buffer, dsize);
        enc.dump();
        printf("\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 {
          printf("5. Failed in exchangeKey.decrypt()\n");
          cryptError();
        }

      } else {
        printf("6. Failed in exchangeKey.encrypt().\n");
        cryptError();
      }

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

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

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

      // 
      Bytes priKey;
      if (exchangeKey.getPrivateKey(priKey) == NO_ERROR) {
        printf("11. OK exchangeKey.getPrivateKey()\n");
        priKey.dump();
      }  else {
        printf("12. Failed in exchangeKey.getPrivateKey()\n");
        cryptError();
      }
    }

    {
  
      printf("\nRSASignatureKey:\n");
      RSASignatureKey signatureKey(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 (signatureKey.encrypt(buffer, &dsize, bsize, TRUE) ==NO_ERROR) {
        printf("2. OK, signatureKey.encrpt()\n");
        Bytes enc(buffer, dsize);
        enc.dump();
        printf("\n3. Try to decrypt\n");
        if (signatureKey.decrypt(buffer, &dsize, TRUE) ==NO_ERROR) {
          printf("4. OK, signatureKey.decrypt()\n:SIZE=%d DATA=[%s]\n", dsize, (char*)buffer);    
        } else {
          printf("5. Failed in signatureKey.decrypt()\n");
          cryptError();
        }
      } else {
        printf("6. Failed in signatureKey.encrypt().\n");
        cryptError();
      }

      // 
      printf("\n7. Try to export publicKey from signatureKey\n");    

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

      printf("\n10. Try to export privateKey from signatureKey\n");    

      // 
      Bytes priKey;
      if (signatureKey.getPrivateKey(priKey) == NO_ERROR) {
        printf("11. OK signatureKey.getPrivateKey()\n");
        priKey.dump();
      }  else {
        printf("12. Failed in signatureKey.getPrivateKey()\n");
        cryptError();
      }
    }

  } catch (Exception& ex) {
    printf("Exception = %s\n", ex.getErrorMessage());
  }

}

Last modified: 2 May 2016

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