Click here to Skip to main content
15,891,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here`s the encryption method in C#:

C#
string CryptographyKey = "BC234xs45nme7HU9";
        public byte[] Encrypt(byte[] IN)
        {
            byte[] OUT;
            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.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(IN, 0, IN.Length);
                        cs.Close();
                    }
                    OUT = ms.ToArray();
                }
            }
            return OUT;
        }


And here's the decryption method in c++:

C++
void Cryptograph::Decrypt(void* in, void* out, int Size)
{
    try
    {
        unsigned char* pIn = (unsigned char*)in;
        unsigned char* pOut = (unsigned char*)out;
        cli::array<unsigned char> ^Buffer;
        Buffer = gcnew cli::array<unsigned char>(Size);

        cli::array<unsigned char> ^OutBuffer;
        OutBuffer = gcnew cli::array<unsigned char>(Size);

        cli::array<unsigned char> ^Salt;
        Salt = gcnew cli::array<unsigned char>(13) { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
        for (int i = 0; i < Size; i++)
            Buffer[i] = pIn[i];

        Aes^ encryptor = Aes::Create();

        System::String^ CryptographyKey = "BC234xs45nme7HU9";
        Rfc2898DeriveBytes^ pdb = gcnew Rfc2898DeriveBytes(CryptographyKey, Salt);
        encryptor->Key = pdb->GetBytes(32);
        encryptor->IV = pdb->GetBytes(16);

        ICryptoTransform^ ICT = encryptor->CreateDecryptor();

        MemoryStream^ ms = gcnew MemoryStream();
        CryptoStream^ cs = gcnew CryptoStream(ms, ICT, CryptoStreamMode::Write);
        cs->Write(Buffer, 0, Size);

        OutBuffer = ms->ToArray();

        cs->Close();
        ms->Close();
        delete cs;
        for (int i = 0; i < Size; i++)
            pOut[i] = OutBuffer[i];
    }
    catch(Exception^ e) { throw gcnew Exception("Failed to create DES Symmetric CryptoStream with error: " + e->Message); }
}


What I have tried:

But it keeps throwing an exception at cs->Close(); that says it's not a complete block? I'm using a 16 byte/ 128 bit array with the block size set to 128. I don't understand what's wrong?

No output results on decryption just that exception message... even if i commented on stream close code i get exception on memory stream close, if its commented too, the OutBuffer is always empty and memory stream length is 0
Posted
Updated 14-Jan-18 0:56am

Calling Close should call FlushFinalBlock - but the reference sources show that it doesn't
specifically override the Close method in the Rfc2898DeriveBytes class, so it might be worth replacing your call to close:
C#
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
    cs.Write(IN, 0, IN.Length);
    cs.FlushFinalBlock();
}
Given that the using block will Close and Dispose the stream for you.
 
Share this answer
 
Comments
Ali Al-Masry 14-Jan-18 4:48am    
is it available to use using statement in this way you provided in c++?
i am facing my problem in c++ code not C# one
Okay problem fixed, i forgot to encrypt first packet was sent to the c++ app, now everything is going well

I also edited the code to be like this

C++
unsigned char* Cryptograph::Decrypt(void* in, int Size)
{
	try
	{
		unsigned char* pIn = (unsigned char*)in;

		cli::array<unsigned char> ^Buffer;
		Buffer = gcnew cli::array<unsigned char>(Size);

		cli::array<unsigned char> ^OutBuffer;
		OutBuffer = gcnew cli::array<unsigned char>(Size);

		cli::array<unsigned char> ^Salt;
		Salt = gcnew cli::array<unsigned char>(13) { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
		for (int i = 0; i < Size; i++)
			Buffer[i] = pIn[i];

		Aes^ encryptor = Aes::Create();

		System::String^ CryptographyKey = "BC234xs45nme7HU9";
		Rfc2898DeriveBytes^ pdb = gcnew Rfc2898DeriveBytes(CryptographyKey, Salt);
		encryptor->Key = pdb->GetBytes(32);
		encryptor->IV = pdb->GetBytes(16);

		ICryptoTransform^ ICT = encryptor->CreateDecryptor();

		MemoryStream^ ms = gcnew MemoryStream();
		CryptoStream^ cs = gcnew CryptoStream(ms, ICT, CryptoStreamMode::Write);
		cs->Write(Buffer, 0, Size);
		cs->FlushFinalBlock();
		OutBuffer = ms->ToArray();

		cs->Close();
		ms->Close();
		delete cs;
		unsigned char* pOut = new unsigned char[OutBuffer->Length];
		for (int i = 0; i < OutBuffer->Length; i++)
			pOut[i] = OutBuffer[i];
		return pOut;
	}
	catch (System::Exception^ e) { throw gcnew System::Exception("Failed to create DES Symmetric CryptoStream with error: " + e->Message); }
}
 
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