Guest User

Untitled

a guest
Aug 27th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. C# Password Encryption
  2. string UserName = txtUser.Text;
  3. string password = txtPass.Text;
  4.  
  5. string encrKey = "keyvalue";
  6. byte[] byteKey = { };
  7. byte[] IV = {25, 47, 60, 88, 99, 106, 125, 139};
  8. byteKey = Encoding.UTF8.GetBytes(encrKey.Substring(0, 8));
  9. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  10. byte[] inputArray = Encoding.UTF8.GetBytes(password);
  11.  
  12. MemoryStream ms = new MemoryStream();
  13. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byteKey, IV), CryptoStreamMode.Write);
  14. cs.Write(inputArray, 0, inputArray.Length);
  15. cs.FlushFinalBlock();
  16. password = Convert.ToBase64String(ms.ToArray());
  17.  
  18. SqlCommand cmd = new SqlCommand("INSERT INTO USers (UserName, Password) VALUES (@UserName, @Password)", myConnection);
  19. cmd.CommandType = CommandType.Text;
  20.  
  21. cmd.Parameters.AddWithValue("@UserName", UserName);
  22. cmd.Parameters.AddWithValue("@Password", password);
  23.  
  24. SqlDataReader rdr = cmd.ExecuteReader();
  25.  
  26. var hash = Encoding.ASCII.GetBytes(password);
  27. var md5 = new MD5CryptoServiceProvider();
  28. var md5hash = md5.ComputeHash(hash);
  29. var hashedPassword = ASCIIEncoding.GetString(md5hash);
  30.  
  31. var hash = Encoding.ASCII.GetBytes(password);
  32. var sha1 = new SHA1CryptoServiceProvider();
  33. var sha1hash = sha1.ComputeHash(hash);
  34. var hashedPassword = ASCIIEncoding.GetString(sha1hash);
  35.  
  36. public sealed class PasswordHash
  37. {
  38. const int SaltSize = 16, HashSize = 20, HashIter = 10000;
  39. readonly byte[] _salt, _hash;
  40. public PasswordHash(string password)
  41. {
  42. new RNGCryptoServiceProvider().GetBytes(_salt = new byte[SaltSize]);
  43. _hash = new Rfc2898DeriveBytes(password, _salt, HashIter).GetBytes(HashSize);
  44. }
  45. public PasswordHash(byte[] hashBytes)
  46. {
  47. Array.Copy(hashBytes, 0, _salt = new byte[SaltSize], 0, SaltSize);
  48. Array.Copy(hashBytes, SaltSize, _hash = new byte[HashSize], 0, HashSize);
  49. }
  50. public PasswordHash(byte[] salt, byte[] hash)
  51. {
  52. Array.Copy(salt, 0, _salt = new byte[SaltSize], 0, SaltSize);
  53. Array.Copy(hash, 0, _hash = new byte[HashSize], 0, HashSize);
  54. }
  55. public byte[] ToArray()
  56. {
  57. byte[] hashBytes = new byte[SaltSize + HashSize];
  58. Array.Copy(_salt, 0, hashBytes, 0, SaltSize);
  59. Array.Copy(_hash, 0, hashBytes, SaltSize, HashSize);
  60. return hashBytes;
  61. }
  62. public byte[] Salt { get { return (byte[])_salt.Clone(); } }
  63. public byte[] Hash { get { return (byte[])_hash.Clone(); } }
  64. public bool Verify(string password)
  65. {
  66. byte[] test = new Rfc2898DeriveBytes(password, _salt, HashIter).GetBytes(HashSize);
  67. for (int i = 0; i < HashSize; i++)
  68. if (test[i] != _hash[i])
  69. return false;
  70. return true;
  71. }
  72. }
Add Comment
Please, Sign In to add comment