Je ne sais pas vraiment ce qui se passe ici. Mon application est correctement et le cryptage des fichiers sans problème, mais il lancer une IndexOutOfRangeException en essayant de déchiffrer le même fichier ...Index hors plage lors du décryptage d'un fichier
Voici mon code:
Public Sub EncryptDecrypt(ByVal Action As String, ByVal InFile As String, ByVal OutFile As String)
Try
Dim Buffer(4096) As Byte
Dim Stream As CryptoStream
Dim Rij As New System.Security.Cryptography.RijndaelManaged
Dim Key(), IV() As Byte
FSIn = New FileStream(InFile, FileMode.Open, FileAccess.Read)
FSOut = New FileStream(OutFile, FileMode.OpenOrCreate, FileAccess.Write)
FSOut.SetLength(0)
Key = CreateKey("p0Ju423KQY7h4D29Ml536jbX7gS2Q6Rtm87XvRttlKiZ")
IV = CreateIV("p0Ju423KQY7h4D29Ml536jbX7gS2Q6Rtm87XvRttlKiZ")
If Action = "E" Then
Stream = New CryptoStream(FSOut, Rij.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
Else
Stream = New CryptoStream(FSOut, Rij.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
End If
Stream.Close()
FSIn.Close()
FSOut.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
L'erreur apparaît sur la ligne Stream.Close()
.
J'ai appliqué le même code ailleurs et il n'a aucun problème ...
Voici mon trace de la pile:
System.IndexOutOfRangeException a été attrapé un message = "index était en dehors des limites du tableau. "
Source = "mscorlib" StackTrace: à System.Security.Cryptography.RijndaelManagedTransform.DecryptData (Byte [] InputBuffer, Int32 inputOffset, Int32 inputCount, Byte [] & OutputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean Flast) à System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock (byte [] INPUTBUFFER, Int32 inputOffset, Int32 inputCount) à System.Security.Cryptography.CryptoStream.FlushFinalBlock() à System.Security.Cryptography.CryptoStream.Dispose (Boolean Disposing) chez Sys tem.IO.Stream.Close() à Crypt.EncryptDecrypt (Action String, String InFile, String OutFile) en D: \ Développement \ Projects \ Web \ WebSite1 \ App_Code \ Crypt.vb: ligne 34 InnerException:
Toute aide sera grandement appréciée.
EDIT 1 Après le commentaire de AAZ, j'ai révisé et remplacé
Stream = New CryptoStream(FSOut, Rij.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
avec
Stream = New CryptoStream(FSIn, Rij.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
Voici le résultat Stack Trace:
System.IndexOutOfRangeException a été pris Message = "Index w en dehors des limites du tableau. " Source = "mscorlib" StackTrace: à System.Security.Cryptography.RijndaelManagedTransform.DecryptData (byte []> INPUTBUFFER, Int32 inputOffset, Int32 inputCount, byte [] & OutputBuffer, Int32> outputOffset, PaddingMode paddingMode, Boolean Flast) à System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock (Byte []> inputBuffer, Int32 inputOffset, Int32 inputCount) à System.Security.Cryptography.CryptoStream.FlushFinalBlock() à System.Security.Cryptography.CryptoStream.Dispose (Boolean disposant) à System.IO.Stream.Close() à Crypt.EncryptDecrypt (Action de chaîne, String InFile, String OutFile) dans> D: \ Development \ Projects \ Web \ WebSite1 \ App_Code \ Crypt.vb: ligne 34 InnerException:
me semble que son la même erreur ...
FIN EDIT 1
Le code est-il complet? Il ne copie pas réellement les données de 'FSIn' à' Stream '. – aaz
C'est ce qui m'embrouille ... Cela fonctionne parfaitement dans une autre application où je l'ai utilisé. Je suis assez nouveau pour le cryptage, donc je ne fais que bricoler ici et là. – Ortund