when I decrypt the encrypted text it
doesn't decrypt the first few words of the document.
My Encryption method is :
public string EncryptString(string plainText,string key)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Key =CreateKey(key);
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform rijndaelEncryptor = rijndaelCipher.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelEncryptor, CryptoStreamMode.Write);
byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
return cipherText;
}
private byte[] CreateKey(string password)
{
var salt = new byte[] { 1, 2, 23, 234, 37, 48, 134, 63, 248, 4 };
const int Iterations = 9872;
var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, Iterations);
return rfc2898DeriveBytes.GetBytes(32);
}
Decryption Method is :
public string DecryptString(string cipherText,string key)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Key =CreateKey(key);
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform rijndaelDecryptor = rijndaelCipher.CreateDecryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelDecryptor, CryptoStreamMode.Write);
string plainText = String.Empty;
try
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] plainBytes = memoryStream.ToArray();
plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
}
finally
{
memoryStream.Close();
cryptoStream.Close();
}
return plainText;
}
My key is : "ghb" (which I provide when decryption method is called)..
Methods call :
textBox1.Text = BCF.EncryptString(textBox1.Text,"ghb");
textBox1.Text = BCF.DecryptString(textBox1.Text,"ghb");
How to solved this method ?
Regards
Jayanta.