| | |
Rijndael algo for Encryption and Decryption of Password
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2006
Posts: 116
Reputation:
Solved Threads: 0
I am trying to encrypt and decrypt the password in ASP.NEt2.0 application with Rijndael
algo.
I am able to encrypt the password but not able to decrypt it.
Can anybody tell me what is the problem with my code,
I am pasting it here
public static string Encrypt(string StringToEncrypt, string Key)
{
Rijndael _encryptionservice = new RijndaelManaged();
ICryptoTransform _encryptor;
byte[] _bytesdata;
byte[] _byteskey;
string _encryptedstring;
Key = Key.ToLower();
_bytesdata = Encoding.ASCII.GetBytes(StringToEncrypt.PadRight(16));
_byteskey = Encoding.ASCII.GetBytes(Key.PadRight(32).Substring(0, 32));
_encryptionservice.Mode = CipherMode.CBC;
_encryptionservice.Key = _byteskey;
//_encryptionservice.IV = Encoding.ASCII.GetBytes("VIMSuitEncoding");
_encryptor = _encryptionservice.CreateEncryptor();
MemoryStream _memstreamencrypteddata = new MemoryStream();
CryptoStream encStream = new CryptoStream(_memstreamencrypteddata, _encryptor, CryptoStreamMode.Write);
try
{
encStream.Write(_bytesdata, 0, _bytesdata.Length);
}
catch (Exception ex)
{
throw new Exception("Error while writing encrypted data to the stream", ex);
}
encStream.FlushFinalBlock();
encStream.Close();
_encryptedstring = Convert.ToBase64String(_memstreamencrypteddata.ToArray());
return _encryptedstring;
}
public static string Decrypt(string StringToDecrypt, string Key)
{
Rijndael _encryptionservice = new RijndaelManaged();
byte[] _bytesdata;
byte[] _byteskey;
string _encryptedstring;
ICryptoTransform _decryptor;
int _decryptedstringlength;
Key = Key.ToLower();
_bytesdata = Convert.FromBase64String(StringToDecrypt);
_byteskey = Encoding.ASCII.GetBytes(Key.PadRight(32).Substring(0, 32));
_encryptionservice.Mode =CipherMode.CBC;
_encryptionservice.Key = _byteskey;
//_encryptionservice.IV = Encoding.ASCII.GetBytes("VIMSuitEncoding");
_decryptor = _encryptionservice.CreateDecryptor();
MemoryStream _memstreamencrypteddata = new MemoryStream(_bytesdata);
CryptoStream encStream = new CryptoStream(_memstreamencrypteddata, _decryptor, CryptoStreamMode.Read);
//_bytesdata = new byte[_bytesdata.Length];
try
{
_decryptedstringlength = encStream.Read(_bytesdata, 0, _bytesdata.Length);
}
catch (Exception ex)
{
throw new Exception("Error while writing encrypted data to the stream", ex);
}
encStream.Close();
//_encryptedstring = Convert.ToString(Encoding.ASCII.GetChars(_memstreamencrypteddata.ToArray())).Substring(0, _decryptedstringlength);
try
{
_encryptedstring = Convert.ToString(Encoding.ASCII.GetChars(_memstreamencrypteddata.ToArray())).Substring(0, _decryptedstringlength);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return _encryptedstring.Trim();
}
}
algo.
I am able to encrypt the password but not able to decrypt it.
Can anybody tell me what is the problem with my code,
I am pasting it here
public static string Encrypt(string StringToEncrypt, string Key)
{
Rijndael _encryptionservice = new RijndaelManaged();
ICryptoTransform _encryptor;
byte[] _bytesdata;
byte[] _byteskey;
string _encryptedstring;
Key = Key.ToLower();
_bytesdata = Encoding.ASCII.GetBytes(StringToEncrypt.PadRight(16));
_byteskey = Encoding.ASCII.GetBytes(Key.PadRight(32).Substring(0, 32));
_encryptionservice.Mode = CipherMode.CBC;
_encryptionservice.Key = _byteskey;
//_encryptionservice.IV = Encoding.ASCII.GetBytes("VIMSuitEncoding");
_encryptor = _encryptionservice.CreateEncryptor();
MemoryStream _memstreamencrypteddata = new MemoryStream();
CryptoStream encStream = new CryptoStream(_memstreamencrypteddata, _encryptor, CryptoStreamMode.Write);
try
{
encStream.Write(_bytesdata, 0, _bytesdata.Length);
}
catch (Exception ex)
{
throw new Exception("Error while writing encrypted data to the stream", ex);
}
encStream.FlushFinalBlock();
encStream.Close();
_encryptedstring = Convert.ToBase64String(_memstreamencrypteddata.ToArray());
return _encryptedstring;
}
public static string Decrypt(string StringToDecrypt, string Key)
{
Rijndael _encryptionservice = new RijndaelManaged();
byte[] _bytesdata;
byte[] _byteskey;
string _encryptedstring;
ICryptoTransform _decryptor;
int _decryptedstringlength;
Key = Key.ToLower();
_bytesdata = Convert.FromBase64String(StringToDecrypt);
_byteskey = Encoding.ASCII.GetBytes(Key.PadRight(32).Substring(0, 32));
_encryptionservice.Mode =CipherMode.CBC;
_encryptionservice.Key = _byteskey;
//_encryptionservice.IV = Encoding.ASCII.GetBytes("VIMSuitEncoding");
_decryptor = _encryptionservice.CreateDecryptor();
MemoryStream _memstreamencrypteddata = new MemoryStream(_bytesdata);
CryptoStream encStream = new CryptoStream(_memstreamencrypteddata, _decryptor, CryptoStreamMode.Read);
//_bytesdata = new byte[_bytesdata.Length];
try
{
_decryptedstringlength = encStream.Read(_bytesdata, 0, _bytesdata.Length);
}
catch (Exception ex)
{
throw new Exception("Error while writing encrypted data to the stream", ex);
}
encStream.Close();
//_encryptedstring = Convert.ToString(Encoding.ASCII.GetChars(_memstreamencrypteddata.ToArray())).Substring(0, _decryptedstringlength);
try
{
_encryptedstring = Convert.ToString(Encoding.ASCII.GetChars(_memstreamencrypteddata.ToArray())).Substring(0, _decryptedstringlength);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return _encryptedstring.Trim();
}
}
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: How to capture ASP.NET2.0 control's event at client side
- Next Thread: Remember username and password in ASP.NET2.0
| Thread Tools | Search this Thread |
.net 2.0 activexcontrol advice ajax alltypeofvideos application asp asp.net bc30451 bottomasp.net box browser button c# c#gridviewcolumn checkbox click commonfunctions confirmationcodegeneration css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dynamically edit expose feedback fileuploader fill flash form formatdecimal forms formview grid gridview gudi homeedition hosting iframe iis javascript jquery listbox login microsoft mono mouse mssql multistepregistration news numerical objects opera panelmasterpagebuttoncontrols parent radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security select silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking unauthorized validation vb.net video videos view virtualdirectory vista visualstudio web webapplications webdevelopemnt webprogramming webservice xml xsl youareanotmemberofthedebuggerusers





