?
Solved

Encrypting Passwords

Posted on 2006-05-08
2
Medium Priority
?
727 Views
Last Modified: 2008-03-03
I am trying to use some code I found online to encrypt and decrypt passwords to store them in a database. I have enclosed the code below. The problem is that the class cannot be seen by visual stidio, unless I add the static key word to the methods, but once I do that, I get an error that says that an object reference must be used with a non-static field. I am farly new to this and would just like to get by it, so any help would be appreciated.

Thank You in advance
CJSantora


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Specialized;
using System.Security.Cryptography;

/// <summary>
/// Summary description for encrypt
/// </summary>
public class encrypt
{
        private  byte[] lbtVector = {240, 3, 45, 29, 0, 76, 173, 59};
        private string lscryptoKey = "";
            
        private encrypt()
            {
                  lscryptoKey = "ChangeThis";
            }

            public string psDecrypt(string sQueryString)
            {

                  byte[] buffer;
                  TripleDESCryptoServiceProvider loCryptoClass = new TripleDESCryptoServiceProvider();
                  MD5CryptoServiceProvider loCryptoProvider = new MD5CryptoServiceProvider();

                  try
                  {

                        buffer = Convert.FromBase64String(sQueryString);
                        loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey));
                        loCryptoClass.IV = lbtVector;
                        return Encoding.ASCII.GetString(loCryptoClass.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
                  }
                  catch(Exception ex)
                  {
                        throw ex;
                  }
                  finally
                  {
                        loCryptoClass.Clear();
                        loCryptoProvider.Clear();
                        loCryptoClass = null;
                        loCryptoProvider = null;

                  }
            }

            
            //Author      :       Nikhil Gupta
            //Description :       This function encrypts a given string
            //Parameters  :       String
            //Return Values:      Encrypted String
            //Called From :       Business Layer
            public string psEncrypt(string sInputVal)
            {

                  TripleDESCryptoServiceProvider loCryptoClass = new TripleDESCryptoServiceProvider();
                  MD5CryptoServiceProvider loCryptoProvider = new MD5CryptoServiceProvider();
                  byte[] lbtBuffer;

                  try
                  {
                        lbtBuffer = System.Text.Encoding.ASCII.GetBytes(sInputVal);
                        loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey));
                        loCryptoClass.IV = lbtVector;
                        sInputVal = Convert.ToBase64String(loCryptoClass.CreateEncryptor().TransformFinalBlock(lbtBuffer, 0, lbtBuffer.Length));
                        return sInputVal;
                  }
                  catch (CryptographicException ex)
                  {
                        throw ex;
                  }
                  catch(FormatException ex)
                  {
                        throw ex;
                  }
                  catch(Exception ex)
                  {
                        throw ex;
                  }
                  finally
                  {
                        loCryptoClass.Clear();
                        loCryptoProvider.Clear();
                        loCryptoClass = null;
                        loCryptoProvider = null;
                  }
            }
}

0
Comment
Question by:CJSantora
2 Comments
 
LVL 12

Accepted Solution

by:
sumix earned 2000 total points
ID: 16633630

you cannot creat an instance of this class because it has a private constructor. You can either make it public, or static (
   static encrypt()
    { lscryptoKey = "ChangeThis;}
  in this case you will also need to make lscryptoKey static:
      private static string lscryptoKey = "";
 )
0
 

Author Comment

by:CJSantora
ID: 16633861
I appreciate the help, I was pretty sure I was just over looking something simple, I just needed another set of eyes.

CJSANTora
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question