Click here to Skip to main content
14,663,182 members
Home » Discussions » ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralRe: Remove COM Objects from VC++ projects Pin
Richard MacCutchan3-Jun-13 22:51
mveRichard MacCutchan3-Jun-13 22:51 
GeneralRe: Remove COM Objects from VC++ projects Pin
Prashant Gupta 2413-Jun-13 23:30
MemberPrashant Gupta 2413-Jun-13 23:30 
GeneralRe: Remove COM Objects from VC++ projects Pin
Richard MacCutchan3-Jun-13 23:43
mveRichard MacCutchan3-Jun-13 23:43 
AnswerRe: Remove COM Objects from VC++ projects Pin
Hrpreet singh6-Jun-13 0:36
MemberHrpreet singh6-Jun-13 0:36 
GeneralRe: Remove COM Objects from VC++ projects Pin
Prashant Gupta 2416-Jun-13 0:42
MemberPrashant Gupta 2416-Jun-13 0:42 
Questionc++ exporting class variables Pin
bfis1081371-Jun-13 9:26
Memberbfis1081371-Jun-13 9:26 
AnswerRe: c++ exporting class variables Pin
MicroVirus3-Jun-13 5:10
MemberMicroVirus3-Jun-13 5:10 
QuestionHow to encrypt and decrypt a const char* in winrt Pin
yourchandrashekhar@gmail.com26-May-13 22:09
Memberyourchandrashekhar@gmail.com26-May-13 22:09 
Hi all,
I have been trying to write encrypt and decrypt functions whose signatures require the input and the output strings to be in 'void*' type only. The code works fine if the inputs can be specified as IBuffer^ but in the other case the source string and the encrypted->decrypted string do not match. I've googled a lot but to no good. Please help...it's urgent.

IBuffer^ byteArrayToIBufferPtr(byte *source, int size)
{
	Platform::ArrayReference<uint8> blobArray(source, size);
	IBuffer ^buffer = CryptographicBuffer::CreateFromByteArray(blobArray);
	return buffer;
}

byte* IBufferPtrToByteArray(IBuffer ^buffer)
{
	Array<unsigned char,1U> ^platArray = ref new Array<unsigned char,1U>(256);
	CryptographicBuffer::CopyToByteArray(buffer,&platArray);

	byte *dest = platArray->Data;
	return dest;
}

int DataEncryption::encryptData(EncryptionAlgorithm algo, int keySize, void* srcData, const unsigned int srcSize,
		void*& encData, unsigned int& encSize)
{

	LOG_D(TAG, "encryptData()");

	if(srcData == nullptr)
	{
		LOG_E(TAG,"");
		return DataEncryption::RESULT_EMPTY_DATA_ERROR;
	}
	if(srcSize == 0)
	{
		LOG_E(TAG,"");
		return DataEncryption::RESULT_SIZE_ZERO_ERROR;
	}

	IBuffer^ encrypted;
    IBuffer^ buffer;
    IBuffer^ iv = nullptr;
	String^ algName;
	bool cbc = false;

	switch (algo)
	{
	case DataEncryption::ENC_DEFAULT:
		algName = "AES_CBC";
		cbc = true;
		break;
	default:
		break;
	}

	// Open the algorithm provider for the algorithm specified on input.
    SymmetricKeyAlgorithmProvider^ Algorithm = SymmetricKeyAlgorithmProvider::OpenAlgorithm(algName);

    // Generate a symmetric key.
    IBuffer^ keymaterial = CryptographicBuffer::GenerateRandom((keySize + 7) / 8);
    CryptographicKey^ key;

    try
    {
        key = Algorithm->CreateSymmetricKey(keymaterial);
    }
    catch(InvalidArgumentException^ e)
    {
		LOG_E(TAG,"encryptData(): Could not create key.");
		return DataEncryption::RESULT_ERROR;
    }

    // CBC mode needs Initialization vector, here just random data.
    // IV property will be set on "Encrypted".
    if (cbc)
        iv = CryptographicBuffer::GenerateRandom(Algorithm->BlockLength);

    // Set the data to encrypt. 
	IBuffer ^srcDataBuffer = byteArrayToIBufferPtr(static_cast<byte*>(srcData),256);

    // Encrypt and create an authenticated tag.
    encrypted = CryptographicEngine::Encrypt(key, srcDataBuffer, iv);

	//encData = encrypted;
	byte *bb = IBufferPtrToByteArray(encrypted);
	encData = IBufferPtrToByteArray(encrypted);
	encSize = encrypted->Length;

	return DataEncryption::RESULT_SUCCESS;
}


int DataEncryption::decryptData(EncryptionAlgorithm algo, int keySize, void* encData, const unsigned int encSize,
		void*& decData, unsigned int& decSize)
{
	LOG_D(TAG, "decryptData()");

	if(encData == nullptr)
	{
		LOG_E(TAG,"");
		return DataEncryption::RESULT_EMPTY_DATA_ERROR;
	}
	if(encSize == 0)
	{
		LOG_E(TAG,"");
		return DataEncryption::RESULT_SIZE_ZERO_ERROR;
	}

	IBuffer^ encrypted;
    IBuffer^ decrypted;
    IBuffer^ iv = nullptr;
	String^ algName;
	bool cbc = false;

	switch (algo)
	{
	case DataEncryption::ENC_DEFAULT:
		algName = "AES_CBC";
		cbc = true;
		break;
	default:
		break;
	}

	// Open the algorithm provider for the algorithm specified on input.
    SymmetricKeyAlgorithmProvider^ Algorithm = SymmetricKeyAlgorithmProvider::OpenAlgorithm(algName);

    // Generate a symmetric key.
    IBuffer^ keymaterial = CryptographicBuffer::GenerateRandom((keySize + 7) / 8);
    CryptographicKey^ key;

    try
    {
        key = Algorithm->CreateSymmetricKey(keymaterial);
    }
    catch(InvalidArgumentException^ e)
    {
		LOG_E(TAG,"encryptData(): Could not create key.");
		return DataEncryption::RESULT_ERROR;
    }

    // CBC mode needs Initialization vector, here just random data.
    // IV property will be set on "Encrypted".
    if (cbc)
        iv = CryptographicBuffer::GenerateRandom(Algorithm->BlockLength);

    // Set the data to decrypt. 
	byte *cc = static_cast<byte*>(encData);
	IBuffer ^encDataBuffer = byteArrayToIBufferPtr(cc,256);
    // Decrypt and verify the authenticated tag.
    decrypted = CryptographicEngine::Decrypt(key, encDataBuffer, iv);

	byte *bb = IBufferPtrToByteArray(decrypted);
	decData = IBufferPtrToByteArray(decrypted);

	decSize = decrypted->Length;

	return DataEncryption::RESULT_SUCCESS;
}

GeneralRe: How to encrypt and decrypt a const char* in winrt Pin
Richard MacCutchan26-May-13 23:36
mveRichard MacCutchan26-May-13 23:36 
GeneralRe: How to encrypt and decrypt a const char* in winrt Pin
yourchandrashekhar@gmail.com28-May-13 9:46
Memberyourchandrashekhar@gmail.com28-May-13 9:46 
GeneralRe: How to encrypt and decrypt a const char* in winrt Pin
Richard MacCutchan28-May-13 11:21
mveRichard MacCutchan28-May-13 11:21 
QuestionVC++ Crashing? Pin
macklinbob20-May-13 9:25
Membermacklinbob20-May-13 9:25 
AnswerRe: VC++ Crashing? Pin
chaau5-Jun-13 15:23
Memberchaau5-Jun-13 15:23 
QuestionSuggestion for project type Pin
bkelly1319-May-13 15:55
Memberbkelly1319-May-13 15:55 
AnswerRe: Suggestion for project type Pin
Garth J Lancaster19-May-13 17:49
professionalGarth J Lancaster19-May-13 17:49 
GeneralRe: Suggestion for project type Pin
bkelly1320-May-13 15:17
Memberbkelly1320-May-13 15:17 
GeneralRe: Suggestion for project type Pin
Garth J Lancaster20-May-13 15:26
professionalGarth J Lancaster20-May-13 15:26 
GeneralRe: Suggestion for project type Pin
bkelly1320-May-13 15:54
Memberbkelly1320-May-13 15:54 
GeneralRe: Suggestion for project type Pin
Garth J Lancaster20-May-13 16:01
professionalGarth J Lancaster20-May-13 16:01 
GeneralRe: Suggestion for project type Pin
bkelly1320-May-13 16:12
Memberbkelly1320-May-13 16:12 
GeneralRe: Suggestion for project type Pin
Garth J Lancaster20-May-13 16:20
professionalGarth J Lancaster20-May-13 16:20 
GeneralRe: Suggestion for project type Pin
bkelly1320-May-13 16:27
Memberbkelly1320-May-13 16:27 
GeneralRe: Suggestion for project type Pin
Garth J Lancaster20-May-13 16:32
professionalGarth J Lancaster20-May-13 16:32 
GeneralRe: Suggestion for project type Pin
bkelly1320-May-13 16:38
Memberbkelly1320-May-13 16:38 
GeneralRe: Suggestion for project type Pin
Garth J Lancaster20-May-13 16:49
professionalGarth J Lancaster20-May-13 16:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.