Question

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);
        }
    }
Was it helpful?

Solution

There are a few problems with your code. First, you are creating a Rijndael (AES) object. However, you're using the DESCryptoServiceProvider which 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 the RijndaelManaged algorithm which is the managed implementation of the AES algorithm 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.

static byte[] u8_Salt = new byte[] { 0x26, 0x19, 0x81, 0x4E, 0xA0, 0x6D, 0x95, 0x34, 0x26, 0x75, 0x64, 0x05, 0xF6 };

public static string EncryptString(string plainText, string password)
{
    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, u8_Salt);
    using (RijndaelManaged i_Alg = new RijndaelManaged { Key = pdb.GetBytes(32), IV = pdb.GetBytes(16) })
    {
        using (var memoryStream = new MemoryStream())
        using (var cryptoStream = new CryptoStream(memoryStream, i_Alg.CreateEncryptor(), CryptoStreamMode.Write))
        {
            byte[] data = Encoding.UTF8.GetBytes(plainText);
            cryptoStream.Write(data, 0, data.Length);
            cryptoStream.FlushFinalBlock();

            return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
        }
    }
}

public static string Decrypt(string cipherText, string password)
{
    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, u8_Salt);

    using (RijndaelManaged i_Alg = new RijndaelManaged { Padding = PaddingMode.Zeros, Key = pdb.GetBytes(32), IV = pdb.GetBytes(16) })
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var cryptoStream = new CryptoStream(memoryStream, i_Alg.CreateDecryptor(), CryptoStreamMode.Write))
            {
                byte[] data = Convert.FromBase64String(cipherText);
                cryptoStream.Write(data, 0, data.Length);
                cryptoStream.Flush();

                return Encoding.UTF8.GetString(memoryStream.ToArray());
            }
        }
    }
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top