[Mono-aspnet-list] fixing CspParameters not supported by Mono in CryptDeriveKey call
Andrus
kobruleht2 at hot.ee
Sat Dec 10 13:58:53 EST 2011
I try to port ASP .NET MVC2 password recovery code in http://stackoverflow.com/questions/7793108/where-to-find-c-sharp-sample-code-to-implement-password-recovery-in-asp-net-mvc
to Mono.
I it contains procedure below. In this CryptDeriveKey call causes exception in Mono
CspParameters not supported by Mono
How to implement password recovery in Mono in ASP .NET MVC2 application ?
/// <summary>
/// Takes a text message and encrypts it using a password as a key.
/// </summary>
/// <param name="plainMessage">A text to encrypt.</param>
/// <param name="password">The password to encrypt the message with.</param>
/// <returns>Encrypted string.</returns>
/// <remarks>This method uses TripleDES symmmectric encryption.</remarks>
public static string EncodeMessageWithPassword(string plainMessage, string password)
{
if (plainMessage == null)
throw new ArgumentNullException("encryptedMessage", "The message cannot be null");
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.IV = new byte[8];
//Creates the key based on the password and stores it in a byte array.
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]);
// in mono CryptDeriveKey causes exception:
// CspParameters not supported by Mono
des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
MemoryStream ms = new MemoryStream(plainMessage.Length * 2);
CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
byte[] plainBytes = Encoding.UTF8.GetBytes(plainMessage);
encStream.Write(plainBytes, 0, plainBytes.Length);
encStream.FlushFinalBlock();
byte[] encryptedBytes = new byte[ms.Length];
ms.Position = 0;
ms.Read(encryptedBytes, 0, (int)ms.Length);
encStream.Close();
return Convert.ToBase64String(encryptedBytes);
}
reference:
http://stackoverflow.com/questions/8459011/how-to-fix-cspparameters-not-supported-by-mono-exception-calling-cryptderivekey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-aspnet-list/attachments/20111210/e52268e6/attachment.html
More information about the Mono-aspnet-list
mailing list