Greetings to all! The problem is minor, but it has a place to be. Encryption works with a bang, the decryption is almost, namely, it decrypts, but gives the error "filling is incorrect and cannot be deleted" on the last block.
The structure is as follows:
Filling is incorrect and cannot be deleted. description of the error: in System.Security.Cryptography.RijndaelManagedTransform.DecryptData (Byte [a] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte [] & outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast) in a system.Security. (Byte [] inputBuffer, Int32 inputOffset, Int32 inputCount) in System.Security.Cryptography.CryptoStream.FlushFinalBlock () in System.Security.Cryptography.CryptoStream.Dispose (Boolean disposing) in System.IO.Stream.Close () in System .IO.Stream.Dispose () in MyEncryptor.Crypto.Decrypt_AES_File (String input, String output, String passPhrase, String saltValue, String hashString, Int32 Iterations, String initVector, Int32 keySize) in e: \ IT \ GITreps \ MyExamples \ MVC un, Int32 keySize) in e: \ IT \ GITreps \ MyExamples \ MVC unvCector, Int32 keySize \ m \ IT \ GITreps \ MyExamples \ MVC, ector Int32.Vector, String output, String. \ MyEncryptor \ Crypto.cs: line 207 in MyEncryptor.MainPresenter
1.DecFile_AES_RSA(Int32 fileIndex) в e:\IT\GITreps\MyExamples\MVCexample\MyEncryptor\MainPresenter.cs:строка 331 в MyEncryptor.MainPresenter. _view_FileOpenClick (Object sender, EventArgs e) in e: \ IT \ GITreps \ MyExamples \ MVCexample \ MyEncryptor \ MainPresenter.cs: line 177
Encryption:
public bool Encrypt_AES_File(string input, string output, string passPhrase, string saltValue, string hashString, int Iterations, string initVect, int keySize) { byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVect); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashString, Iterations); byte[] keyBytes = password.GetBytes(keySize / 8); RijndaelManaged aes = new RijndaelManaged(); aes.Mode = CipherMode.CBC; byte[] progs = File.ReadAllBytes(input); CurrentMaxProg = progs.Length; byte[] buf = new byte[128]; int BytesRead = 0; using (FileStream fsCrypt = new FileStream(output, FileMode.Create)) using (ICryptoTransform encryptor = aes.CreateEncryptor(keyBytes, initVectorBytes)) using (CryptoStream cryptoStream = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write)) using (FileStream fsIn = new FileStream(input, FileMode.Open)) { try { do { if (_cancelled) { CurrentProg = 0; CurrentMaxProg = 0; return false; } BytesRead = fsIn.Read(buf, 0, buf.Length); CurrentProg += BytesRead; if (BytesRead == 0) break; cryptoStream.Write(buf, 0, buf.Length); CurrentProgressChanged(CurrentProg); Application.DoEvents(); } while (BytesRead != 0); cryptoStream.FlushFinalBlock(); CurrentProg = 0; CurrentMaxProg = 0; return true; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } } } Decryption:
public bool Decrypt_AES_File(string input, string output, string passPhrase, string saltValue, string hashString, int Iterations, string initVector, int keySize) { byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashString, Iterations); byte[] keyBytes = password.GetBytes(keySize / 8); RijndaelManaged aes = new RijndaelManaged(); aes.Mode = CipherMode.CBC; byte[] progs = File.ReadAllBytes(input); CurrentMaxProg = progs.Length; byte[] buf = new byte[128]; int BytesRead = 0; using (FileStream fsDecrypt = new FileStream(input, FileMode.Open,FileAccess.Read)) using (FileStream fsOut = new FileStream(output, FileMode.Create, FileAccess.Write)) using (ICryptoTransform decryptor = aes.CreateDecryptor(keyBytes, initVectorBytes)) using (CryptoStream cryptoStream = new CryptoStream(fsOut, decryptor, CryptoStreamMode.Write)) { try { do { if (_cancelled) { CurrentProg = 0; CurrentMaxProg = 0; return false; } BytesRead = fsDecrypt.Read(buf, 0, buf.Length); CurrentProg += BytesRead; if (BytesRead == 0) break; cryptoStream.Write(buf, 0, buf.Length); CurrentProgressChanged(CurrentProg); Application.DoEvents(); } while (BytesRead != 0); cryptoStream.FlushFinalBlock(); CurrentProg = 0; CurrentMaxProg = 0; return true; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } } } The call is correct. An error occurs on the line
cryptoStream.FlushFinalBlock(); Why do I think that stands for correctly? Tested on .mp3. Encrypts correctly, because if you change the file extension manually or stupidly with the .crypt extension into the player after encryption, it will not play. If, after decryption, to throw the resulting file into the player, play without spaces, from beginning to end. Who knows how to fix?