public override byte[] GetBytes (int cb)
{
#if NET_2_0
#pragma warning restore 809
#else
// 1.0/1.1 was a little late at throwing the argument exception ;-)
if (password == null)
throw new ArgumentNullException ("Password");
#endif
if (cb < 1)
throw new IndexOutOfRangeException ("cb");
if (state == 0) {
// it's now impossible to change the HashName, Salt
// and IterationCount
Reset ();
state = 1;
}
byte[] result = new byte [cb];
int cpos = 0;
// the initial hash (in reset) + at least one iteration
int iter = Math.Max (1, IterationsValue - 1);
// start with the PKCS5 key
if (output == null) {
// calculate the PKCS5 key
output = initial;
// generate new key material
for (int i = 0; i < iter - 1; i++)
output = hash.ComputeHash (output);
}
while (cpos < cb) {
byte[] output2 = null;
if (hashnumber == 0) {
// last iteration on output
output2 = hash.ComputeHash (output);
}
else if (hashnumber < 1000) {
string n = Convert.ToString (hashnumber);
output2 = new byte [output.Length + n.Length];
for (int j=0; j < n.Length; j++)
output2 [j] = (byte)(n [j]);
Buffer.BlockCopy (output, 0, output2, n.Length, output.Length);
// don't update output
output2 = hash.ComputeHash (output2);
}
else {
throw new CryptographicException (
Locale.GetText ("too long"));
}
int rem = output2.Length - position;
int l = Math.Min (cb - cpos, rem);
Buffer.BlockCopy (output2, position, result, cpos, l);
cpos += l;
position += l;
while (position >= output2.Length) {
position -= output2.Length;
hashnumber++;
}
}
return result;
}
protected static string DecryptString(string InputText, string Password) { try { RijndaelManaged RijndaelCipher = new RijndaelManaged(); byte[] EncryptedData = Convert.FromBase64String(InputText); byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString()); PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt); // Create a decryptor from the existing SecretKey bytes. ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(16), SecretKey.GetBytes(16)); MemoryStream memoryStream = new MemoryStream(EncryptedData); // Create a CryptoStream. (always use Read mode for decryption). CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read); // Since at this point we don't know what the size of decrypted data // will be, allocate the buffer long enough to hold EncryptedData; // DecryptedData is never longer than EncryptedData. byte[] PlainText = new byte[EncryptedData.Length]; // Start decrypting. int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length); memoryStream.Close(); cryptoStream.Close(); // Convert decrypted data into a string. string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount); // Return decrypted string. return DecryptedData; } catch (Exception exception) { return (exception.Message); } }