MD5 Random Hash Encryption algorithm

Source: Internet
Author: User
Tags md5 encryption

In the project, the password should be encrypted when the user name and password are authenticated, so the security aspect must be considered because it is a more commercialized software. The use of MD5 random hash encryption algorithm makes the password is not reversible after encryption, to a large extent, improve security. Nonsense not much to say, see example.

MD5 Encryption Class

[CSharp]View Plaincopy
  1. <span style="FONT-SIZE:18PX;"  >using System;
  2. Using System.Text;
  3. Using System.Security.Cryptography;
  4. Namespace Encrypttype
  5. {
  6. public class Encrypt
  7. {
  8. public String Encrypt (string strpwd)
  9. {
  10. String str = "";
  11. //Initialize the MD5 object
  12. MD5 MD5 = new MD5CryptoServiceProvider ();
  13. //Encode character as a byte array
  14. byte[] data = Encoding.Default.GetBytes (STRPWD);
  15. //Computes the hash value of the data byte array
  16. byte[] Md5data = md5.computehash (data);
  17. //Empty MD5
  18. MD5. Clear ();
  19. //Traversal md5data hash Array
  20. For (int i = 0; i < md5data.length-1; i++)
  21. {
  22. str + = Md5data[i]. ToString ("x").  PadLeft (2, ' 0 ');
  23. }
  24. return str;
  25. }
  26. }
  27. }
  28. </span>

Form code

[CSharp]View Plaincopy
  1. <span style="FONT-SIZE:18PX;" >//Defining variables
  2. String userName = "yh101";
  3. String PassWord = "1234";
  4. String userName = TxtUserName.Text.Trim ();
  5. String PassWord = TxtPwd.Text.Trim ();
  6. String str1 = "";
  7. String str2 = "";
  8. Instantiating cryptographic classes
  9. Encrypt Encrypt = new Encrypt ();
  10. Encrypt password
  11. String strpwd = Encrypt.encrypt (PassWord);
  12. Verify User Name
  13. if (userName! = TxtUserName.Text.Trim ())
  14. {
  15. MessageBox.Show ("user name does not exist, please re-enter!")  ");
  16. Txtusername.clear ();
  17. Txtusername.focus ();
  18. return;
  19. }
  20. Encrypt the password you entered
  21. String Strmima = Encrypt.encrypt (TxtPwd.Text.Trim ());
  22. Verify Password
  23. if (Strmima! = strpwd)
  24. {
  25. MessageBox.Show ("password is incorrect, login failed!  ");
  26. Txtpwd.clear ();
  27. Txtpwd.focus ();
  28. return;
  29. }
  30. MessageBox.Show ("Password is correct, login successful! "+ " Enter password: "+ Strmima + " Original password: "+ strpwd);</span>

Source: http://blog.csdn.net/happylee6688/article/details/8504067 of the Purple Feather Wind Blog

MD5 Random Hash Encryption algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.