Here`s the encryption method in 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++:
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