简体   繁体   English

C#3DES加密到C解密

[英]C# 3DES encryption to C decryption

I have created a C# assembly that does 3DES encryption/encryption and tested it. 我创建了一个C#程序集,它进行3DES加密/加密并对其进行测试。 I now need to decrypt the data on a remote machine for an install. 我现在需要在远程计算机上解密数据以进行安装。 .NET is not guaranteed to be present when my native process runs, so I need to decrypt it using Win32 C++ methods. 当我的本机进程运行时,不保证.NET存在,所以我需要使用Win32 C ++方法解密它。 This is for a commercial applicaiton, so third party libraries are going to need to flexible with their licensing. 这是一个商业应用程序,因此第三方图书馆需要灵活的许可。 I would prefer a simple example to get me started. 我想要一个简单的例子让我开始。 Most of the examples I have found so far require importing session keys. 到目前为止,我发现的大多数示例都需要导入会话密钥。 I'm not using those. 我没有使用那些。 I am encrypting on machineA with .NET 2.0, and passing over to machineB where I will retrive the key and decrypt with native Win32 API's. 我在使用.NET 2.0的machineA上进行加密,然后传递给machineB,我将在其中检索密钥并使用本机Win32 API进行解密。 Can anyone point me in the right direction with some examples? 有人可以用一些例子指出我正确的方向吗?

I know I need to start with CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFY_CONTEXT) . 我知道我需要从CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFY_CONTEXT) However, the next step appears to be import key and it looks like it requires ( http://support.microsoft.com/kb/228786 ). 但是,下一步似乎是导入密钥,它看起来像它需要( http://support.microsoft.com/kb/228786 )。 Is this correct, or am I making this too difficult. 这是正确的,还是我让这个太难了。 I have a basic understanding of encryption. 我对加密有基本的了解。 Thanks in advance! 提前致谢!

Take a look to the following code: 看看以下代码:

#define TRIPLEDES_KEYSIZE 24
#define TRIPLEDES_BLOCKSIZE 8

...

BYTE key[TRIPLEDES_KEYSIZE] = { ... };

...

HCRYPTKEY hKey;

typedef struct
{
    BLOBHEADER hdr;
    DWORD cbKeySize;
    BYTE rgbKeyData [TRIPLEDES_KEYSIZE];
} KEYBLOB;

KEYBLOB keyBlob;
memset(&keyBlob, 0, sizeof(keyBlob));
keyBlob.cbKeySize = TRIPLEDES_KEYSIZE;
keyBlob.hdr.bType = PLAINTEXTKEYBLOB;
keyBlob.hdr.bVersion = CUR_BLOB_VERSION;
keyBlob.hdr.aiKeyAlg = CALG_3DES;
memcpy(keyBlob.rgbKeyData, key, TRIPLEDES_KEYSIZE);

BOOL res = CryptImportKey(hCryptProv, (const BYTE*)&keyBlob, sizeof(keyBlob), 0, 0, &hKey);
if (res)
{
    res = CryptSetKeyParam(hKey, KP_MODE, CRYPT_MODE_ECB, 0);

Please note you can use CRYPT_MODE_ECB or CRYPT_MODE_CBC in the call to the function CryptSetKeyParam with KP_MODE option depending on what you want to do. 请注意,您可以在使用KP_MODE选项调用CryptSetKeyParam函数时使用CRYPT_MODE_ECBCRYPT_MODE_CBC ,具体取决于您要执行的操作。 You can set an IV by for example the following code 您可以通过以下代码设置IV

res = CryptSetKeyParam(hKey, KP_IV, iv, 0);

which makes only sense in a CRYPT_MODE_CBC like mode. 这在CRYPT_MODE_CBC类的模式下才有意义。

Please note there is also a different 3DES mode ( CALG_3DES_112 ) working with only 112 Bit key (ie with two normal DES keys). 请注意,还有一个不同的3DES模式( CALG_3DES_112 )仅使用112位密钥(即使用两个普通DES密钥)。 You have to modify the code if you want to use this mode. 如果要使用此模式,则必须修改代码。

Edit: 编辑:

You should write some classes in C++ to manage all things of the CryptoApi. 您应该用C ++编写一些类来管理CryptoApi的所有内容。 It will save you a lot of headache. 它会为你省去很多麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM