帮助我在VC ++ 2010,MFC,Unicode应用程序中加密字符串 [英] help me to encrypt string in VC++ 2010, MFC, unicode application

查看:82
本文介绍了帮助我在VC ++ 2010,MFC,Unicode应用程序中加密字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是从Unicode应用程序中加密一个字符串,并将其存储到Windows注册表中,然后在其他应用程序中获取该注册表值,然后对其进行解密.我可以成功添加注册表项,但是不知道如何加密字符串...请尽快帮助我...
我花了几天时间寻找正确的字符串加密指南(密码,文件夹的物理路径,标志),但是我仍然没有成功.我成功实现了以下代码,但由于我必须在其他应用程序中使用加密的字符串,因此此代码失败...请帮助我:(

My objective is to encrypt a string from Unicode application and store it to the windows registry and fetch that registry value in other application, decrypt it. I can successfully add registry key but DON''T KNOW HOW TO ENCRYPT STRINGS... please help me as soon as possible...
I spent days looking for proper guide to encrypt strings (passwords,physical path to folders, flags) but I''m still unsuccessful. I successfully Implemented following code but Since I have to use encrypted string in other application this code failed... PLEASE HELP ME :(

// Encrypt data from DATA_BLOB DataIn to DATA_BLOB DataOut.

//--------------------------------------------------------------------
// Declare and initialize variables.

DATA_BLOB DataIn;
DATA_BLOB DataOut;
BYTE *pbDataInput =(BYTE *)password;
DWORD cbDataInput = strlen((char *)pbDataInput)+1;

//--------------------------------------------------------------------
// Initialize the DataIn structure.

	DataIn.pbData = pbDataInput;    
	DataIn.cbData = cbDataInput;

//--------------------------------------------------------------------
//  Begin protect phase. Note that the encryption key is created
//  by the function and is not passed.

if(CryptProtectData(&DataIn,
	L"This is the description string.", // A description string to be included with the encrypted data. 
	NULL,                               // Optional entropy not used.
	NULL,                               // Reserved.
	NULL,                               // Pass NULL for the prompt structure.
	0,
	&DataOut));





谢谢
Sumit





Thank you
Sumit

推荐答案

可以找到很多建议
Lots of suggestions can be found here[^].


我已解决了您的问题...请在下面找到更新的代码根据需要进行加密和解密.问题是您占用的长度.您试图加密Unicode文本,但使用strlen总是返回您
长度为2.因此用于加密的文本只有2个字节,但与所需的实际字节不同..:) Enjoy

I have solved your issue... Please find the updated code below which encrypts and decrypts as you need. The problem was the length which you were taking.. You were trying to encrypt Unicode text but using strlen which always return you
length 2. So text which goes for encryption has only 2 bytes but which was different from actual bytes it needed.. :) Enjoy

void main()
{
        wchar_t password[] = L"Hello world of data protection.";
	DATA_BLOB DataIn;
	DATA_BLOB DataOut;
	DATA_BLOB DataVerify;
	LPWSTR pDescrOut = NULL;
	BYTE *pbDataInput =(BYTE *)password;
	DWORD cbDataInput = wcslen(password)*2+1;
	DataIn.pbData = pbDataInput;    
	DataIn.cbData = cbDataInput;


	if(CryptProtectData(
		 &DataIn,
		 L"This is the description string.", // A description string. 
		 NULL,                               // Optional entropy
											 // not used.
		 NULL,                               // Reserved.
		 NULL,                      // Pass a PromptStruct.
		 0,
		 &DataOut))
	{
		 printf("The encryption phase worked. \n");
	}
	else
	{
		printf("Encryption error!");
	}
	
	//-------------------------------------------------------------------
	//   Begin unprotect phase.

	if (CryptUnprotectData(
			&DataOut,
			&pDescrOut,
			NULL,                 // Optional entropy
			NULL,                 // Reserved
			NULL,        // Optional PromptStruct
			0,
			&DataVerify))
	{
		 printf("The decrypted data is: %S\n", DataVerify.pbData);
		 printf("The description of the data was: %S\n",pDescrOut);
	}
	else
	{
		MyHandleError("Decryption error!");
	}
	//-------------------------------------------------------------------
	//  Clean up.

	LocalFree(pDescrOut);
	LocalFree(DataOut.pbData);
	LocalFree(DataVerify.pbData);
}


我使用Crypto API解决了此问题..


前往..

http://msdn.microsoft.com/zh-我们/library/windows/desktop/aa379924%28v=vs.85%29.aspx [
要从密码获取哈希值,请先使用CryptCreateHash创建一个哈希对象,然后可以调用CryptHashData来获取从密码派生的哈希值.

[Step-3]生成密钥:CryptDeriveKey,CryptGenKey,CryptDestroyKey
这三个函数是用于生成键的句柄的函数:

CryptDeriveKey函数用于从指定的密码生成密钥.
CryptGenKey函数用于从随机生成的数据生成密钥.
CryptDestroyKey函数用于释放键对象的句柄.


[Step-4]加密和解密数据:CryptEncrypt,CryptDecrypt
在此步骤中,您准备为CryptEncrypt/CryptDecrypt调用准备纯文本或密码文本(加密文本)的缓冲区,然后可以调用CryptEncrypt进行加密或CryptDecrypt进行解密.

[步骤5]清理:CryptDestroyKey,CryptDestroyHash,CryptReleaseContext
完成加密/解密后,您必须清理Crypto Apis占用的资源.清理需要执行以下步骤
-使用CryptDestroyKey
销毁会话密钥 -使用CryptDestroyKey
销毁密钥交换密钥句柄 -使用CryptDestroyHash
销毁哈希对象 -使用CryptReleaseContext
I solved this issue using Crypto APIs..


Go to..

http://msdn.microsoft.com/en-us/library/windows/desktop/aa379924%28v=vs.85%29.aspx[^]

Here is the basic steps to encrypt/decrypt data using Crypto APIs

[Step-1] Initiating the Cryptography Service Provider (CSP): CryptAcquireContext, CryptReleaseContext
The CryptAcquireContext function is used to obtain a handle to a particular key container within a particular CSP. This returned handle can then be used to make calls to the selected CSP.

At the end of encryption/decryption you can call the CryptReleaseContext function to release the handle returned from a call to CryptAcquireContext.

[Step-2] Hashing Data: CryptCreateHash, CryptHashData, CryptGetHashParam, and CryptDestroyHash
"hashing" or "hash," refers to the method or algorithm used to derive a numeric value from a piece of data. In our case we will derive a numeric value (Hash) from our password which will be used to encrypt/decrypt the data and then this Hash value will be used to generate session key which we will see in the next step.

To get hash value from Password first create a hash object using CryptCreateHash then you can call CryptHashData to get hash value derived from your password.

[Step-3] Generating Keys: CryptDeriveKey, CryptGenKey, CryptDestroyKey
These three functions are the ones used to generate handles to keys:

The CryptDeriveKey function is used to generate a key from a specified password.
The CryptGenKey function is used to generate a key from random generated data.
The CryptDestroyKey function is used to release the handle to the key object.


[Step-4] Encrypting and Decrypting Data: CryptEncrypt, CryptDecrypt
In this step you prepare Buffer for Plain text or Cipher text (Encrypted text) for CryptEncrypt/CryptDecrypt call and then you can call CryptEncrypt for encryption or CryptDecrypt for decryption.

[Step-5] Cleanup : CryptDestroyKey, CryptDestroyHash, CryptReleaseContext
Once you are done with encryption/decryption you have to do cleanup of resources taken by Crypto Apis. Cleanup requires the following steps
- Destroy session key using CryptDestroyKey
- Destroy key exchange key handle using CryptDestroyKey
- Destroy hash object using CryptDestroyHash
- Release Context provider handle using CryptReleaseContext


这篇关于帮助我在VC ++ 2010,MFC,Unicode应用程序中加密字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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