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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Encryption/Decryption

Encryption/Decryption

Scheduled Pinned Locked Moved C#
databasesecurityregexannouncement
3 Posts 2 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.
  • J Offline
    J Offline
    just4ulove7
    wrote on last edited by
    #1

    Hi, I want to encrypt a password and store the encrypted version in the database. then when the user logs in, I want to decrypt the user password and match with the entered password. I read about DESCryptoServiceProvider, but still i am not able to understand it. Can someone send a link of article which is easy to understand/provide a piece of code here. It would be great for me. Thanks

    D 1 Reply Last reply
    0
    • J just4ulove7

      Hi, I want to encrypt a password and store the encrypted version in the database. then when the user logs in, I want to decrypt the user password and match with the entered password. I read about DESCryptoServiceProvider, but still i am not able to understand it. Can someone send a link of article which is easy to understand/provide a piece of code here. It would be great for me. Thanks

      D Offline
      D Offline
      Dave Shaw
      wrote on last edited by
      #2

      Hi, If you want to store a password in database, I suggest you use a 'One-Way Hash' algorythm. These are un-reversable encryption algorythms, hence the name one-way. Then you follow these steps to authenticate your users: 1. User Registers, Username and Hashed Password are stored in DB. 2. User types in Username and Password. P = password 3. The Software encrypts the Users Password with a One-Way Hash. P1 = Hash(P) 4. The Software queries the database and finds the password of the user (Storing Hashed in the database). P2 = database_pass 5. If the Hashed Pass in the DB and Hashed Pass the user typed in Match, Allow Login. If P1 == P2 then Login else Fail (remeber P1 = Hash(P)) Try this code to generate a one-way hash of a password. ------------------------------------------------------------------------------- private Byte[] GetByteArray( String originalString ) { Char[] charArray = originalString.ToCharArray(); Byte[] byteArray = new Byte[charArray.Length]; for ( int i=0; i /// Creates a one-way SHA1 hash of the pt string /// /// Plaintext to Hash /// Ciphertext string public string Hash(string pt) { //Implement SHA1 Hashing Algorythm (40 Bytes / 320 bits) byte[] data = new byte[40]; byte[] hash = new byte[40]; while (pt.Length % 4 != 0) pt += "g"; data = Convert.FromBase64String(pt); SHA1 sha = new SHA1CryptoServiceProvider(); hash = sha.ComputeHash(data); return Convert.ToBase64String(hash); } ------------------------------------------------------------------------------- Thanx! Dave Shaw History admires the wise, but elevates the brave. - Edmund Morris

      J 1 Reply Last reply
      0
      • D Dave Shaw

        Hi, If you want to store a password in database, I suggest you use a 'One-Way Hash' algorythm. These are un-reversable encryption algorythms, hence the name one-way. Then you follow these steps to authenticate your users: 1. User Registers, Username and Hashed Password are stored in DB. 2. User types in Username and Password. P = password 3. The Software encrypts the Users Password with a One-Way Hash. P1 = Hash(P) 4. The Software queries the database and finds the password of the user (Storing Hashed in the database). P2 = database_pass 5. If the Hashed Pass in the DB and Hashed Pass the user typed in Match, Allow Login. If P1 == P2 then Login else Fail (remeber P1 = Hash(P)) Try this code to generate a one-way hash of a password. ------------------------------------------------------------------------------- private Byte[] GetByteArray( String originalString ) { Char[] charArray = originalString.ToCharArray(); Byte[] byteArray = new Byte[charArray.Length]; for ( int i=0; i /// Creates a one-way SHA1 hash of the pt string /// /// Plaintext to Hash /// Ciphertext string public string Hash(string pt) { //Implement SHA1 Hashing Algorythm (40 Bytes / 320 bits) byte[] data = new byte[40]; byte[] hash = new byte[40]; while (pt.Length % 4 != 0) pt += "g"; data = Convert.FromBase64String(pt); SHA1 sha = new SHA1CryptoServiceProvider(); hash = sha.ComputeHash(data); return Convert.ToBase64String(hash); } ------------------------------------------------------------------------------- Thanx! Dave Shaw History admires the wise, but elevates the brave. - Edmund Morris

        J Offline
        J Offline
        just4ulove7
        wrote on last edited by
        #3

        Thanks a lot :)

        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