Solved

Throws error on Decrypting String c# code

Posted on 2016-11-16
3
26 Views
Last Modified: 2016-11-20
Hi experts ,

We are trying to encrypt and decrypt the password to store on database . we are successfully encrypt the password . but when we try to decrypt it shows error as
Length of the data to decrypt is invalid.
. Don't Understand why the error is occurred

 public static string DecryptString(string Message, string Passphrase)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

            // Step 1. We hash the passphrase using MD5 
            // We use the MD5 hash generator as the result is a 128 bit byte array 
            // which is a valid length for the TripleDES encoder we use below 

            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

            // Step 2. Create a new TripleDESCryptoServiceProvider object 
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

            // Step 3. Setup the decoder 
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;

            // Step 4. Convert the input string to a byte[] 
            byte[] DataToDecrypt = Convert.FromBase64String(Message);

            // Step 5. Attempt to decrypt the string 
            try
            {
                ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information 
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }

            // Step 6. Return the decrypted string in UTF8 format 
            return UTF8.GetString(Results);
        }

Open in new window


Any idea on this Experts
0
Comment
Question by:karthik80c
  • 2
3 Comments
 
LVL 32

Expert Comment

by:sarabande
ID: 41889797
don't see any encrypting of 'Message' in your code.

Sara
0
 

Author Comment

by:karthik80c
ID: 41891177
Hi Sara ,

Which means you don't have the message to encrypt sara ?

and i don't know exactly . this line is responsible for encrypting

ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }

Open in new window

0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 41891458
i don't know exactly . this line is responsible for encrypting

it can't as the 'Message' is not input. encrypting means that the message or a byte stream from message was converted to an encrypted byte-stream.

the code only does base64 translation on message text what is a 6-bit-to-8-bit transformation into a 64 character ASCII alphabet and not an encryption.

i think between step 3 and 4 (or between 4 and 5 if you want do base64 before) you have to add encoding ...

Sara
1

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

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

20 Experts available now in Live!

Get 1:1 Help Now