Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.57/5 (3 votes)
See more:
Here is my decrypt code..
C#
public String psDecrypt(String sQueryString,string key)
{
   Byte[] buffer;
   TripleDESCryptoServiceProvider loCryptoClass =new TripleDESCryptoServiceProvider();
   MD5CryptoServiceProvider loCryptoProvider = new MD5CryptoServiceProvider();
   try
   {
      buffer = Convert.FromBase64String(sQueryString);
      loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
      loCryptoClass.IV = lbtVector;
      return Encoding.ASCII.GetString(loCryptoClass.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
   }
   catch(Exception ex)
   {
      throw ex;
   }
}


Now i want to decrypt my password..

code ..

C#
protected void btnsave_Click(object sender, EventArgs e)
{
   if (txtusername1.Text.ToString() != "" || txtno.Text.ToString() != "")
   {
      objloginpl.username = txtusername1.Text.ToString();
      objloginpl.phoneno= txtno.Text.ToString();
      DataTable dtcheck = new DataTable();

      dtcheck = objloginbal.getforgotpsswrd(objloginpl);
      string str_decryptpwd;
      str_decryptpwd = objSecurity.psDecrypt(dtcheck.Rows[0]["password"].ToString(),"lkjlkjlkjlk" );

      //my doubt this line..
   }
   else
   {
      ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Result", "alert('Please enter username or moblie no');", true);
   }
}

Which value pass psdecrypt second parameter...
Posted
Updated 13-May-14 0:18am
v2
Comments
Raul Iloc 14-May-14 7:33am    
I gave you a complete class that I'm also using it in my projects. See details bellow in Solution2. Did you try it?

You can't.

MD5 is not an encryption algorithm: it is a hashing algorithm. The difference is that encryption can be reversed, hashing cannot - because it "throws away" information which cannot be recovered at all.

Hashing passwords is fine - though MD5 is not recommended any more, use SHA instead - and one of the advantages is that you can;t work backwards from the stored value to "regenerate" or "decrypt" the original input. This increased security enormously!
 
Share this answer
 
Comments
Thanks7872 13-May-14 5:51am    
And there is more explanation available at Password Storage: How to do it.

:-)
OriginalGriff 13-May-14 5:52am    
I think I've seen that tip before! :laugh:
You could create a class that use TripleDESCryptoServiceProvider similar with my class bellow, and use it.
C#
/// <summary>
/// The basic class for encrypt/decrypt data.
/// </summary>
/// <remarks>
/// It uses a TripleDESCryptoServiceProvider object.
/// </remarks>
public class Des3 : IDisposable
{
    /// <summary>
    /// The used DES crypto service provider.
    /// </summary>
    private TripleDESCryptoServiceProvider _des = new TripleDESCryptoServiceProvider();

    /// <summary>
    /// Initializes a new instance of the Des3 class.
    /// </summary>
    public Des3()
    {
        //
        // Sets the data for the encryption algorithm.
        //
        string password = "YourSecreetKeyMustBeHere!!!"; //You should change this value!
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        byte[] passwordHash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));
        hashmd5.Clear();
        //
        // The key is the secret password hash.
        //
        _des.Key = passwordHash;
        _des.Mode = CipherMode.ECB;
    }

    /// <summary>
    /// Used as destructor.
    /// </summary>
    public void Dispose()
    {
        if (_des != null)
        {
            _des.Clear();
            _des = null;
        }
        //
        // Take this object off the finalization queue and prevent finalization
        // code for this object from executing a second time.
        //
        GC.SuppressFinalize(this);
    }

    /// <summary>
    /// Encrypts the argument string.
    /// </summary>
    /// <param name="inClearValue">String to be encrypted.</param>
    /// <returns>The encrypted string or empty string.</returns>
    public string Encrypt(string inClearValue)
    {
        byte[] buffer = ASCIIEncoding.ASCII.GetBytes(inClearValue);
        //
        string encrypted = Convert.ToBase64String(_des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
        return encrypted;
    }

    /// <summary>
    /// Decrypts the argument string.
    /// </summary>
    /// <param name="encryptedValue">String to be decrypted.</param>
    /// <returns>The decrypted string or empty string.</returns>
    public string Decrypt(string encryptedValue)
    {
        byte[] buffer = Convert.FromBase64String(encryptedValue);
        //
        // Decrypt DES 3 encrypted byte buffer and return ASCII string.
        //
        string decrypted = ASCIIEncoding.ASCII.GetString(_des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
        return decrypted;
    }
}
 
Share this answer
 
v2
It will Help you.......

In the Decryption Class file you need to create one more method and in that you have to declare defualtly one value to that keyvalue. And pass that value as decryption method arguments.
like below
In Decryption Class page code.....
C#
public Security()
  {
      lscryptoKey = "ChangeThis!";
  }

public String psDecrypt(String sQueryString,string key)
{
Byte[] buffer;
TripleDESCryptoServiceProvider loCryptoClass =new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider loCryptoProvider = new MD5CryptoServiceProvider();
try
{
buffer = Convert.FromBase64String(sQueryString);
loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
loCryptoClass.IV = lbtVector;
return Encoding.ASCII.GetString(loCryptoClass.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
}
catch(Exception ex)
{
throw ex;
}
finally
{
loCryptoClass.Clear();
loCryptoProvider.Clear();
loCryptoClass = null;
loCryptoProvider = null;
}
}
in Your .CS page code.....
C#
protected void btnsave_Click(object sender, EventArgs e)
{
   if (txtusername1.Text.ToString() != "" || txtno.Text.ToString() != "")
   {
      objloginpl.username = txtusername1.Text.ToString();
      objloginpl.phoneno= txtno.Text.ToString();
      DataTable dtcheck = new DataTable();

      dtcheck = objloginbal.getforgotpsswrd(objloginpl);
      string str_decryptpwd;
      str_decryptpwd = objSecurity.psDecrypt(dtcheck.Rows[0]["password"].ToString(),"ChangeThis!" );

      //my doubt this line..
   }
   else
   {
      ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Result", "alert('Please enter username or moblie no');", true);
   }
}
 
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