codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Code to encrypt the Password or any string  hanusoftware at 09:40 on Tuesday, July 10, 2007
 

/// <summary>

/// Thsi method retrieve the string to encrypt from the Presentation Layer

/// And return the Encrypted String

/// </summary>

/// <param name="str"></param>

/// <returns></returns>

public string encryptPassword(string strText)

{

return Encrypt(strText, "&%#@?,:*");

}

/// <summary>

/// This method retrieve the encrypted string to decrypt from the Presentation Layer

/// And return the decrypted string

/// </summary>

/// <param name="str"></param>

/// <returns></returns>

public string decryptPassword(string str)

{

return Decrypt(str, "&%#@?,:*");

}

/// <summary>

/// This method has been used to get the Encrypetd string for the

/// passed string

/// </summary>

/// <param name="strText"></param>

/// <param name="strEncrypt"></param>

/// <returns></returns>

private string Encrypt(string strText, string strEncrypt)

{

byte[] byKey = new byte[20];

byte[] dv ={ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

try

{

byKey = System.Text.Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));

DESCryptoServiceProvider des = new DESCryptoServiceProvider();

byte[] inputArray = System.Text.Encoding.UTF8.GetBytes(strText);

MemoryStream ms = new MemoryStream();

CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, dv), CryptoStreamMode.Write);

cs.Write(inputArray, 0, inputArray.Length);

cs.FlushFinalBlock();

return Convert.ToBase64String(ms.ToArray());

}

catch (Exception ex)

{

throw ex;

}

}

/// <summary>

/// This method has been used to Decrypt the Encrypted String

/// </summary>

/// <param name="strText"></param>

/// http://www.hanusoftware.com

/// <param name="strEncrypt"></param>

/// <returns></returns>

private string Decrypt(string strText, string strEncrypt)

{

byte[] bKey = new byte[20];

byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

try

{

bKey = System.Text.Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));

DESCryptoServiceProvider des = new DESCryptoServiceProvider();

Byte[] inputByteArray = inputByteArray = Convert.FromBase64String(strText);

MemoryStream ms = new MemoryStream();

CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(bKey, IV), CryptoStreamMode.Write);

cs.Write(inputByteArray, 0, inputByteArray.Length);

cs.FlushFinalBlock();

System.Text.Encoding encoding = System.Text.Encoding.UTF8;

return encoding.GetString(ms.ToArray());

}

catch (Exception ex)

{

throw ex;

}

}


Software Development India









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: horizontal scroll bar for drop down list box
•  Wheel Project
•  TO SEND Text message or URL through Clickatell
•  Re: JTable
•  Unix command output displayed 0 in browser
•  Perl 5.6 PPM is not working
•  Re: Need Code
•  telnet to a system with username and password using perl
•  Re: validating drop down


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007