0

I tried to decrypt bytes, but i have error "length of data to decrypt is invalid" in line read = cryptoStream.Read(buffer, 0, len); inputFile.lenght = 4256, len = 4256

This is the code of decrypting

        byte[] salt = new byte[32];

        FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

        fsCrypt.Seek(fsCrypt.Length - 376, SeekOrigin.Begin);
        fsCrypt.Read(salt, 0, salt.Length);
        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        RijndaelManaged AES = new RijndaelManaged();
        AES.KeySize = 256;
        AES.BlockSize = 128;
        var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
        AES.Key = key.GetBytes(AES.KeySize / 8);
        AES.IV = key.GetBytes(AES.BlockSize / 8);
        AES.Padding = PaddingMode.PKCS7;
        AES.Mode = CipherMode.CFB;
        int read;
        byte[] buffer = new byte[1048576];
        List < byte > bytes = new List < byte > ();

        fsCrypt.Seek(0, SeekOrigin.Begin);
        read = 1;
        var len = Convert.ToInt32(fsCrypt.Length);

        MemoryStream mem = new MemoryStream();
        fsCrypt.CopyTo(mem);
        mem.Position = 0;
        using(CryptoStream cryptoStream = new CryptoStream(mem, AES.CreateDecryptor(), CryptoStreamMode.Read)) // CryptoStream cryptoStream = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read
         {
          //var buffer2 = new byte[len];
          while ((read = cs.Read(buffer, 0, buffer.Length)) > 0) {
           foreach(var m in buffer)
           bytes.Add(m);
          }
         }

Can u help me? what is problem in this code ?

I took the example of the AES encryption on large files

How i encrypt:

byte[] salt = GenerateRandomSalt();


 //convert password string to byte arrray
 byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);

 //Set Rijndael symmetric encryption algorithm 71 246
 RijndaelManaged AES = new RijndaelManaged();
 AES.KeySize = 256;
 AES.BlockSize = 128;
 AES.Padding = PaddingMode.PKCS7;


 var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
 AES.Key = key.GetBytes(AES.KeySize / 8);
 AES.IV = key.GetBytes(AES.BlockSize / 8);

 AES.Mode = CipherMode.CFB;
 var kk = key.ToString();


 FileStream fsIn = new FileStream(inputFile, FileMode.Open);


 byte[] buffer = new byte[1048576];
 int read = 0;



 byte[] encrypted;
 using(MemoryStream msEncrypt = new MemoryStream()) {
  using(CryptoStream csEncrypt = new CryptoStream(msEncrypt, AES.CreateEncryptor(), CryptoStreamMode.Write)) {
   fsIn.Seek(0, SeekOrigin.Begin);

   while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0) {
    csEncrypt.Write(buffer, 0, read);
   }
   encrypted = msEncrypt.ToArray();
  }
 }

 fsIn.Seek(0, SeekOrigin.Begin);
 fsIn.Write(encrypted, 0, encrypted.Length);

 fsIn.Seek(fsIn.Length, SeekOrigin.Begin);
 fsIn.Write(salt, 0, salt.Length);
Lolidze
  • 219
  • 2
  • 13

1 Answers1

1

You call encrypted = msEncrypt.ToArray(); too early on your encryption, it must be after the the end of the using statement for csEncrypt so all data gets flushed out to the memory stream.

Also in your decryption you are starting way to early in the file

fsCrypt.Seek(fsCrypt.Length - 376, SeekOrigin.Begin); should likey be fsCrypt.Seek(fsCrypt.Length - 32, SeekOrigin.Begin); if your salt.Length was 32 when you wrote it out.

Lastly you also don't trim the salt off before you pass it to mem so you are passing the salt data in to the stream along with the cyphertext and it makes your message too long.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • i moved line `encrypted = msEncrypt.ToArray();` after `using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, AES.CreateEncryptor(), CryptoStreamMode.Write))` [link](https://pastebin.com/y9ge5W5P) but when decrypt again i get error – Lolidze Jun 30 '17 at 20:06
  • thank you very much, and do not tell me how to cut salt from stream? – Lolidze Jun 30 '17 at 20:17
  • ty, i wrote this `fsCrypt.SetLength(fsCrypt.Length-32);` and decode without errors and right, but i have so much space after text, in notepad++ show "NULL" [link](http://joxi.ru/12MQg4RiMxoEg2) – Lolidze Jun 30 '17 at 20:25
  • Oh, I think I understand – Lolidze Jun 30 '17 at 20:28
  • yeah, "NULL" i fix, but now added to the end "OЩ-ЖмЕЦФ»r’Іціъ" – Lolidze Jun 30 '17 at 20:30