Solved

Throws error on Decrypting String c# code

Posted on 2016-11-16
3
47 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 33

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 33

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System.Speech 2 26
Problem!!! 8 33
How come this regular expression allows "e"? 14 34
C# LINQ ForEach() question 6 18
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

12 Experts available now in Live!

Get 1:1 Help Now