troubleshooting Question

Converting PHP triple des decrypt to c#

Avatar of Don VonderBurg
Don VonderBurgFlag for United States of America asked on
PHPC#Encryption
9 Comments1 Solution2420 ViewsLast Modified:
I have the following code written for a PHP page. I send it a text key and text encryption string and I get the correct answer. But when I attempt it in c# function I get an error stating "Specified key is not a valid size for this algorithm." I am banging my head against the wall on this. Any suggestions?

PHP code currently using that works:
function decrypt($str, $key)
{   
    $keysize = mcrypt_get_key_size(MCRYPT_3DES, MCRYPT_MODE_CFB);
    $enc = base64_decode($str);
    $k = hash(SHA256, $key, true);
    $iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC);
    $empty_iv = str_repeat(chr(0), $iv_size);
    $iv = mcrypt_encrypt(MCRYPT_3DES, substr($k, 0, $keysize), "", MCRYPT_MODE_CBC, $empty_iv);
    $dec = mcrypt_decrypt(MCRYPT_3DES, substr($k, 0, $keysize), $enc, MCRYPT_MODE_CFB, $iv);

    return $dec;

}

c# code that throws the error:
public static string Decrypt(string input, string key)
        {
            byte[] inputArray = Convert.FromBase64String(input);
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
            tripleDES.Mode = CipherMode.CFB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tripleDES.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
            tripleDES.Clear();
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
ASKER CERTIFIED SOLUTION
gr8gonzo
Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros