Password Encryption And Decryption

Here is cod for encryption and Decryption of the password.
For Encryption


Private Shared KEY_64() As Byte = {42, 16, 93, 156, 78, 4, 218, 32}
Private Shared IV_64() As Byte = {55, 103, 246, 79, 36, 99, 167, 3}

public static string Encryption(string value)
{
if (value != "")
{
DESCryptoServiceProvider CryptoProvidor = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, CryptoProvidor.CreateEncryptor(Key_64, Iv_64), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(value);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
}
return string.Empty;
}


For Decryption

public static string Descryption(string value)
{
if (value != "")
{
DESCryptoServiceProvider CryptoProvidor = new DESCryptoServiceProvider();
Byte[] buf = Convert.FromBase64String(value);
MemoryStream ms = new MemoryStream(buf);
CryptoStream cs = new CryptoStream(ms, CryptoProvidor.CreateDecryptor(Key_64, Iv_64), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
return string.Empty;
}


How to Use
----------
-> When User enter their new password then you pass the string to "Encryption" method they take one argument as as string this way

string strencryptpwd = Encryption(yourpassword);


this is return encrypt string store this code in the database.

-> When you retire that password from the database then pass that encrypt string to "Descryption" method they accept encrypt string and return orignal value. This way

string strencryptpwd = Descryption(yourencryptstring);


Comments

Author: VinC16 Sep 2008 Member Level: Silver   Points : 0

what is Key_64 and Iv_64 ? and where u r filling them ?

Author: vipul16 Sep 2008 Member Level: Gold   Points : 0

Sorry,
i can't add in the code now check.

Author: Devbrat Ghosh07 Oct 2008 Member Level: Bronze   Points : 2

Dear vipul,
Please update this article. Array declaration is done in Vb.net syntax as
Private Shared KEY_64() As Byte = {42, 16, 93, 156, 78, 4, 218, 32}
Private Shared IV_64() As Byte = {55, 103, 246, 79, 36, 99, 167, 3}
where it must be as
public static Byte[] Key_64= { 42,16,93,156,78,4,218,32 };
public static Byte[] Iv_64= { 55,103,246,79,36,99,167,3 };

Since in method Key_64 and Iv_64 is used ; so I changed it in declaration portion.
Thanks for your code. Its working fine.
Regards
Dev



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: