File decryption is missing ~ 10 characters from the end

I've written encryption / decryption methods using RC2CryptoServiceProvider

in C # and for some reason I can't get the decryptor to decrypt the last few bytes. The file seems to be just disconnected. My encryption method looks like this:

    public static byte[] EncryptString(byte[] input, string password)
    {
        PasswordDeriveBytes pderiver = new PasswordDeriveBytes(password, null);
        byte[] ivZeros = new byte[8];
        byte[] pbeKey = pderiver.CryptDeriveKey("RC2", "MD5", 128, ivZeros);

        RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider();

        byte[] IV = new byte[8];
        ICryptoTransform encryptor = RC2.CreateEncryptor(pbeKey, IV);

        MemoryStream msEncrypt = new MemoryStream();
        CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
        csEncrypt.Write(input, 0, input.Length);
        csEncrypt.FlushFinalBlock();

        return msEncrypt.ToArray();
    }

      

So far, my decryption looks like this:

    public static byte[] DecryptString(byte[] input, string password, int originalSize)
    {
        PasswordDeriveBytes pderiver = new PasswordDeriveBytes(password, null);
        byte[] ivZeros = new byte[8];
        byte[] pbeKey = pderiver.CryptDeriveKey("RC2", "MD5", 128, ivZeros);

        RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider();

        byte[] IV = new byte[8];
        ICryptoTransform decryptor = RC2.CreateDecryptor(pbeKey, IV);

        MemoryStream msDecrypt = new MemoryStream();
        CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);

        csDecrypt.Write(input, 0, originalSize);
       // csDecrypt.FlushFinalBlock();

        char[] decrypted = new char[input.Length];
        decrypted = System.Text.Encoding.UTF8.GetChars(msDecrypt.ToArray());
        return msDecrypt.ToArray();

    }

      

char [] decrypted

returns the entire file decrypted, except that the file ends with </LudoData>

, and when decrypted, I only get the first character <

.

I play with things and change nothing. In my particular case, it input

has a length of 11296 and originalSize

has a size of 11290. However, it decrypted

ends up with a size of 11280 when decrypted. What gives!

+2


source to share


2 answers


Is there any reason you disabled flash ()? Have you tried to close your streams completely?



+6


source


Sigh, I fought this battle about a month ago and had a very similar problem, except that in the end I was worried a lot. ToArray was my solution.

You are doing some weird stuff here, I'm not really sure. You are using crypto shots when you don't need it, you are tracking the original length for some strange reason and you are using deprecated classes. Your problem is probably a combination of indentation, wrong assumptions (confirmed by originalLength) and incorrect thread handling (which can be tricky). Try this instead:

Encryption:

var rij = RijndaelManaged.Create();
rij.Mode = CipherMode.CBC;
rij.BlockSize = 256;
rij.KeySize = 256;
rij.Padding = PaddingMode.ISO10126;
var pdb = new Rfc2898DeriveBytes(password, 
          Encoding.Default.GetBytes("lolwtfbbqsalt" + password));
var enc = rij.CreateEncryptor(pdb.GetBytes(rij.KeySize / 8), 
          pdb.GetBytes(rij.BlockSize / 8));
return enc.TransformFinalBlock(unencryptedBytes, 0, unencryptedBytes.Length);

      



Decrypt:

// throws a cryptographic exception if password is wrong
var rij = RijndaelManaged.Create();
rij.Mode = CipherMode.CBC;
rij.BlockSize = 256;
rij.KeySize = 256;
rij.Padding = PaddingMode.ISO10126;
var pdb = new Rfc2898DeriveBytes(password, 
          Encoding.Default.GetBytes("lolwtfbbqsalt" + password));
var dec = rij.CreateDecryptor(pdb.GetBytes(rij.KeySize / 8), 
          pdb.GetBytes(rij.BlockSize / 8));
return dec.TransformFinalBlock(encryptedBytes, 0, 
          encryptedBytes.Length);

      

Note that the only thing different from these two methods is CreateEncryptor / CreateDecryptor, so you can refactor a lot of duplication. Also note that I am entering the byte array and exiting the byte array without using any streams. It's also slightly safer than RC2, and even more so if the salt was more random.

0


source







All Articles