ASP.NET - decryption of the password - Asked By lakshmi chaitanya on 03-Jan-12 04:54 AM

How to decrypt  the encryption of the password.....
Sreekumar P replied to lakshmi chaitanya on 03-Jan-12 04:59 AM
Hi,

You should have the Encrypt Key with you, that you used to Encrypt.
And also u should either know the Algorithm or should have Code of Encryption.
Jitendra Faye replied to lakshmi chaitanya on 03-Jan-12 05:02 AM

To securely store a password so that it can be read back, use the ProtectedData class.



public static string ProtectPassword(string password)
{
    byte[] bytes = Encoding.Unicode.GetBytes(password);
    byte[] protectedPassword = ProtectedData.Protect(bytes, null, DataProtectionScope.CurrentUser);
    return Convert.ToBase64String(protectedPassword);
}

public static string UnprotectPassword(string protectedPassword)
{
    byte[] bytes = Convert.FromBase64String(protectedPassword);
    byte[] password = ProtectedData.Unprotect(bytes, null, DataProtectionScope.CurrentUser);
    return Encoding.Unicode.GetString(password);
}

hope this will help you.

Riley K replied to lakshmi chaitanya on 03-Jan-12 05:05 AM


What algorithm are you using to encrypt ??

The below code decrypts pwd using DESCryptoServiceProvider 

public static string Decrypt(string cryptedString)
{
  if (String.IsNullOrEmpty(cryptedString))
  {
    throw new ArgumentNullException
       ("The string which needs to be decrypted can not be null.");
  }
  DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  MemoryStream memoryStream = new MemoryStream
      (Convert.FromBase64String(cryptedString));
  CryptoStream cryptoStream = new CryptoStream(memoryStream,
    cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
  StreamReader reader = new StreamReader(cryptoStream);
  return reader.ReadToEnd();
}
http://www.codeproject.com/KB/cs/NET_Encrypt_Decrypt.aspx


Regards
smr replied to lakshmi chaitanya on 03-Jan-12 05:10 AM
hi

here is an example

private string encrypt(string str)
{
    string _result = string.Empty;
    char[] temp = str.ToCharArray();
    foreach (var _singleChar in temp)
    {
        var i = (int)_singleChar;
        i = i - 2;
        _result += (char)i;
    }
    return _result;
}
private string decrypt(string str)
{
    string _result = string.Empty;
    char[] temp = str.ToCharArray();
    foreach (var _singleChar in temp)
    {
        var i = (int)_singleChar;
        i = i + 2;
        _result += (char)i;
    }
    return _result;
}

follow
http://www.codeproject.com/Questions/195713/how-to-encrypt-and-decrypt-password-in-asp-net
dipa ahuja replied to lakshmi chaitanya on 03-Jan-12 06:03 AM
static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");
   
void passQueryString()
{
  string Encryptedcode = Encrypt(TextBox1.Text.ToString());
  Response.Redirect("products.aspx?code=" + Encryptedcode);
 
}
  
/* Encrypt */
 
public static string Encrypt(string originalString)
{
  if (String.IsNullOrEmpty(originalString))
  {
    throw new ArgumentNullException
      ("The string which needs to be encrypted can not be null.");
  }
  DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  MemoryStream memoryStream = new MemoryStream();
  CryptoStream cryptoStream = new CryptoStream(memoryStream,
    cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
  StreamWriter writer = new StreamWriter(cryptoStream);
  writer.Write(originalString);
  writer.Flush();
  cryptoStream.FlushFinalBlock();
  writer.Flush();
  return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
 
/* Decrypt */
public static string Decrypt(string cryptedString)
{
  if (String.IsNullOrEmpty(cryptedString))
  {
    throw new ArgumentNullException
      ("The string which needs to be decrypted can not be null.");
  }
  DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  MemoryStream memoryStream = new MemoryStream
    (Convert.FromBase64String(cryptedString));
  CryptoStream cryptoStream = new CryptoStream(memoryStream,
    cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
  StreamReader reader = new StreamReader(cryptoStream);
  return reader.ReadToEnd();
}