Decryption problem using C#

Posted on 2009-02-24
Last Modified: 2012-05-06
Hej I am trying to decrypt a password using a key and the following line is return empty byte Array...

ICryptoTransform t = des.CreateDecryptor();
byte[] resultArray = t.TransformFinalBlock(buf, 0, buf.Length);

Please help me it is really urgent.......
Thanks in advanced
private static string Decrypt(string source, string key, Encoding encoding) {

            byte[] buf;

            TripleDESCryptoServiceProvider des = GetDESService(key, encoding);

            buf = Convert.FromBase64String(source);


           ICryptoTransform t = des.CreateDecryptor(); //new line

           byte[] resultArray = t.TransformFinalBlock(buf, 0, buf.Length);//here it returns empty byte arrat

           String decryptedStr = encoding.GetString(resultArray); //new line

            return decryptedStr ;


 private static TripleDESCryptoServiceProvider GetDESService(string key, Encoding encoding)


            MD5CryptoServiceProvider hashmd5;

            TripleDESCryptoServiceProvider des;

            byte[] pwdhash;

            hashmd5 = new MD5CryptoServiceProvider();

            pwdhash = hashmd5.ComputeHash(encoding.GetBytes(key));

            des = new TripleDESCryptoServiceProvider();

            des.Key = pwdhash;

            des.Mode = CipherMode.ECB;

            des.Padding = PaddingMode.PKCS7; 

            return des;


Open in new window

Question by:hussainAbid
    LVL 2

    Expert Comment


    Maybe you can try to use a memorystream (see attached code). It's what I'm using in my application.

    //Set up the memory stream for the decrypted data.
              MemoryStream memStreamDecryptedData = new MemoryStream();
              CryptoStream decStream = new CryptoStream(memStreamDecryptedData, t, CryptoStreamMode.Write);
                decStream.Write(buf, 0, buf.Length);
              catch(Exception ex)
                throw new Exception("Error while writing encrypted data to the stream: \n"  + ex.Message);
              // Read the data back.
              byte[] resultArray = memStreamDecryptedData.ToArray();

    Open in new window

    LVL 12

    Expert Comment

    Does buf have anything in it?


    Accepted Solution

    Thanks for your help guys but I found the problem. Problem wasn't in the code that is decrypting but was indeed an hour on other business side where I was getting an encrypted password back from.
    so I would say that above mentioned code to decrypt a given encrption string and key is working smoothly.
    I am sorry for inconvenience. Cheers
    LVL 12

    Expert Comment

    There was nothing in buf right .. no password :)

    buf = Convert.FromBase64String(source);

    might want to check for null or 0 length

    Author Comment

    There was an encoded empty string and thats exactly what our business partner were sending to us due to wrong parameters set by us towards the call ...
    So It was me ..... Sorry
    LVL 12

    Expert Comment

    Any chance of getting the points :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now