如何在winrt中加密和解密const char * [英] How to encrypt and decrypt a const char* in winrt

查看:88
本文介绍了如何在winrt中加密和解密const char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我一直在尝试编写加密和解密函数,这些函数的签名需要输入,输出字符串只能是''void *''类型。如果输入可以指定为IBuffer ^,则代码可以正常工作,但在另一种情况下,源字符串和加密的>解密字符串不匹配。我已经google了很多但没有好处。请帮忙......这很紧急。



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;
}

推荐答案

我在单元测试用例中调用编码和解码函数。以下是他们的摘录。



这里的源数据(pData)和加密后的解密数据(pDecryptedData)最终应该是相同的字符串,即; SampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSample



但是没有发生这种情况。 pDecryptedData很奇怪。



I am calling the encode and decode functions in my unit test cases. Below is an extract from them.

Here the source data (pData) and the encrypted and then decrypted data (pDecryptedData) should have eventually come to be the same string i.e; "SampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSample"

but that is not happening. pDecryptedData is something weird.

char pData[] = "SampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSampleSampleTextSample";

DataEncryption::encryptData(DataEncryption::ENC_DEFAULT, 256, pData, sizeof(pData), pEncryptedData, encryptedLength)

DataEncryption::decryptData(DataEncryption::ENC_DEFAULT, 256, pEncryptedData, encryptedLength, pDecryptedData, decryptedLength)

if(!strcmp(pData,static_cast<char*>(pDecryptedData)))
{
	cout<<"Same";
}
else
{
	cout<<"Not Same";
}


信息不足。

CONST CHAR *字符串的比较可能会被优化。

解密结果不能是逻辑上的CONST CHAR *。



但主要是我不知道你的意思...

如果输入可以指定为IBuffer ^但在另一种情况下源字符串和加密的>解密字符串不匹配



什么是输入,在哪里使用,如何使用....代码丢失。
Not enough information.
Comparison of CONST CHAR* strings may be optimized.
Result of decription can''t be logicaly CONST CHAR*.

But mainly I don''t know what you mean with...
"if the inputs can be specified as IBuffer^ but in the other case the source string and the encrypted->decrypted string do not match"

What is input, where is used, how is used.... code missing.


这篇关于如何在winrt中加密和解密const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆