本文介绍了使用CryptoAPI加密和.NET和C#解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个程序需要一起对文本进行加密和解密.其中一个使用C#并在.NET中运行,具有简单的加密和解密方案.另一个不能在.NET中运行,而另一个可以在Win32中运行,并且是用C/C ++编写的. Win32片段需要加密数据,以便.NET片段可以解密它.我已经使用Microsoft提供的示例代码实现了.NET代码和Win32代码,它们自己可以很好地加密和解密数据.但是,每当我尝试从Win32加密数据时,.NET都无法解密它,并抛出"Bad数据"异常.我使用TripleDESCryptoServiceProvider类编写了.NET,并使用CryptoAPI编写了Win32.我不确定我需要做些什么才能获得同等的结果. .NET加密代码简单明了,但是我不确定到底是什么魔术":

I have two programs that need to work together encrypting and decrypting text. One, which is in C# and runs in .NET, has a simple encryption and decryption scheme. The other, which cannot run in .NET, runs in Win32 and is written in C/C++. The Win32 piece needs to encrypt data so that the .NET piece can decrypt it. I have implemented the both the .NET code and the Win32 piece using the sample code provided by Microsoft, and they work fine encrypting and decrypting data by themselves. However, whenever I try to encrypt data from Win32, the .NET is unable to decrypt it, throwing an exception as "Bad data." I have written the .NET piece using the TripleDESCryptoServiceProvider class, and the Win32 using the CryptoAPI. I am not certain just what I need to do to get equivalent results. The .NET encryption code is simple and straightforward, but I am not certain just what the "magic" is underneath:

TripleDESCryptoServiceProvider _desProvider = new TripleDESCryptoServiceProvider();
//bytes for key and initialization vector
byte[] keyBytes;
byte[] vectorBytes;

FileStream fStream = File.Open(locationOfFile, FileMode.Create, FileAccess.Write);

CryptoStream cStream = new CryptoStream(fStream,
    _desProvider.CreateEncryptor(keyBytes, vectorBytes),
    CryptoStreamMode.Write);

// Create a StreamWriter using the CryptoStream.
BinaryWriter bWriter = new BinaryWriter(cStream);
//write out encrypted data
byte[] rawData;
bWriter.Write(rawData);


使用Crypto API的等效代码是什么?我是否需要调用CryptDeriveKey(),使用密钥字节创建哈希码,在提供程序的名称中添加一些内容,导入密钥等?

我已经尝试过类似的操作:


What would be the equivalent code using the Crypto API? Do I need to call CryptDeriveKey(), create a hash code with the key bytes, put something into the name of the provider, import a key, etc.?

I''ve tried something like:

//the contents of keyBytes and vectorBytes are identical to the C# code
byte[] keyBytes;
byte[] vectorBytes;
	if(CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
	{
		HCRYPTKEY hKey = NULL;
                //what do I do here?
		{
			//set the IV.
			if(CryptSetKeyParam(hKey, KP_IV, vectorbytes, 0))
			{
				//encrypt the data
				BYTE filebytes[2048];
				DWORD cbEncrypted, dwSize;
				if (CryptEncrypt(hKey, NULL, TRUE, 0, filebytes, &cbEncrypted, dwSize))
				{
				}
			}
			CryptDestroyKey(hKey);
			hKey = NULL;
		}
         }


我已经查看了几个Microsoft网站上的示例代码( http://msdn.microsoft.com/en-us /library http://msdn.microsoft.com/zh-我们/library/aa382358(VS.85).aspx [ ^ ]),但我无法使其与.NET解密配合使用.我在做什么错?


I''ve looked at a couple of Microsoft sites for sample code (http://msdn.microsoft.com/en-us/library and http://msdn.microsoft.com/en-us/library/aa382358(VS.85).aspx[^]), but I haven''t been able to get it to work with the .NET decryption. What am I doing wrong?

推荐答案


这篇关于使用CryptoAPI加密和.NET和C#解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 15:28