Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Decryption problem using C#

Posted on 2009-02-24
6
Medium Priority
?
233 Views
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

0
Comment
Question by:hussainAbid
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:JulienVan
ID: 23722558
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

0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 23739333
Does buf have anything in it?

0
 

Accepted Solution

by:
hussainAbid earned 0 total points
ID: 23742491
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
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 12

Expert Comment

by:williamcampbell
ID: 23751157
There was nothing in buf right .. no password :)

buf = Convert.FromBase64String(source);

might want to check for null or 0 length
0
 

Author Comment

by:hussainAbid
ID: 23753681
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
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 23759226
Any chance of getting the points :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

810 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