如何解密密码? [英] how to decrypt password?

查看:96
本文介绍了如何解密密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的解密代码..

  public  字符串 psDecrypt(字符串 sQueryString,字符串键)
{
字节 []缓冲区;
TripleDESCryptoServiceProvider loCryptoClass = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider loCryptoProvider = new MD5CryptoServiceProvider();
尝试
{
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 (例外情况)
{
throw ex;
}
}





现在我想解密我的密码..



代码..



<前lang =cs> 受保护 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) ;

// 我怀疑这一行......
}
else
{
ScriptManager.RegisterClientScriptBlock( this .Page,< span class =code-keyword> this .GetType(), 结果 alert('请输入用户名或moblie no'););
}
}



哪个值通过psdecrypt第二个参数...

解决方案

你不能。



MD5不是加密算法:它是一种散列算法。不同之处在于加密可以反转,哈希不能 - 因为它抛弃无法恢复的信息。



哈希密码很好 - 尽管MD5是不推荐使用SHA,而是使用SHA - 其中一个优点是你可以;从存储的值向后工作以重新生成或解密原始输入。这极大地提高了安全性!


您可以创建一个使用 TripleDESCryptoServiceProvider 的类与我的班级类似,并使用它。

  ///    <  摘要 >  
/// 加密/解密数据的基本类。
/// < / summary >
/// < 备注 < span class =code-summarycomment>>
/// 它使用TripleDESCryptoServiceProvider对象。
/// < /备注 >
public class Des3:IDisposable
{
/// < 摘要 >
/// 使用的DES加密服务提供商。
/// < / summary >
private TripleDESCryptoServiceProvider _des = new TripleDESCryptoServiceProvider();

/// < 摘要 >
/// 初始化Des3类的新实例。
/// < / summary >
public Des3()
{
//
// 设置加密算法的数据。
//
string password = YourSecreetKeyMustBeHere !!!; // 您应该更改此值!
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte [] passwordHash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));
hashmd5.Clear();
//
// 密钥是密码密码哈希。
//
_des.Key = passwordHash;
_des.Mode = CipherMode.ECB;
}

/// < 摘要 >
/// 用作析构函数。
/// < / summary >
public void Dispose()
{
if (_des != null
{
_des.Clear();
_des = null ;
}
//
// 将此对象从终结队列中取出并阻止最终确定
// 此对象的代码执行第二次。
//
GC.SuppressFinalize( this );
}

/// < 摘要 >
/// 加密参数字符串。
/// < / summary >
/// < param < span class =code-summarycomment> name =inClearValue > 字符串到加密。< / param >
/// < 返回 > 加密的字符串或空字符串。< / returns >
< span class =code-keyword> public string 加密( string inClearValue)
{
byte [] buffer = ASCIIEncoding.ASCII.GetBytes(inClearValue);
//
string encrypted = Convert.ToBase64String(_des.CreateEncryptor()。TransformFinalBlock(buffer, 0 ,buffer.Length));
返回加密;
}

/// < 摘要 >
/// 解密参数字符串。
/// < / summary >
/// < param < span class =code-summarycomment> name =encryptedValue > 字符串到解密。< / param >
/// < 返回 > 解密的字符串或空字符串。< / returns >
< span class =code-keyword> public string 解密( string encryptedValue)
{
byte [] buffer = Convert.FromBase64String(encryptedValue);
//
// 解密DES 3加密字节缓冲区并返回ASCII字符串。
// < span class =code-comment>
string decrypted = ASCIIEncoding.ASCII.GetString(_des.CreateDecryptor()。TransformFinalBlock(buffer, 0 ,buffer.Length));
return decrypted;
}
}


它会帮助你.......



在Decryption Class文件中,您需要再创建一个方法,并且必须向该keyvalue声明一个值。并将该值作为解密方法参数传递。

如下所示

在解密类页面代码中.....

  public 安全性()
{
lscryptoKey = ChangeThis!;
}



public String psDecrypt(String sQueryString,string key)

{

Byte []缓冲区;

TripleDESCryptoServiceProvider loCryptoClass = new TripleDESCryptoServiceProvider();

MD5CryptoServiceProvider loCryptoProvider = new MD5CryptoServiceProvider();

try

{

buffer = Convert.FromBase64String(sQueryString);

loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));

loCryptoClass.IV = lbtVector;

返回Encoding.ASCII.GetString(loCryptoClass.CreateDecryptor()。TransformFinalBlock(buffer,0,buffer.Length));

}

catch(例外情况)

{

抛出ex;

}

终于

{

loCryptoClass.Clear();

loC ryptoProvider.Clear();

loCryptoClass = null;

loCryptoProvider = null;

}

} .....

 受保护  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! );

// 我怀疑这一行......
}
else
{
ScriptManager.RegisterClientScriptBlock( this .Page,< span class =code-keyword> this .GetType(), 结果 alert('请输入用户名或moblie no'););
}
}


Here is my decrypt code..

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 ..

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...

解决方案

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!


You could create a class that use TripleDESCryptoServiceProvider similar with my class bellow, and use it.

/// <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;
    }
}


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.....

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.....

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);
   }
}


这篇关于如何解密密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆