Pothi Sam

Pothi Sam

  • NA
  • 335
  • 50.7k

How to Give Error Message if Decrypt value change manually

Feb 19 2015 1:44 AM
i have a password Encryptor and  Decrypt if any one person change Encrptor value mannualy example Actual value is(W18cJ8//VPFBwPsSo4kxjQ==) Modified Value Is(W18cJ8//VPFBwPsSo41234==) now Decryptor show error msg .so i need to show error msg in lable.Codes Are Below
--------------------------------------------------------------------------------------------------------------------
private string Decrypt(string cipherText)
{
string EncryptionKey = "Pothisam";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
 -------------------------------------------------------------------------------------------
private string Encrypt(string clearText)
{
string EncryptionKey = "Pothisam";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
 -------------------------------------------------------------------------
now showing error msg like this
 

Exception Details: System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed.

Source Error:

Line 47:                     cs.Write(cipherBytes, 0, cipherBytes.Length);
 Line 48:                     cs.Close(); Line 49:               
                                } 
Line 50:                 cipherText = Encoding.Unicode.GetString(ms.ToArray()); 
Line 51:             }
 Please Any One Help Me
 

Answers (1)