Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TripleDes decryption with some invalid data at the beginning

I am trying to decrypt data using tripleDes. Everything looks fine but it has some invalid characters at the beginning? What am I doing wrong? For same data if call this function again and again these first few characters are always different but the rest of the data is same.

I am passing useHashing to false.

public static byte[] GetTripleDesDecryption(string dataToDecrypt, byte[] key, bool useHashing)
    {
        byte[] keyArray;
        byte[] plainbytes = null;
        byte[] cipherbytes;

        try
        {
            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(key);
                hashmd5.Clear();
            }
            else
                keyArray = key;

            using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
            {
                tdes.Key = keyArray;
                tdes.Mode = CipherMode.CBC;
                tdes.Padding = PaddingMode.None;

                using (ICryptoTransform cTransform = tdes.CreateDecryptor())
                {
                    cipherbytes = Convert.FromBase64String(dataToDecrypt);
                    plainbytes = cTransform.TransformFinalBlock(cipherbytes, 0, cipherbytes.Length);
                }
            }
        }
        catch (Exception e)
        {
            LogMessage(e.Message + " Attribute Parsing error. DataToDecrypt = " + dataToDecrypt);
            throw e;
        }
        return plainbytes;
    }

This is what I get:

"�{c��]�sertion xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\"><saml:AttributeStatement><saml:Attribute Name=\"userID\"><saml:AttributeValue>456</saml:AttributeValue></saml:Attribute><saml:Attribute Name=\"financialInstitutionNumber\"><saml:AttributeValue>303986258</saml:AttributeValue></saml:Attribute><saml:Attribute Name=\"password\"><saml:AttributeValue>galaxy</saml:AttributeValue></saml:Attribute></saml:AttributeStatement></saml:Assertion>   "
like image 958
user228777 Avatar asked Apr 08 '26 15:04

user228777


1 Answers

I think that the C# classes use a random IV if it is not set. Try to set the IV to a byte array of 8 bytes valued 00h bytes and try to decrypt with that. If that does not work, you will have to retrieve the IV somehow.

PS the right way is of course to request the blocksize from the tdes instance instead of putting in the literal 8

like image 73
Maarten Bodewes Avatar answered Apr 11 '26 05:04

Maarten Bodewes