|
-
Mar 12th, 2003, 10:18 PM
#1
Thread Starter
Lively Member
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
-
Mar 13th, 2003, 11:12 AM
#2
Junior Member
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();
}
}
-
Mar 13th, 2003, 01:33 PM
#3
Thread Starter
Lively Member
-
Mar 14th, 2003, 12:42 AM
#4
Junior Member
I am so sorry! I forgot to mention what was supposed to be imported. Good you were able to figure it out
-
Mar 14th, 2003, 11:11 AM
#5
Member
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'
-
Mar 14th, 2003, 12:37 PM
#6
PowerPoster
SimonVega: What is the AKA 'Lethal' mean in your sig? Are you Lethal under a new name?
-
Mar 14th, 2003, 12:50 PM
#7
Member
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..
-
Mar 14th, 2003, 01:42 PM
#8
PowerPoster
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.
-
Mar 14th, 2003, 01:47 PM
#9
Member
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.
-
Jun 6th, 2007, 03:16 AM
#10
Hyperactive Member
Re: Encrypting Passwords
hello! SimonVega
ur class has has only 8 chars, how do create more than 8 characters?
-
Jun 11th, 2007, 06:09 AM
#11
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|