TripleDESCryptoServiceProvider

I have an encryption class I use in c# that is almost working in c++.

The code runs with out exception but there are two properties of the TripleDESCryptoServiceProvider object I could not find a way to set:

TripleDESCryptoServiceProvider^ tdes = gcnew TripleDESCryptoServiceProvider();
tdes->Key = keyArray;
tdes->Mode = CipherMode.ECB;
tdes->Padding = PaddingMode.PKCS7;

I can't Google anything on CipherMode.ECB or PaddingMode.PKCS7. In a simple test such as:

String^ plainText = "See if this code works";
String^ encCode = "abdg64hrUHGF5rEW";
String^ encText = encryptStr(plainText, encCode, false);
String^ decryptedText = decryptStr(encText, encCode, false);

My string comes back with a few odd characters at the beginning of the string that are NOT correct and then part of the original string is returned correctly. I don't know if it has anything to do with these properties I cannot seem to set.

Here are the calls I am making with the mysterious properties commented out:

private: static String^ decryptStr(String^ toDecrypt, String^ key, bool useHashing)
{
array<Byte>^ keyArray;
array<Byte>^ toEncryptArray = Convert::FromBase64String(toDecrypt);

UTF8Encoding^ utf8 = gcnew UTF8Encoding();

if (useHashing)
{
MD5CryptoServiceProvider^ hashmd5 = gcnew MD5CryptoServiceProvider();
keyArray = hashmd5->ComputeHash(utf8->GetBytes(key));

hashmd5->Clear();
}
else
{
UTF8Encoding^ utf8 = gcnew UTF8Encoding();
keyArray = utf8->GetBytes(key);
}

TripleDESCryptoServiceProvider^ tdes = gcnew TripleDESCryptoServiceProvider();
tdes->Key = keyArray;
//tdes->Mode = CipherMode.ECB;
//tdes->Padding = PaddingMode.PKCS7;

ICryptoTransform^ cTransform = tdes->CreateDecryptor();
array<Byte>^ resultArray = cTransform->TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

tdes->Clear();

return utf8->GetString(resultArray);
}

private: static String^ encryptStr(String^ toEncrypt, String^ key, bool useHashing)
{
UTF8Encoding^ utf8 = gcnew UTF8Encoding();

array<Byte>^ keyArray;
array<Byte>^ toEncryptArray = utf8->GetBytes(toEncrypt);

if (useHashing)
{
MD5CryptoServiceProvider^ hashmd5 = gcnew MD5CryptoServiceProvider();
keyArray = hashmd5->ComputeHash(utf8->GetBytes(key));

hashmd5->Clear();
}
else
{
keyArray = utf8->GetBytes(key);
}

TripleDESCryptoServiceProvider^ tdes = gcnew TripleDESCryptoServiceProvider();
tdes->Key = keyArray;
//tdes->Mode = CipherMode.ECB;
//tdes->Padding = PaddingMode.PKCS7;

ICryptoTransform^ cTransform = tdes->CreateEncryptor();
array<Byte>^ resultArray = cTransform->TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

tdes->Clear();

return Convert::ToBase64String(resultArray, 0, resultArray.Length);
}

Any help would be appreciated.

Thanks

BTW - I just tested the hash and they both do the same thing: they mess up the first 8 characters and then get the rest of the string correct. 8 ... hmmmmmmmm


THE ANSWER:

This is a syntax error - why the compiler doesn't point it out is beyond me.

Here is the offending code:
tdes->Mode = CipherMode.ECB;

It should be:
tdes->Mode = CipherMode::ECB;

Now all is well.

BTW - everything I read says not to use ECB.
Last edited on
Topic archived. No new replies allowed.