Click here to Skip to main content
16,020,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i determine whether the password entered by user is right or wrong in program ? Generally when wrong password is entered a file with garbage data is created.

Here's the code -
C#
private void DecryptFile(string inputFile2, string outputFile2)
   {

       try
       {
           string password = password2;      // password2 is data coming through textbox

           System.Text.UnicodeEncoding UE = new System.Text.UnicodeEncoding();
           byte[] key = UE.GetBytes(password);

           FileStream fsCrypt = new FileStream(inputFile2, FileMode.Open);


           RijndaelManaged RMCrypto = new RijndaelManaged();

           RMCrypto.Padding = PaddingMode.None;


           CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);

           FileStream fsOut = new FileStream(outputFile2, FileMode.Create);

           int data;

           while ((data = cs.ReadByte()) != -1)

           fsOut.WriteByte((byte)data);

               fsOut.Close();
               cs.Close();
               fsCrypt.Close();
                return;


       }

       catch(Exception e)
       {
           MessageBox.Show("Your password does not match"+e.Message);
           DeLoadImage_tbx.Text = "";  //Stegno image
           DeSaveFile_tbx.Text = "";   // Location to save data file
           textBox3.Text = "";        // textbox control for accepting password

       }

   }

   }
Posted
Updated 30-Jan-12 20:26pm
v4
Comments
Anuja Pawar Indore 30-Jan-12 8:36am    
Added pre tag
Sergey Alexandrovich Kryukov 30-Jan-12 13:16pm    
This is not a question. Also, it looks like the problem is in encryption, not in the code you show. "Padding" is a term of some encryption algorithms.
--SA

1 solution

This means that this is the error you get when you enter a wrong password. What is the issue ? Catch this exception and report that the password was wrong.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900