Click here to Skip to main content
15,844,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
using System::IO;
using System::Security::Cryptography;


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

C#
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

C#
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);
    };
}


and in c++ application i made something like this:

C++
#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();
}


So when i tried to debug C# .dll i got an exception message with
HTML
SafeArrayTypeMismatchException  


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.
Posted
Updated 19-Jan-18 23:38pm
v4

1 solution

Why are you using char arrays? COM is capable of using strings.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900