I have the following code, and it was working for almost two years. But now we have started to see random issue with the Padding. When I say random, I mean same thing works one day but doesn't work the other day. And someday it decides to work randomly.
Now, if I add the padding to none like mentioned in answers above, I could mess up all the previously encrypted files. I'm thinking to create different approach using GOTO statement in catch block in this method same way as I did when I changed the encryption key. Or is there any better approach to change padding to None?
/// <summary>
///
/// </summary>
[Serializable]
public static class EncryptDecrypt
{
private static string EncryptionKey_old = "MAKV2SPBNI99212";
private static string EncryptionKey = "Yi9BpGG1cXR01gBwGPZRTOznoJHpkGBOzisBg5jl3iRu48yhcFGdZu76fDpa5FUu";
/// <summary>
///
/// </summary>
/// <param name="clearText"></param>
/// <returns></returns>
public static string Encrypt(string clearText)
{
byte[] whitebs = 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);
encryptor.Mode = CipherMode.ECB;
encryptor.Padding = PaddingMode.PKCS7;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(whitebs, 0, whitebs.Length);
cs.FlushFinalBlock();
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText.EndsWith("==") ? clearText.Remove(clearText.Length - 2) : clearText;
}
/// <summary>
///
/// </summary>
/// <param name="cipherText"></param>
/// <returns></returns>
public static string Decrypt(string cipherText)
{
int attempts = 0;
string exception = string.Empty;
StartHere:
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes;
try { cipherBytes = Convert.FromBase64String(cipherText); }
catch { 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);
encryptor.Mode = CipherMode.ECB;
encryptor.Padding = PaddingMode.PKCS7;
try
{
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.FlushFinalBlock();
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
catch
{
if (attempts == 2) throw;
EncryptionKey = EncryptionKey_old;
attempts++;
goto StartHere;
}
}
return cipherText;
}
'
Changing this now is not a good idea also I don't know how I would go about doing that because there are thousands of files we encrypted with this code.