且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Windows应用程序中将加密密码转换为C#中解密

更新时间:2023-09-28 11:51:58

为什么你需要解密密码?通常存储并比较密码的盐渍哈希。如果您加密/解密密码,则再次将密码作为纯文本,这很危险。如果某些用户具有相同的密码,则应该使用哈希值来避免重复哈希。对于盐,您可以使用用户名。



 HashAlgorithm hash =  new  SHA256Managed(); 
string password = 12345跨度>;
string salt = UserName跨度>;

// 密码的计算哈希
byte [] plainTextBytes = Encoding.UTF8.GetBytes(password);
byte [] hashBytes = hash.ComputeHash(plainTextBytes);

// 创建盐渍字节数组
byte [] saltBytes = Encoding.UTF8.GetBytes(salt);
byte [] plainTextWithSaltBytes = new byte [plainTextBytes.Length + saltBytes.Length];
for int i = 0 ; i < plainTextBytes.Length; i ++)
{
plainTextWithSaltBytes [i] = plainTextBytes [i];
}

// 计算盐渍哈希
byte [] saltedHashBytes = hash.ComputeHash(plainTextWithSaltBytes);
string saltedHashValue = Convert.ToBase64String(saltedHashBytes);





您可以计算密码的盐渍哈希并将其存储在您的文件中。在身份验证期间,您再次从用户条目计算哈希值,并将此哈希值与存储的密码哈希值进行比较。因为从哈希中获取纯文本应该是非常困难的(从来都不是时间问题)密码被保护不再以纯文本形式阅读。



提示:切勿存储或发送未加密的密码。如果您获得新密码,请尽快加密!

参考:***.com



请参考:

如何:加密和解密字符串使用C# [ ^ ]

如何使用C#在asp.net中加密和解密密码? [ ^ ]



一些CP文章:

在C#中简单加密和解密数据 [ ^ ]

使用C#加密和解密数据 [ ^ ]

使用C#中的DES加密/解密字符串 [ ^ ]

.NET Framework上的加密和解密 [ ^ ]


通过使用这段代码,您可以加密你在asp.net应用程序的文本框中写的密码。



  String  myPassword =  .txtpassword.Text; 
HashAlgorithm mhash = new SHA1CryptoServiceProvider();
byte [] bytValue = System.Text.Encoding.UTF8.GetBytes(myPassword);
byte [] bytHash = mhash.ComputeHash(bytValue);
mhash.Clear();
this .rtxtpassword.Text = Convert.ToBase64String(bytHash);





试试吧&建议。


Hi
How to convert encrypted password to decrypted in C# in windows application

in my database table password was encrypted now i want to do decrypted.

Why you need to decrypt the password? Usually a salted hash of the password is stored and compared. If you encrypt/decrypt the password you have the password as plain text again and this is dangerous. The hash should be salted to avoid duplicated hash if the some users have the same passwords. For the salt you can take the user name.

HashAlgorithm hash = new SHA256Managed();
string password = "12345";
string salt = "UserName";

// compute hash of the password
byte[] plainTextBytes = Encoding.UTF8.GetBytes(password);
byte[] hashBytes = hash.ComputeHash(plainTextBytes);

// create salted byte array
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];
for (int i = 0; i < plainTextBytes.Length; i++)
{
   plainTextWithSaltBytes[i] = plainTextBytes[i];
}

// compute salted hash
byte[] saltedHashBytes = hash.ComputeHash(plainTextWithSaltBytes);
string saltedHashValue = Convert.ToBase64String(saltedHashBytes);



You can calculate the salted hash of the password and store that within your file. During the authentication you calculate the hash from the user entries again and compare this hash with the stored password hash. Since it should be very difficult (its never impossible, always a matter of time) to get the plain text from a hash the password is protected from reading as plain text again.

Tip: Never store or send a password unencrypted. If you get a new password, encrypt is as soon as possible!
Ref: ***.com

Please refer:
How To: Encrypt and Decrypt string Using C#[^]
How to encrypt and decrypt password in asp.net using C#?[^]

Some CP Articles:
Simple encrypting and decrypting data in C#[^]
Encrypt and Decrypt Data with C#[^]
Encrypt/Decrypt String using DES in C#[^]
Encryption and Decryption on the .NET Framework[^]


By Using this piece of code you can encrypt your password written in the Textbox in asp.net application.

String myPassword = this.txtpassword.Text;
HashAlgorithm mhash = new SHA1CryptoServiceProvider();
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(myPassword);
byte[] bytHash = mhash.ComputeHash(bytValue);
mhash.Clear();
this.rtxtpassword.Text = Convert.ToBase64String(bytHash);



try it & suggest.