I’m trying to get this code to encrypt and decrypt but I keep getting data length too long when decrypting during the .FlushFinalBlock().
I’m not sure what I’m doing wrong here because I’m out of my element on this issue.
Can anyone tell me what I’m doing wrong here? Encryption is OK, but it cannot decrypt its encryption.
static byte[] u8_Salt = new byte[] { 0x26, 0x19, 0x81, 0x4E, 0xA0, 0x6D, 0x95, 0x34, 0x26, 0x75, 0x64, 0x05, 0xF6 };
public static string Encrypt(string data, string password)
{
//
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, u8_Salt);
//
Rijndael i_Alg = Rijndael.Create();
i_Alg.Padding = PaddingMode.None;
i_Alg.Key = pdb.GetBytes(32);
i_Alg.IV = pdb.GetBytes(16);
//
using (var cryptoProvider = new DESCryptoServiceProvider())
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(), CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(data);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
}
public static string Decrypt(string data, string password)
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, u8_Salt);
//
Rijndael i_Alg = Rijndael.Create();
i_Alg.Padding = PaddingMode.None;
i_Alg.Key = pdb.GetBytes(32);
i_Alg.IV = pdb.GetBytes(16);
//
using (var cryptoProvider = new DESCryptoServiceProvider())
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(), CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(data);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
}
There are a few problems with your code. First, you are creating a Rijndael (AES) object. However, you’re using the
DESCryptoServiceProviderwhich means that you are actually using DES, not Rijndael. Because of that you also have the problem that you are not setting the key for the algorithm. Below are your two functions re-written using theRijndaelManagedalgorithm which is the managed implementation of theAESalgorithm on .NET. I’ve tested and you can encrypt and decrypt with no problem. Also: don’t use that salt value, it’s all over the internet.To generate a random salt I would use the RNGCryptoServiceProvider this will generate a cryptographically secure random byte array that you can use. You’ll need to generate it in a separate process and use it as your salt.