Results 1 to 11 of 11

Thread: Encrypting Passwords

  1. #1

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121

    Encrypting Passwords

    I'm storing passwords in a database and don't want some Joe going in and finding out what the passwords are. So I was wondering how I make a one-way encryption to encrypt the password.

    Thanks in advanced

  2. #2
    Junior Member husain's Avatar
    Join Date
    Jul 2002
    Location
    United Arab Emirates
    Posts
    31
    You can use the MD5 encryption technique. I use this class for encrypting text.
    Code:
    public class TextEncryption
    {
      public TextEncryption()
      {
        // Empty constructor
      }
    
      public static string MD5(string text)
      {
        byte[] tempSource = ASCIIEncoding.ASCII.GetBytes(text);
        byte[] tempHash = new MD5CryptoServiceProvider().ComputeHash(tempSource);
        return ByteToString(tempHash);
      }
    
      private static string ByteToString(byte[] input)
      {
        StringBuilder s = new StringBuilder(input.Length);
        for (int i = 0; i < input.Length; i++)
        {
          s.Append(input[i].ToString("X2"));
        }
        return s.ToString();
      }  
    }

  3. #3

    Thread Starter
    Lively Member Cagez's Avatar
    Join Date
    Oct 2002
    Posts
    121
    Thanks, works great

    I'm new to C# and I couldn't get it working, then I realised the 'static' keyword! I kept creating a object And it took me a while to figure you were using System.Text also... What can I say

    Thanks again

  4. #4
    Junior Member husain's Avatar
    Join Date
    Jul 2002
    Location
    United Arab Emirates
    Posts
    31
    I am so sorry! I forgot to mention what was supposed to be imported. Good you were able to figure it out

  5. #5
    Member
    Join Date
    Mar 2003
    Posts
    34
    Just for future reference, I wrote this encryption class that uses the symmetric TripleDES algorithm. Alot of the encryption examples I've seen posted on the web make heavy use of the FileStream. I cut out the file i/o, and use the a MemoryStream as my backing store. You could use this class for tighter encryption.:

    Code:
    using System;
    using System.Text;
    using System.IO;
    using System.Security.Cryptography;
    using Encryption;
    
    namespace Encryption {
    
        public class TripleDESEncryption {
    
    	public static string EncryptData(string data, out byte[] desKey, out byte[] desIV) {		
       	    MemoryStream output = new MemoryStream();		
    	    byte[] byteData = new UnicodeEncoding().GetBytes(data);
    
      	    //Use the TripleDES symmetric encryption algorithm to encrypt our data. Without an IV, the 			
       	    //same input block of plaintext will encrypt to same output block of ciphertext. IV guarantees 
    	    //output of two identical plaintext blocks are different. 
    	    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    	    CryptoStream crypt = new CryptoStream(output, des.CreateEncryptor(), CryptoStreamMode.Write);
    		
    	    //Assign our crypto-generated key and iv values to our output arguments
    	    desKey = des.Key; desIV = des.IV;
    	    crypt.Write(byteData, 0, byteData.Length);			
    
        	    crypt.Close(); output.Close(); 		
    	    return new UnicodeEncoding().GetString(output.ToArray());
    	}
    
    	public static string EncryptData(byte[] desKey, byte[] desIV, string data) {		
    	    MemoryStream output = new MemoryStream();		
    	    byte[] byteData = new UnicodeEncoding().GetBytes(data);
    
       	    //Use the TripleDES symmetric encryption algorithm to encrypt our data. Without an IV, the 			
      	    //same input block of plaintext will encrypt to same output block of ciphertext. IV guarantees 
    	    //output of two identical plaintext blocks are different. 
    	    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    	    CryptoStream crypt = new CryptoStream(output, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
    	    crypt.Write(byteData, 0, byteData.Length);			
    
                crypt.Close(); output.Close(); 		
    	    return new UnicodeEncoding().GetString(output.ToArray());
    	}
    
    	public static string DecryptData(string data, byte[] desKey, byte[] desIV) {
    	    MemoryStream output = new MemoryStream();
    	    byte[] byteData = new UnicodeEncoding().GetBytes(data);
    
      	    //Use the TripleDES symmetric encryption algorithm to decrypt our data. In order for the ciphertext to be
    	    //successfully decrypted, the exact same key and iv must be used when initially encryted.
    	    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    	    CryptoStream crypt = new CryptoStream(output, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
    	    crypt.Write(byteData, 0, byteData.Length);
    
     	    crypt.Close(); output.Close(); 		
    	    return new UnicodeEncoding().GetString(output.ToArray());
    	}
        }
    }
    
    class CipherText {
    
        static void Main() {
    	string password = "secret";
    	//*************************
    	byte[] key; byte[] iv; 		
    	string cif = TripleDESEncryption.EncryptData(password, out key, out iv);
    	Console.WriteLine(TripleDESEncryption.DecryptData(cif, key, iv));
    	
    	//**********************************************************************
            //NOTE: Key and IVector must be 16 bytes each
    	//byte[] key = UnicodeEncoding.Unicode.GetBytes("abcdefgh");	
    	//string cif = TripleDESEncryption.EncryptData(key, key, password);
    	//Console.WriteLine(TripleDESEncryption.DecryptData(cif, key, key));
        }
    }
    Last edited by SimonVega; Mar 14th, 2003 at 11:15 AM.
    AKA 'Lethal'

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    SimonVega: What is the AKA 'Lethal' mean in your sig? Are you Lethal under a new name?

  7. #7
    Member
    Join Date
    Mar 2003
    Posts
    34
    Yep, thats me..My Lethal account is all screwed up. I can log in but I can't reply or create threads. I've contacted the admins, hopefully I can get this squared away soon..
    AKA 'Lethal'

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Cool,

    My account had gotten screwed up a while back. Didn't know what happened, but I was allowed to recreate the same user name, but post count was lost along with searching for all my past posts....it sucks.

    Did you take the MS test yet for .Net? I am still studying and learning. I have a lot to know before I can take them.

  9. #9
    Member
    Join Date
    Mar 2003
    Posts
    34
    No, I haven't gotten around to taken any of them yet. I actually just got a new job last week as a C# developer for a software company. I'm part of the UI team for the first few months, so I've been brushing up on the GUI widgets and whatnot. Hopefully, I'll be ready for the windows exam here soon.
    AKA 'Lethal'

  10. #10
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: Encrypting Passwords

    hello! SimonVega
    ur class has has only 8 chars, how do create more than 8 characters?

  11. #11
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Encrypting Passwords

    Probably try Rijndael encryption.

    Jennifer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width