Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Storing password

Storing password

Scheduled Pinned Locked Moved C#
databasequestioncsharp
13 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Reza Shojaee

    Hi everybody What is the best method for storing passwords in SQL database through C# ?

    Best Regards, Reza Shojaee

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    I create MD5 hash from the password and store it in SQL database.

    R OriginalGriffO U 3 Replies Last reply
    0
    • L Lost User

      I create MD5 hash from the password and store it in SQL database.

      R Offline
      R Offline
      Reza Shojaee
      wrote on last edited by
      #3

      How can create MD5 hash in C#?

      Best Regards, Reza Shojaee

      A OriginalGriffO 2 Replies Last reply
      0
      • R Reza Shojaee

        Hi everybody What is the best method for storing passwords in SQL database through C# ?

        Best Regards, Reza Shojaee

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #4

        Rather than storing the password, store a hash of it. Encrypt and "salt" the password before modifying it to get the hash. Store this hash. Then, when the user enters their password, create the hash from it and compare it to the hash you have stored in the database. Doing it this way will prevent somebody who gains access to your database from learning any of the passwords, but still gives you the ability to use passwords to secure data and transactions.

        [Forum Guidelines]

        1 Reply Last reply
        0
        • R Reza Shojaee

          How can create MD5 hash in C#?

          Best Regards, Reza Shojaee

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #5

          Use Google.

          [Forum Guidelines]

          1 Reply Last reply
          0
          • L Lost User

            I create MD5 hash from the password and store it in SQL database.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #6

            You shouldn't use MD5 for new applications - it is officially "broken". Use SHA-512 instead, as it is currently ok until the SHA-1024 spec is released in 2012.

            You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • R Reza Shojaee

              Hi everybody What is the best method for storing passwords in SQL database through C# ?

              Best Regards, Reza Shojaee

              A Offline
              A Offline
              Abhinav S
              wrote on last edited by
              #7

              Encrypt it before storing it into the database.

              Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
              Honestly. It's the honest ones you want to watch out for...

              1 Reply Last reply
              0
              • R Reza Shojaee

                How can create MD5 hash in C#?

                Best Regards, Reza Shojaee

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #8

                Don't use MD5 - it is officially "broken". Use SHA instead. Attached the class I use for SHA handling:

                using System;
                using System.Text;
                using System.Security.Cryptography;
                using System.Runtime.InteropServices;
                using System.Security;
                using System.IO;

                namespace UtilityControls
                {
                /// <summary>
                /// Supports SHA2 hash processing
                /// NOTE: SHA3 is under developemnt and the specification is due in 2012
                /// </summary>
                public class SHA2Hash
                {
                #region Fields
                private byte[] _SHA2Data;
                /// <summary>
                /// Bits in an SHA2 hash
                /// </summary>
                public const int SHA2Bits = 512;
                /// <summary>
                /// Bytes in an SHA2 hash
                /// </summary>
                public const int SHA2Bytes = SHA2Bits / 8;
                /// <summary>
                /// Size of SHA2Hash string
                /// </summary>
                public const int Length = SHA2Bytes * 2; // As in SHA2 as a string...
                #endregion

                    #region Properties
                    /// <summary>
                    /// Returns the SHA2 hash as a string
                    /// </summary>
                    public string SHA2data
                        {
                        get
                            {
                            StringBuilder sb = new StringBuilder(Length);
                            foreach (byte b in \_SHA2Data)
                                {
                                sb.Append(b.ToString("x2"));
                                }
                            return sb.ToString();
                            }
                        }
                    #endregion
                
                    #region Constructors
                    /// <summary>
                    /// Constructs an SHA2 hash from a stream
                    /// </summary>
                    /// <param name="s">stream, data to construct SHA2 from</param>
                    public SHA2Hash(Stream s)
                        {
                        SHA512 shaM = new SHA512Managed();
                        \_SHA2Data = shaM.ComputeHash(s);
                        }
                
                    /// <summary>
                    /// Constructs an SHA2 hash from a SecureString
                    /// </summary>
                    /// <param name="ss">SecureString, data to construct SHA2 from</param>
                    public SHA2Hash(SecureString ss)
                        {
                        SHA512 shaM = new SHA512Managed();
                        if (ss != null)
                            {
                            IntPtr ptr = Marshal.SecureStringToBSTR(ss);
                            byte\[\] bs = Encoding.UTF8.GetBytes(Marshal.PtrToStringAuto(ptr));
                            \_SHA2Data = shaM.ComputeHash(bs);
                            Marshal.ZeroFreeBSTR(ptr);
                

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                A L D 3 Replies Last reply
                0
                • OriginalGriffO OriginalGriff

                  Don't use MD5 - it is officially "broken". Use SHA instead. Attached the class I use for SHA handling:

                  using System;
                  using System.Text;
                  using System.Security.Cryptography;
                  using System.Runtime.InteropServices;
                  using System.Security;
                  using System.IO;

                  namespace UtilityControls
                  {
                  /// <summary>
                  /// Supports SHA2 hash processing
                  /// NOTE: SHA3 is under developemnt and the specification is due in 2012
                  /// </summary>
                  public class SHA2Hash
                  {
                  #region Fields
                  private byte[] _SHA2Data;
                  /// <summary>
                  /// Bits in an SHA2 hash
                  /// </summary>
                  public const int SHA2Bits = 512;
                  /// <summary>
                  /// Bytes in an SHA2 hash
                  /// </summary>
                  public const int SHA2Bytes = SHA2Bits / 8;
                  /// <summary>
                  /// Size of SHA2Hash string
                  /// </summary>
                  public const int Length = SHA2Bytes * 2; // As in SHA2 as a string...
                  #endregion

                      #region Properties
                      /// <summary>
                      /// Returns the SHA2 hash as a string
                      /// </summary>
                      public string SHA2data
                          {
                          get
                              {
                              StringBuilder sb = new StringBuilder(Length);
                              foreach (byte b in \_SHA2Data)
                                  {
                                  sb.Append(b.ToString("x2"));
                                  }
                              return sb.ToString();
                              }
                          }
                      #endregion
                  
                      #region Constructors
                      /// <summary>
                      /// Constructs an SHA2 hash from a stream
                      /// </summary>
                      /// <param name="s">stream, data to construct SHA2 from</param>
                      public SHA2Hash(Stream s)
                          {
                          SHA512 shaM = new SHA512Managed();
                          \_SHA2Data = shaM.ComputeHash(s);
                          }
                  
                      /// <summary>
                      /// Constructs an SHA2 hash from a SecureString
                      /// </summary>
                      /// <param name="ss">SecureString, data to construct SHA2 from</param>
                      public SHA2Hash(SecureString ss)
                          {
                          SHA512 shaM = new SHA512Managed();
                          if (ss != null)
                              {
                              IntPtr ptr = Marshal.SecureStringToBSTR(ss);
                              byte\[\] bs = Encoding.UTF8.GetBytes(Marshal.PtrToStringAuto(ptr));
                              \_SHA2Data = shaM.ComputeHash(bs);
                              Marshal.ZeroFreeBSTR(ptr);
                  
                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #9

                  FYI, looks like you encoded your ending PRE tag. And you may want to specify a lang attribute on that PRE tag (though I'm sure the code will not be looked at... probably just copy/pasted... so perhaps that's a moot point).

                  [Forum Guidelines]

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Don't use MD5 - it is officially "broken". Use SHA instead. Attached the class I use for SHA handling:

                    using System;
                    using System.Text;
                    using System.Security.Cryptography;
                    using System.Runtime.InteropServices;
                    using System.Security;
                    using System.IO;

                    namespace UtilityControls
                    {
                    /// <summary>
                    /// Supports SHA2 hash processing
                    /// NOTE: SHA3 is under developemnt and the specification is due in 2012
                    /// </summary>
                    public class SHA2Hash
                    {
                    #region Fields
                    private byte[] _SHA2Data;
                    /// <summary>
                    /// Bits in an SHA2 hash
                    /// </summary>
                    public const int SHA2Bits = 512;
                    /// <summary>
                    /// Bytes in an SHA2 hash
                    /// </summary>
                    public const int SHA2Bytes = SHA2Bits / 8;
                    /// <summary>
                    /// Size of SHA2Hash string
                    /// </summary>
                    public const int Length = SHA2Bytes * 2; // As in SHA2 as a string...
                    #endregion

                        #region Properties
                        /// <summary>
                        /// Returns the SHA2 hash as a string
                        /// </summary>
                        public string SHA2data
                            {
                            get
                                {
                                StringBuilder sb = new StringBuilder(Length);
                                foreach (byte b in \_SHA2Data)
                                    {
                                    sb.Append(b.ToString("x2"));
                                    }
                                return sb.ToString();
                                }
                            }
                        #endregion
                    
                        #region Constructors
                        /// <summary>
                        /// Constructs an SHA2 hash from a stream
                        /// </summary>
                        /// <param name="s">stream, data to construct SHA2 from</param>
                        public SHA2Hash(Stream s)
                            {
                            SHA512 shaM = new SHA512Managed();
                            \_SHA2Data = shaM.ComputeHash(s);
                            }
                    
                        /// <summary>
                        /// Constructs an SHA2 hash from a SecureString
                        /// </summary>
                        /// <param name="ss">SecureString, data to construct SHA2 from</param>
                        public SHA2Hash(SecureString ss)
                            {
                            SHA512 shaM = new SHA512Managed();
                            if (ss != null)
                                {
                                IntPtr ptr = Marshal.SecureStringToBSTR(ss);
                                byte\[\] bs = Encoding.UTF8.GetBytes(Marshal.PtrToStringAuto(ptr));
                                \_SHA2Data = shaM.ComputeHash(bs);
                                Marshal.ZeroFreeBSTR(ptr);
                    
                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    Thanks Griff, just what I need.

                    txtspeak is the realm of 9 year old children, not developers. Christian Graus

                    1 Reply Last reply
                    0
                    • L Lost User

                      I create MD5 hash from the password and store it in SQL database.

                      U Offline
                      U Offline
                      Un Known Legend
                      wrote on last edited by
                      #11

                      Hi please check this link http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5.aspx thanks

                      1 Reply Last reply
                      0
                      • R Reza Shojaee

                        Hi everybody What is the best method for storing passwords in SQL database through C# ?

                        Best Regards, Reza Shojaee

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #12

                        See here[^].

                        1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          Don't use MD5 - it is officially "broken". Use SHA instead. Attached the class I use for SHA handling:

                          using System;
                          using System.Text;
                          using System.Security.Cryptography;
                          using System.Runtime.InteropServices;
                          using System.Security;
                          using System.IO;

                          namespace UtilityControls
                          {
                          /// <summary>
                          /// Supports SHA2 hash processing
                          /// NOTE: SHA3 is under developemnt and the specification is due in 2012
                          /// </summary>
                          public class SHA2Hash
                          {
                          #region Fields
                          private byte[] _SHA2Data;
                          /// <summary>
                          /// Bits in an SHA2 hash
                          /// </summary>
                          public const int SHA2Bits = 512;
                          /// <summary>
                          /// Bytes in an SHA2 hash
                          /// </summary>
                          public const int SHA2Bytes = SHA2Bits / 8;
                          /// <summary>
                          /// Size of SHA2Hash string
                          /// </summary>
                          public const int Length = SHA2Bytes * 2; // As in SHA2 as a string...
                          #endregion

                              #region Properties
                              /// <summary>
                              /// Returns the SHA2 hash as a string
                              /// </summary>
                              public string SHA2data
                                  {
                                  get
                                      {
                                      StringBuilder sb = new StringBuilder(Length);
                                      foreach (byte b in \_SHA2Data)
                                          {
                                          sb.Append(b.ToString("x2"));
                                          }
                                      return sb.ToString();
                                      }
                                  }
                              #endregion
                          
                              #region Constructors
                              /// <summary>
                              /// Constructs an SHA2 hash from a stream
                              /// </summary>
                              /// <param name="s">stream, data to construct SHA2 from</param>
                              public SHA2Hash(Stream s)
                                  {
                                  SHA512 shaM = new SHA512Managed();
                                  \_SHA2Data = shaM.ComputeHash(s);
                                  }
                          
                              /// <summary>
                              /// Constructs an SHA2 hash from a SecureString
                              /// </summary>
                              /// <param name="ss">SecureString, data to construct SHA2 from</param>
                              public SHA2Hash(SecureString ss)
                                  {
                                  SHA512 shaM = new SHA512Managed();
                                  if (ss != null)
                                      {
                                      IntPtr ptr = Marshal.SecureStringToBSTR(ss);
                                      byte\[\] bs = Encoding.UTF8.GetBytes(Marshal.PtrToStringAuto(ptr));
                                      \_SHA2Data = shaM.ComputeHash(bs);
                                      Marshal.ZeroFreeBSTR(ptr);
                          
                          D Offline
                          D Offline
                          Dan Mos
                          wrote on last edited by
                          #13

                          nice example. thanks :)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups