将const unsigned char *传递给C#DLL [英] Pass const unsigned char* to C# DLL
问题描述
起初我想提一下我是c ++的新手。
i正在使用c ++核心系统服务器端应用程序(非托管),C#auth系统服务器端应用程序和C#客户端应用程序。我正在使用简单的2个函数来加密/解密服务器和客户端之间的数据包加密和解密在2个c#应用程序(客户端和身份验证系统服务器)之间完美运行
我尝试过:
i试图在C ++核心服务器中使用相同的系统类/函数,如
at first i would like to mention that i am new to c++.
i am working on a c++ core system server side application(unmanaged), C# auth system server side application and C# client side application. i am using a simple 2 functions to encrypt/decrypt packets between server`s and client Encrypt and Decrypt is working perfect between 2 c# apps (client and auth system server)
What I have tried:
i tried to use the same system class`s/functions in C++ core server using it like
using System::IO;
using System::Security::Cryptography;
但是没能在C ++应用程序中完成完整的代码。
这让我想到我在C#中创建.dll包含加密的两个函数/用c ++解密数据?
这里是C#.dll代码密码学
but failed to complete the full code in C++ app.
so that`s got me thinking of how about i create .dll in C# contains those two functions to encrypt/decrypt data in c++ ?
and here is C# .dll code Cryptography
using System.Security.Cryptography;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System;
namespace __Security
{
[Guid("413A1D2D-B7A8-42D7-8751-EB5D87F98873")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("__Security.Cryptography")]
public class Cryptography : ICryptography
{
string CryptographyKey = "TESTINGKEY";
public char[] Decrypt(char[] IN)
{
char[] OUT;
OUT = null;
try
{
byte[] CLEAR_IN = IN.Select(c => (byte)c).ToArray();
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(CryptographyKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(CLEAR_IN, 0, CLEAR_IN.Length);
cs.Close();
}
OUT = System.Text.Encoding.Unicode.GetChars(ms.ToArray());
}
}
}
catch(System.Exception E) { Debugger.Debug(E.Message); Debugger.Debug(E.StackTrace); };
return OUT;
}
}
}
ICryptography
ICryptography
using System;
using System.Runtime.InteropServices;
namespace __Security
{
[Guid("B057AB41-D218-44AC-BEE6-66D41AC1AD90")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ICryptography
{
[DispId(1)]
char[] Encrypt(char[] IN);
char[] Decrypt(char[] IN);
};
}
和c ++应用程序我做了这样的事情:
and in c++ application i made something like this:
#include <list>
#import "__Security.tlb" raw_interfaces_only
using namespace __Security;
unsigned char* Kernel::Decrypt(unsigned char* _IN, int Len)
{
HRESULT hr = CoInitialize(NULL);
ICryptographyPtr _Cryptography(__uuidof(Cryptography));
SAFEARRAY* SAFEARRAY_IN = GetSafeArray(_IN, 0);
SAFEARRAY* *SAFEARRAY_OUT;//this is for return value
HRESULT hr3 = _Cryptography->Decrypt(SAFEARRAY_IN, SAFEARRAY_OUT);
if (!SUCCEEDED(hr3))// always fails
{
_com_error err(hr3);
LPCTSTR errMsg = err.ErrorMessage();
_tprintf(errMsg);
}
CoUninitialize();
unsigned char* _OUT = new unsigned char[Len];
return _OUT;
}
SAFEARRAY* Kernel::GetSafeArray(const unsigned char* source, int codePage)
{
int Size = sizeof(source);
CComSafeArray<BSTR> result(1);
BSTR bstr = (BSTR)source;
BSTR bstr2 = SysAllocString(bstr);
HRESULT hr = result.SetAt(0, bstr2, FALSE);
if (FAILED(hr))
AtlThrow(hr);
return result.Detach();
}
因此,当我尝试调试C#.dll时,我收到了一条带有
$ b $的异常消息b
So when i tried to debug C# .dll i got an exception message with
SafeArrayTypeMismatchException
现在我需要知道如何安全地将const unsigned char *发送到C#dll在加密/解密后收回它。
如果这是我继续的错误方式...那么如何转换我的C#Cryptography类/函数转换为c ++,与所有3个系统应用程序兼容。
Now i need to know how to safely send the const unsigned char* to C# dll and receive it back after doing encryption/decryption.
if that`s is the wrong way i go on... so how do i convert my C# Cryptography class/functions into c++ with to be compatible with all 3 system apps.
推荐答案
为什么使用char数组? COM能够使用字符串。
Why are you using char arrays? COM is capable of using strings.
这篇关于将const unsigned char *传递给C#DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!