Example #1
0
KviCryptEngine::DecryptResult KviRijndaelEngine::decrypt(const char * inBuffer, KviCString & plainText)
{
	if(!m_pDecryptCipher)
	{
		setLastError(__tr2qs("Oops! Decryption cipher not initialized"));
		return KviCryptEngine::DecryptError;
	}

	if(*inBuffer != KviControlCodes::CryptEscape)
	{
		plainText = inBuffer;
		return KviCryptEngine::DecryptOkWasPlainText;
	}

	inBuffer++;

	if(!*inBuffer)
	{
		plainText = inBuffer;
		return KviCryptEngine::DecryptOkWasPlainText; // empty buffer
	}

	int len;
	char * binary;

	if(!asciiToBinary(inBuffer, &len, &binary))
		return KviCryptEngine::DecryptError;

	char * buf = (char *)KviMemory::allocate(len + 1);
	unsigned char * iv = nullptr;
	if(m_bEncryptMode == CBC)
	{
		// extract the IV from the cyphered string
		len -= MAX_IV_SIZE;
		iv = (unsigned char *)KviMemory::allocate(MAX_IV_SIZE);
		KviMemory::move(iv, binary, MAX_IV_SIZE);
		KviMemory::move(binary, binary + MAX_IV_SIZE, len);
		binary = (char *)KviMemory::reallocate(binary, len);
	}

	int retVal = m_pDecryptCipher->padDecrypt((const unsigned char *)binary, len, (unsigned char *)buf, iv);
	KviMemory::free(binary);
	KviMemory::free(iv);

	if(retVal < 0)
	{
		KviMemory::free(buf);
		setLastErrorFromRijndaelErrorCode(retVal);
		return KviCryptEngine::DecryptError;
	}

	buf[retVal] = '\0';

	plainText = buf;

	KviMemory::free(buf);
	return KviCryptEngine::DecryptOkWasEncrypted;
}
/*-----MAIN------*/
int main (void)
{ 
   int optionStats[NUM_OPTION_STATS];
   int optionChoice = 0;
   char sel[ 1 + EXTRA_SPACES ];
   char* prompt = "Please enter a selection: ";
   char *p;

   while (optionChoice != EXIT_OPTION )
   {
      /*prints the main menu */
      printMenu();
      
      /*gets user input as a string */
      getString(sel, 1 + EXTRA_SPACES, prompt);
      optionChoice = strtol(sel, &p, 10);

      switch (optionChoice)
      {     
       case PERFECT_SQUARES_OPTION : printf("option 1 selected \n");
                 perfectSquares(optionStats); 
                  break;
      case ASCII_TO_BINARY_OPTION : printf("option 2 selected \n");
                  asciiToBinary(optionStats);
                  break;
      case MATCHING_BRACKETS_OPTION : printf("option 3 selected \n");
                  matchingBrackets(optionStats);
                  break;
      case TIC_TAC_OPTION : printf("option 4 selected \n");
                  ticTacToeGame(optionStats);
                  break;
      case FORMAT_OPTION : printf("Option 5 selected \n");
                  formattingText(optionStats);
                  break;
      case SUMMARY_OPTION : printf("Option 6 selected \n");
                  sessionSummary(optionStats);
                  break;
      }
   }
   return EXIT_SUCCESS;
}