Link to home
Start Free TrialLog in
Avatar of hussainAbid
hussainAbid

asked on

Decryption problem using C#

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

Avatar of JulienVan
JulienVan
Flag of France image

Hi,

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

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

Open in new window

Does buf have anything in it?

ASKER CERTIFIED SOLUTION
Avatar of hussainAbid
hussainAbid

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There was nothing in buf right .. no password :)

buf = Convert.FromBase64String(source);

might want to check for null or 0 length
Avatar of hussainAbid
hussainAbid

ASKER

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
Any chance of getting the points :)