Optimize Filestream for decrypting files

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 19 Antworten in diesem Thema. Der letzte Beitrag () ist von zackmark29.

    Optimize Filestream for decrypting files

    Hi could suggest someone or help me optimize the process of filestream?
    When I try to decrypt 600MB files it takes about 10mins to finish.
    Here's my

    VB.NET-Quellcode

    1. Private Sub BGW_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW.DoWork
    2. Dim newWorer As BackgroundWorker = DirectCast(sender, BackgroundWorker)
    3. Dim counter As Double = 1
    4. Dim outputPath As String = txtOuput.Text
    5. Dim files = Directory.GetFiles(txtInput.Text, "*.ts")
    6. Dim key As Byte() = File.ReadAllBytes(txtKey.Text)
    7. Dim IV(15) As Byte
    8. Using outputStream As FileStream = New FileStream(outputPath, FileMode.Create, FileAccess.Write)
    9. For i As Integer = 0 To files.Length - 1
    10. Using inputStream As FileStream = New FileStream(files(i), FileMode.Open, FileAccess.Read, FileShare.Read, FileOptions.SequentialScan)
    11. Using AES As New AesManaged
    12. AES.KeySize = 256
    13. AES.BlockSize = 128
    14. AES.Key = key
    15. AES.IV = IV
    16. AES.Padding = PaddingMode.PKCS7
    17. AES.Mode = CipherMode.CBC
    18. Using AESDecrypter As ICryptoTransform = AES.CreateDecryptor(AES.Key, AES.IV)
    19. Using cryptoStream As CryptoStream = New CryptoStream(inputStream, AESDecrypter, CryptoStreamMode.Read, FileOptions.SequentialScan)
    20. Dim value As Double = (counter / files.Length) * 100
    21. newWorer.ReportProgress(value)
    22. Dim PlaintextBytes(inputStream.Length - 1) As Byte
    23. cryptoStream.CopyTo(outputStream, PlaintextBytes.Length)
    24. counter += 1
    25. cryptoStream.Dispose()
    26. cryptoStream.Close()
    27. inputStream.Close()
    28. inputStream.Dispose()
    29. End Using
    30. End Using
    31. End Using
    32. End Using
    33. Next
    34. outputStream.Close()
    35. outputStream.Dispose()
    36. End Using
    37. End Sub


    CodeTags adjusted ~VaporiZed

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „VaporiZed“ ()

    @zackmark29 I beleve that only one AesManaged instance is sufficient, use this using part as first block.
    Use Option Strict On: Visual Studio - Empfohlene Einstellungen
    Use only one AesManaged instance, put this using to the 1st position.
    Use value as Integer variable and use Integer Division with \.
    =====
    Are these TS - Transport Stream movie files?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Here's my update with your suggestion as well.
    But don't know yet about using option strict on

    VB.NET-Quellcode

    1. Public Shared Function Decrypt(ByVal inputFS As FileStream, ByVal key As Byte(), ByVal outputFS As FileStream)
    2. Dim IV(15) As Byte
    3. Dim Algo As AesManaged = New AesManaged
    4. With Algo
    5. Algo.BlockSize = 128
    6. Algo.KeySize = 128
    7. Algo.Key = key
    8. Algo.IV = IV
    9. Algo.Mode = CipherMode.CBC
    10. Algo.Padding = PaddingMode.PKCS7
    11. End With
    12. Using AESDecrypter As ICryptoTransform = Algo.CreateDecryptor
    13. Using cryptoS As New CryptoStream(inputFS, AESDecrypter, CryptoStreamMode.Read)
    14. While inputFS.Position < inputFS.Length
    15. cryptoS.CopyTo(outputFS)
    16. End While
    17. Return cryptoS
    18. End Using
    19. End Using
    20. End Function


    VB.NET-Quellcode

    1. Private Sub BGW_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW.DoWork
    2. Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
    3. Dim outputFilePath = Path.Combine(txtOutputFolder.Text, txtOutputFileName.Text & ".ts")
    4. Dim inputFilePath = Directory.GetFiles(txtInputFolder.Text, "*.ts")
    5. Dim key As Byte() = File.ReadAllBytes(txtKeyPath.Text)
    6. Dim i As Integer
    7. Try
    8. Using outputFS = New FileStream(outputFilePath, FileMode.Create, FileAccess.Write)
    9. For i = 0 To inputFilePath.Length - 1
    10. Using inputFS = New FileStream(inputFilePath(i), FileMode.Open, FileAccess.Read)
    11. AESDecrypt.Decrypt(inputFS, key, outputFS)
    12. If BGW.CancellationPending = True Then
    13. e.Cancel = True
    14. Exit For
    15. End If
    16. worker.ReportProgress(i / inputFilePath.Length * 100)
    17. End Using
    18. Next
    19. End Using
    20. Catch ex As Exception
    21. MsgBox(ex.Message, vbCritical, "Error")
    22. End Try
    23. End Sub


    CodeTags adjusted ~VaporiZed

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „VaporiZed“ ()

    zackmark29 schrieb:

    option strict on
    is a Visual Basic feature that forces you to use the correct variable types.
    Try this code using Strict Off and Strict On:

    VB.NET-Quellcode

    1. Dim anyArray(20) ' 21 elements
    2. For i As Double = 0 To 10 Step 0.5
    3. anyArray(2 * i) = i ' Error on Strict On: Index must be an integer value
    4. Next
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    @zackmark29 what kind of hard drive do you have?
    if i convert ts files with ffmpeg, it takes different times depending on the storage medium.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    I made an test earlier today in which i created 600MB of files with random data, each between 1-2 MB. Encrypted them into a new file, and decrypted them into a new file and created and compared the SHA1 hash of the plain and decrypted file.
    On my NVME SSD, this took ~20 sec. on an SATA SSD ~30 Sec, on an HDD ~50 sec.
    I'm going to post that test tomorrow since I don't have access to it right now.
    Maybe that helps.
    I also have something a code (without BackgroundWorker) if someone is interested, and will be post tomorrow too

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Strict On
    2. Option Explicit On
    3. Imports System.IO
    4. Imports System.Text
    5. Imports System.Security.Cryptography
    6. Public Module AesLargeFileToFileTest
    7. Private ReadOnly Rand As Random = New Random
    8. Public Sub Main()
    9. Dim src = "data"
    10. Dim dst = "data.aes"
    11. Dim pw = Encoding.UTF8.GetBytes("1234567890")
    12. AllInOneFile(src, dst, pw)
    13. Console.WriteLine("Wait and Press Enter ... ")
    14. Console.ReadKey()
    15. Dim keyfile = "keydata"
    16. WithKeyFile(src, dst, keyfile, pw)
    17. Console.WriteLine()
    18. Console.WriteLine("Wait ... ")
    19. Console.ReadKey()
    20. End Sub
    21. Private Async Sub AllInOneFile(srcfile As String, dstfile As String, password() As Byte)
    22. Dim sw = New Stopwatch
    23. sw.Restart()
    24. 'Encryption
    25. Await Task.Run(Sub() AESEncryption(srcfile, dstfile, password))
    26. Console.WriteLine("Encryption {0} ms", sw.ElapsedMilliseconds)
    27. Dim fi = New FileInfo(srcfile)
    28. fi.Delete()
    29. sw.Restart()
    30. 'Decryption
    31. Await Task.Run(Sub() AesDecryption(dstfile, srcfile, password))
    32. Console.WriteLine("Decryption {0} ms", sw.ElapsedMilliseconds)
    33. fi = New FileInfo(dstfile)
    34. fi.Delete()
    35. End Sub
    36. Private Async Sub WithKeyFile(srcfile As String, dstfile As String, keyfile As String, password() As Byte)
    37. Dim sw = New Stopwatch
    38. sw.Restart()
    39. 'Encryption
    40. Await Task.Run(Sub() AESEncryption(srcfile, dstfile, keyfile, password))
    41. Console.WriteLine("Encryption {0} ms", sw.ElapsedMilliseconds)
    42. Dim fi = New FileInfo(srcfile)
    43. fi.Delete()
    44. sw.Restart()
    45. 'Decryption
    46. Await Task.Run(Sub() AesDecryption(dstfile, srcfile, keyfile, password))
    47. Console.WriteLine("Decryption {0} ms", sw.ElapsedMilliseconds)
    48. fi = New FileInfo(dstfile)
    49. fi.Delete()
    50. End Sub
    51. Private Sub AESEncryption(srcfile As String, dstfile As String, password() As Byte)
    52. If password.Length < 10 Then
    53. Throw New ArgumentException(NameOf(password))
    54. End If
    55. Dim iv() As Byte
    56. Dim fi = New FileInfo(srcfile)
    57. If Not fi.Exists Then
    58. Throw New ArgumentException(NameOf(srcfile))
    59. End If
    60. Using fsopt = New FileStream(dstfile, FileMode.Create, FileAccess.Write)
    61. Using fsipt = New FileStream(srcfile, FileMode.Open, FileAccess.Read)
    62. 'Saltlänge im Minimum Passwortlänge
    63. Dim salt = CreateSalt(3 * password.Length)
    64. Using aes = New AesManaged
    65. aes.KeySize = 256
    66. aes.BlockSize = 128
    67. aes.Mode = CipherMode.CBC
    68. aes.Padding = PaddingMode.PKCS7
    69. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    70. 'IV und Key-Zuweisungen immer nach
    71. 'KeySize/BlockSize/Mode/Padding
    72. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    73. aes.GenerateIV()
    74. iv = aes.IV
    75. aes.Key = NewKey(password, salt, 1000, aes.KeySize)
    76. Using hsha512 = New HMACSHA512(password)
    77. fsipt.Position = 0
    78. 'Make a Authentication
    79. Dim hashsha512 = hsha512.ComputeHash(fsipt)
    80. fsipt.Flush()
    81. Dim authhash = New Byte(hashsha512.Length + salt.Length + iv.Length - 1) {}
    82. Array.Copy(hashsha512, authhash, hashsha512.Length)
    83. Array.Copy(salt, 0, authhash, hashsha512.Length, salt.Length)
    84. Array.Copy(iv, 0, authhash, hashsha512.Length + salt.Length, iv.Length)
    85. hashsha512 = hsha512.ComputeHash(authhash)
    86. fsopt.Write(hashsha512, 0, hashsha512.Length)
    87. fsopt.Write(salt, 0, salt.Length)
    88. fsopt.Write(iv, 0, iv.Length)
    89. fsopt.Flush()
    90. End Using
    91. Dim ec = aes.CreateEncryptor(aes.Key, aes.IV)
    92. Using cs = New CryptoStream(fsopt, ec, CryptoStreamMode.Write)
    93. fsipt.Seek(0, SeekOrigin.Begin)
    94. fsipt.CopyTo(cs)
    95. End Using
    96. End Using
    97. End Using
    98. End Using
    99. End Sub
    100. Private Sub AESEncryption(srcfile As String, dstfile As String, keyfile As String, password() As Byte)
    101. If password.Length < 10 Then
    102. Throw New ArgumentException(NameOf(password))
    103. End If
    104. Dim iv() As Byte
    105. Dim fi = New FileInfo(srcfile)
    106. If Not fi.Exists Then
    107. Throw New ArgumentException(NameOf(srcfile))
    108. End If
    109. Using fsopt = New FileStream(dstfile, FileMode.Create, FileAccess.Write)
    110. Using fsipt = New FileStream(srcfile, FileMode.Open, FileAccess.Read)
    111. 'Saltlänge im Minimum Passwortlänge
    112. Dim salt = CreateSalt(3 * password.Length)
    113. Using aes = New AesManaged
    114. aes.KeySize = 256
    115. aes.BlockSize = 128
    116. aes.Mode = CipherMode.CBC
    117. aes.Padding = PaddingMode.PKCS7
    118. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    119. 'IV und Key-Zuweisungen immer nach
    120. 'KeySize/BlockSize/Mode/Padding
    121. '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    122. aes.GenerateIV()
    123. iv = aes.IV
    124. aes.Key = NewKey(password, salt, 1000, aes.KeySize)
    125. Using hsha512 = New HMACSHA512(password)
    126. fsipt.Position = 0
    127. 'Make a Authentication
    128. Dim hashsha512 = hsha512.ComputeHash(fsipt)
    129. fsipt.Flush()
    130. Dim authhash = New Byte(hashsha512.Length + salt.Length + iv.Length - 1) {}
    131. Array.Copy(hashsha512, authhash, hashsha512.Length)
    132. Array.Copy(salt, 0, authhash, hashsha512.Length, salt.Length)
    133. Array.Copy(iv, 0, authhash, hashsha512.Length + salt.Length, iv.Length)
    134. hashsha512 = hsha512.ComputeHash(authhash)
    135. Dim hashinfile = New Byte(hashsha512.Length + salt.Length + iv.Length - 1) {}
    136. Array.Copy(hashsha512, hashinfile, hashsha512.Length)
    137. Array.Copy(salt, 0, hashinfile, hashsha512.Length, salt.Length)
    138. Array.Copy(iv, 0, hashinfile, hashsha512.Length + salt.Length, iv.Length)
    139. File.WriteAllBytes(keyfile, hashinfile)
    140. End Using
    141. Dim ec = aes.CreateEncryptor(aes.Key, aes.IV)
    142. Using cs = New CryptoStream(fsopt, ec, CryptoStreamMode.Write)
    143. fsipt.Seek(0, SeekOrigin.Begin)
    144. fsipt.CopyTo(cs)
    145. End Using
    146. End Using
    147. End Using
    148. End Using
    149. End Sub
    150. Private Sub AesDecryption(srcfile As String, dstfile As String, password() As Byte)
    151. Dim fi = New FileInfo(srcfile)
    152. If Not fi.Exists Then
    153. Throw New ArgumentException(NameOf(srcfile))
    154. End If
    155. Dim iv, salt As Byte()
    156. Dim hash = New Byte(64 - 1) {}
    157. Using fsopt = New FileStream(dstfile, FileMode.Create, FileAccess.Write)
    158. Using fsipt = New FileStream(srcfile, FileMode.Open, FileAccess.Read)
    159. Using aes = New AesManaged
    160. aes.KeySize = 256
    161. aes.BlockSize = 128
    162. aes.Mode = CipherMode.CBC
    163. aes.Padding = PaddingMode.PKCS7
    164. Dim streamposition = 0L
    165. iv = New Byte(aes.BlockSize \ 8 - 1) {}
    166. salt = New Byte(3 * password.Length - 1) {}
    167. fsipt.Read(hash, 0, hash.Length)
    168. fsipt.Read(salt, 0, salt.Length)
    169. fsipt.Read(iv, 0, iv.Length)
    170. streamposition = fsipt.Position
    171. fsipt.Flush()
    172. aes.IV = iv
    173. aes.Key = NewKey(password, salt, 1000, aes.KeySize)
    174. Try
    175. Dim dc = aes.CreateDecryptor(aes.Key, aes.IV)
    176. Using cs = New CryptoStream(fsopt, dc, CryptoStreamMode.Write)
    177. fsipt.Position = streamposition
    178. fsipt.CopyTo(cs)
    179. End Using
    180. Catch ex As Exception
    181. End Try
    182. End Using
    183. End Using
    184. End Using
    185. If Validation(dstfile, password, salt, iv, hash) Then
    186. Return
    187. End If
    188. fi = New FileInfo(dstfile)
    189. fi.Delete()
    190. Throw New Exception("Authentication is false")
    191. End Sub
    192. Private Sub AesDecryption(srcfile As String, dstfile As String, keyfile As String, password() As Byte)
    193. Dim fi = New FileInfo(srcfile)
    194. If Not New FileInfo(srcfile).Exists OrElse Not New FileInfo(keyfile).Exists Then
    195. Throw New ArgumentException($"{NameOf(srcfile)} / {NameOf(keyfile)} ?")
    196. End If
    197. Dim iv, salt As Byte()
    198. Dim hash = New Byte(64 - 1) {}
    199. Using fsopt = New FileStream(dstfile, FileMode.Create, FileAccess.Write)
    200. Using fsipt = New FileStream(srcfile, FileMode.Open, FileAccess.Read)
    201. Using aes = New AesManaged
    202. aes.KeySize = 256
    203. aes.BlockSize = 128
    204. aes.Mode = CipherMode.CBC
    205. aes.Padding = PaddingMode.PKCS7
    206. iv = New Byte(aes.BlockSize \ 8 - 1) {}
    207. salt = New Byte(3 * password.Length - 1) {}
    208. Dim bytes = File.ReadAllBytes(keyfile)
    209. Array.Copy(bytes, hash, hash.Length)
    210. Array.Copy(bytes, hash.Length, salt, 0, salt.Length)
    211. Array.Copy(bytes, hash.Length + salt.Length, iv, 0, iv.Length)
    212. aes.IV = iv
    213. aes.Key = NewKey(password, salt, 1000, aes.KeySize)
    214. Try
    215. Dim dc = aes.CreateDecryptor(aes.Key, aes.IV)
    216. Using cs = New CryptoStream(fsopt, dc, CryptoStreamMode.Write)
    217. fsipt.CopyTo(cs)
    218. End Using
    219. Catch ex As Exception
    220. End Try
    221. End Using
    222. End Using
    223. End Using
    224. If Validation(dstfile, password, salt, iv, hash) Then
    225. Return
    226. End If
    227. fi = New FileInfo(dstfile)
    228. fi.Delete()
    229. Throw New Exception("Authentication is false")
    230. End Sub
    231. Private Function Validation(src As String, pw() As Byte, salt() As Byte, iv() As Byte, hash() As Byte) As Boolean
    232. Dim hashvalue() As Byte
    233. Using fsipt = New FileStream(src, FileMode.Open, FileAccess.Read)
    234. Using hsha512 = New HMACSHA512(pw)
    235. hashvalue = hsha512.ComputeHash(fsipt)
    236. Dim authhash = New Byte(hashvalue.Length + salt.Length + iv.Length - 1) {}
    237. Array.Copy(hashvalue, authhash, hashvalue.Length)
    238. Array.Copy(salt, 0, authhash, hashvalue.Length, salt.Length)
    239. Array.Copy(iv, 0, authhash, hashvalue.Length + salt.Length, iv.Length)
    240. hashvalue = hsha512.ComputeHash(authhash)
    241. End Using
    242. End Using
    243. Return hashvalue.SequenceEqual(hash)
    244. End Function
    245. Private Function NewKey(pw() As Byte, salt() As Byte, it As Int32, ksize As Int32) As Byte()
    246. 'Alternative: HMACSHA256 with password and salt as one array
    247. Using r = New Rfc2898DeriveBytes(pw, salt, 1000)
    248. Return r.GetBytes(ksize \ 8)
    249. End Using
    250. End Function
    251. Private Function CreateSalt(size As Int32) As Byte()
    252. 'Alternative: RNGCryptoServiceProvider
    253. Dim result = New Byte(size - 1) {}
    254. Rand.NextBytes(result)
    255. Return result
    256. End Function
    257. End Module


    greeting

    exc-jdbi

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „exc-jdbi“ ()

    Just because I got curious, I added a Big file test, where a single 600MB file is being created, encrypted, decrypted, and compared, which takes ~8 seconds on an NVME SSD and ~24 Seconds on an HDD. Everything except for the last comparison only takes up to 40MB of RAM.

    The Code presented is taken from a console project. You should be able to paste this into a program.cs or program.vb file and run it. It was written in C#8 and then put through a converter for VB.NET and fixed where needed. There still may be some weird things like the use of System.Threading.Interlocked.Increment for the C# ++xyz which may have a better alternative.

    C# Original

    C#-Quellcode

    1. using System;
    2. using System.Diagnostics;
    3. using System.IO;
    4. using System.Linq;
    5. using System.Security.Cryptography;
    6. namespace MnyFileCryptTest
    7. {
    8. class Program
    9. {
    10. private static long writeFileTime = 0;
    11. private static long encryptFileTime = 0;
    12. private static long decryptFileTime = 0;
    13. private static long compareFileTime = 0;
    14. private static long createBigTime = 0;
    15. private static long encryptBigTime = 0;
    16. private static long decryptBigTime = 0;
    17. private static long compareBigTime = 0;
    18. private static int endFileSize = 600 * 1024 * 1024;
    19. private static int currentFileSize = 0;
    20. private static int filesCreated = 1;
    21. private static Stopwatch timeWatch;
    22. private static Random randomFile = new Random();
    23. private static Random randomKey = new Random(42);
    24. private static DirectoryInfo randomDirectory;
    25. private static AesManaged aesEncryption;
    26. private static SHA1 shaHash = SHA1.Create();
    27. private static void CreateManyFiles()
    28. {
    29. Console.WriteLine("Write Files....");
    30. while (endFileSize > currentFileSize)
    31. {
    32. int buffersize = (int)((1 + randomFile.NextDouble()) * 1024 * 1024);
    33. if (currentFileSize + buffersize > endFileSize)
    34. {
    35. buffersize = endFileSize - currentFileSize;
    36. }
    37. currentFileSize += buffersize;
    38. byte[] fileBuffer = new byte[buffersize];
    39. randomFile.NextBytes(fileBuffer);
    40. using FileStream fs = File.Create(Path.Combine(randomDirectory.FullName, $"rnd{filesCreated}.rnd"));
    41. fs.Write(fileBuffer, 0, fileBuffer.Length);
    42. ClearCurrentConsoleLine();
    43. Console.Write($"{filesCreated++} files created. Current size total: {currentFileSize / 1024.0 / 1024:f2} MB");
    44. }
    45. filesCreated--;
    46. }
    47. private static void EncryptManyFiles()
    48. {
    49. Console.WriteLine("Encrypt files....");
    50. int filesEncrypted = 0;
    51. timeWatch.Restart();
    52. foreach (FileInfo file in randomDirectory.EnumerateFiles())
    53. {
    54. using FileStream fsRead = file.OpenRead();
    55. using FileStream fsWrite = File.Create(Path.ChangeExtension(file.FullName, ".cryp"));
    56. using CryptoStream csEnrcypt = new CryptoStream(fsWrite, aesEncryption.CreateEncryptor(), CryptoStreamMode.Write);
    57. byte[] buffer = new byte[50000];
    58. int bytesRead = 0;
    59. do
    60. {
    61. bytesRead = fsRead.Read(buffer, 0, buffer.Length);
    62. csEnrcypt.Write(buffer, 0, bytesRead);
    63. } while (bytesRead == buffer.Length);
    64. ClearCurrentConsoleLine();
    65. Console.Write($"{++filesEncrypted} of {filesCreated} files encrypted.");
    66. }
    67. }
    68. private static void DecryptManyFiles()
    69. {
    70. Console.WriteLine("Decrypt files....");
    71. int filesDecrypted = 0;
    72. timeWatch.Restart();
    73. foreach (FileInfo file in randomDirectory.EnumerateFiles("*.cryp"))
    74. {
    75. using FileStream fsRead = file.OpenRead();
    76. using FileStream fsWrite = File.Create(Path.ChangeExtension(file.FullName, ".decryp"));
    77. using CryptoStream csDecrypt = new CryptoStream(fsRead, aesEncryption.CreateDecryptor(), CryptoStreamMode.Read);
    78. byte[] buffer = new byte[50000];
    79. int bytesRead = 0;
    80. do
    81. {
    82. bytesRead = csDecrypt.Read(buffer, 0, buffer.Length);
    83. fsWrite.Write(buffer, 0, bytesRead);
    84. } while (bytesRead == buffer.Length);
    85. ClearCurrentConsoleLine();
    86. Console.Write($"{++filesDecrypted} of {filesCreated} files decrypted.");
    87. }
    88. }
    89. private static void CompareManyFiles()
    90. {
    91. Console.WriteLine("Compare files...");
    92. FileInfo[] rndFiles = randomDirectory.GetFiles("*.rnd");
    93. FileInfo[] decryptFiles = randomDirectory.GetFiles("*.decryp");
    94. for (int i = 0; i < rndFiles.Length; i++)
    95. {
    96. byte[] rndFile = File.ReadAllBytes(rndFiles[i].FullName);
    97. byte[] decryptFile = File.ReadAllBytes(decryptFiles[i].FullName);
    98. byte[] rndHash = shaHash.ComputeHash(rndFile);
    99. byte[] decrypHash = shaHash.ComputeHash(decryptFile);
    100. ClearCurrentConsoleLine();
    101. if (rndHash.SequenceEqual(decrypHash))
    102. {
    103. Console.WriteLine($"rnd{i + 1}.rnd and rnd{i + 1}.decryp are Equal - {BitConverter.ToString(rndHash).Replace("-", "")}");
    104. }
    105. else
    106. {
    107. Console.WriteLine($"rnd{i + 1}.rnd and rnd{i + 1}.decryp differ - {BitConverter.ToString(rndHash).Replace("-", "")} : {BitConverter.ToString(decrypHash).Replace("-", "")}");
    108. }
    109. }
    110. }
    111. private static void EnsureFolder(string folderPath)
    112. {
    113. randomDirectory = new DirectoryInfo(folderPath);
    114. if (randomDirectory.Exists)
    115. {
    116. randomDirectory.Delete(true);
    117. }
    118. randomDirectory.Create();
    119. }
    120. private static void CreateEncryption()
    121. {
    122. byte[] key = new byte[256 / 8];
    123. randomKey.NextBytes(key);
    124. aesEncryption = new AesManaged
    125. {
    126. Key = key,
    127. Padding = PaddingMode.PKCS7,
    128. Mode = CipherMode.CBC
    129. };
    130. aesEncryption.GenerateIV();
    131. }
    132. private static void CreateBigFile()
    133. {
    134. Console.WriteLine("Create big File...");
    135. byte[] bigBuffer = new byte[500000];
    136. using FileStream fsBIG = File.Create(Path.Combine(randomDirectory.FullName, "rndBIG.rnd"));
    137. for (int i = 0; i < 1200; i++)
    138. {
    139. randomFile.NextBytes(bigBuffer);
    140. fsBIG.Write(bigBuffer, 0, bigBuffer.Length);
    141. }
    142. }
    143. private static void EncryptBigFile()
    144. {
    145. Console.WriteLine("Encrypt big file...");
    146. using FileStream fsBig = File.OpenRead(Path.Combine(randomDirectory.FullName, "rndBIG.rnd"));
    147. using FileStream fsBigCryp = File.Create(Path.Combine(randomDirectory.FullName, "rndBIG.cryp"));
    148. using CryptoStream csBigEnrcypt = new CryptoStream(fsBigCryp, aesEncryption.CreateEncryptor(), CryptoStreamMode.Write);
    149. byte[] bigBuffer = new byte[500000];
    150. int bytesBigRead = 0;
    151. do
    152. {
    153. bytesBigRead = fsBig.Read(bigBuffer, 0, bigBuffer.Length);
    154. csBigEnrcypt.Write(bigBuffer, 0, bytesBigRead);
    155. } while (bytesBigRead == bigBuffer.Length);
    156. }
    157. private static void DecryptBigFile()
    158. {
    159. Console.WriteLine("Derypt big file...");
    160. using FileStream fsBigCryp = File.OpenRead(Path.Combine(randomDirectory.FullName, "rndBIG.cryp"));
    161. using FileStream fsBigDecryp = File.Create(Path.Combine(randomDirectory.FullName, "rndBIG.decryp"));
    162. using CryptoStream csBigDecrypt = new CryptoStream(fsBigCryp, aesEncryption.CreateDecryptor(), CryptoStreamMode.Read);
    163. byte[] bigBuffer = new byte[500000];
    164. int bytesBigRead = 0;
    165. do
    166. {
    167. bytesBigRead = csBigDecrypt.Read(bigBuffer, 0, bigBuffer.Length);
    168. fsBigDecryp.Write(bigBuffer, 0, bytesBigRead);
    169. } while (bytesBigRead == bigBuffer.Length);
    170. }
    171. private static void CompareBigFile()
    172. {
    173. Console.WriteLine("Compare big files...");
    174. using FileStream fsBig = File.OpenRead(Path.Combine(randomDirectory.FullName, "rndBIG.rnd"));
    175. using FileStream fsBigDecryp = File.OpenRead(Path.Combine(randomDirectory.FullName, "rndBIG.decryp"));
    176. byte[] bigFileOrig = new byte[fsBig.Length];
    177. byte[] bigFileDecrypt = new byte[fsBigDecryp.Length];
    178. fsBig.Read(bigFileOrig, 0, bigFileOrig.Length);
    179. fsBigDecryp.Read(bigFileDecrypt, 0, bigFileDecrypt.Length);
    180. string hashOrig = BitConverter.ToString(shaHash.ComputeHash(bigFileOrig)).Replace("-", "");
    181. string hashDecryp = BitConverter.ToString(shaHash.ComputeHash(bigFileDecrypt)).Replace("-", "");
    182. if (hashOrig.Equals(hashDecryp, StringComparison.Ordinal))
    183. {
    184. Console.WriteLine("Files are equal - " + hashOrig);
    185. }
    186. else
    187. {
    188. Console.WriteLine($"Files differ - {hashOrig}:{hashDecryp}");
    189. }
    190. }
    191. static void Main(string[] args)
    192. {
    193. EnsureFolder(@"C:\DriveTest\RandomFiles");
    194. timeWatch = Stopwatch.StartNew();
    195. CreateManyFiles();
    196. Console.WriteLine();
    197. Console.WriteLine();
    198. timeWatch.Stop();
    199. writeFileTime = timeWatch.ElapsedMilliseconds;
    200. timeWatch.Restart();
    201. CreateEncryption();
    202. EncryptManyFiles();
    203. Console.WriteLine();
    204. Console.WriteLine();
    205. timeWatch.Stop();
    206. encryptFileTime = timeWatch.ElapsedMilliseconds;
    207. timeWatch.Restart();
    208. DecryptManyFiles();
    209. Console.WriteLine();
    210. Console.WriteLine();
    211. timeWatch.Stop();
    212. decryptFileTime = timeWatch.ElapsedMilliseconds;
    213. timeWatch.Restart();
    214. CompareManyFiles();
    215. Console.WriteLine();
    216. Console.WriteLine();
    217. timeWatch.Stop();
    218. compareFileTime = timeWatch.ElapsedMilliseconds;
    219. Console.WriteLine($"Files created in {writeFileTime / 1000.0:f3} seconds.");
    220. Console.WriteLine($"Files encrypted in {encryptFileTime / 1000.0:f3} seconds");
    221. Console.WriteLine($"Files decrypted in {decryptFileTime / 1000.0:f3} seconds");
    222. Console.WriteLine($"Files compared in {compareFileTime / 1000.0:f3} seconds");
    223. Console.WriteLine($"File operations done in {(writeFileTime + encryptFileTime + decryptFileTime + compareFileTime) / 1000.0:f3} seconds! Press any key continue with big file test.");
    224. Console.ReadKey(true);
    225. Console.Clear();
    226. timeWatch.Restart();
    227. CreateBigFile();
    228. Console.WriteLine();
    229. timeWatch.Stop();
    230. createBigTime = timeWatch.ElapsedMilliseconds;
    231. timeWatch.Restart();
    232. EncryptBigFile();
    233. Console.WriteLine();
    234. timeWatch.Stop();
    235. encryptBigTime = timeWatch.ElapsedMilliseconds;
    236. timeWatch.Restart();
    237. DecryptBigFile();
    238. Console.WriteLine();
    239. timeWatch.Stop();
    240. decryptBigTime = timeWatch.ElapsedMilliseconds;
    241. timeWatch.Restart();
    242. CompareBigFile();
    243. Console.WriteLine();
    244. timeWatch.Stop();
    245. compareBigTime = timeWatch.ElapsedMilliseconds;
    246. Console.WriteLine($"Big file created in {createBigTime / 1000.0:f3} seconds.");
    247. Console.WriteLine($"Big file encrypted in {encryptBigTime / 1000.0:f3} seconds");
    248. Console.WriteLine($"Big file decrypted in {decryptBigTime / 1000.0:f3} seconds");
    249. Console.WriteLine($"Big file compared in {compareBigTime / 1000.0:f3} seconds");
    250. Console.WriteLine($"File operations done in {(createBigTime + encryptBigTime + decryptBigTime + compareBigTime) / 1000.0:f3} seconds! Press any key to close program.");
    251. Console.ReadKey(true);
    252. }
    253. private static void ClearCurrentConsoleLine()
    254. {
    255. int currentLineCursor = Console.CursorTop;
    256. Console.SetCursorPosition(0, Console.CursorTop);
    257. Console.Write(new string(' ', Console.BufferWidth));
    258. Console.SetCursorPosition(0, currentLineCursor);
    259. }
    260. }
    261. }
    VB.NET via Converter

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports System.Security.Cryptography
    3. Module Program
    4. Private writeFileTime As Long = 0
    5. Private encryptFileTime As Long = 0
    6. Private decryptFileTime As Long = 0
    7. Private compareFileTime As Long = 0
    8. Private createBigTime As Long = 0
    9. Private encryptBigTime As Long = 0
    10. Private decryptBigTime As Long = 0
    11. Private compareBigTime As Long = 0
    12. Private endFileSize As Integer = 600 * 1024 * 1024
    13. Private currentFileSize As Integer = 0
    14. Private filesCreated As Integer = 1
    15. Private timeWatch As Stopwatch
    16. Private randomFile As Random = New Random()
    17. Private randomKey As Random = New Random(42)
    18. Private randomDirectory As DirectoryInfo
    19. Private aesEncryption As AesManaged
    20. Private shaHash As SHA1 = SHA1.Create()
    21. Private Sub CreateManyFiles()
    22. Console.WriteLine("Write Files....")
    23. While endFileSize > currentFileSize
    24. Dim buffersize As Integer = (1 + randomFile.NextDouble()) * 1024 * 1024
    25. If currentFileSize + buffersize > endFileSize Then
    26. buffersize = endFileSize - currentFileSize
    27. End If
    28. currentFileSize += buffersize
    29. Dim fileBuffer As Byte() = New Byte(buffersize - 1) {}
    30. randomFile.NextBytes(fileBuffer)
    31. Using fs As FileStream = File.Create(Path.Combine(randomDirectory.FullName, $"rnd{filesCreated}.rnd"))
    32. fs.Write(fileBuffer, 0, fileBuffer.Length)
    33. End Using
    34. ClearCurrentConsoleLine()
    35. Console.Write($"{Math.Min(System.Threading.Interlocked.Increment(filesCreated), filesCreated - 1)} files created. Current size total: {currentFileSize / 1024.0 / 1024} MB")
    36. End While
    37. filesCreated -= 1
    38. End Sub
    39. Private Sub EncryptManyFiles()
    40. Console.WriteLine("Encrypt files....")
    41. Dim filesEncrypted As Integer = 0
    42. timeWatch.Restart()
    43. For Each smallFile As FileInfo In randomDirectory.EnumerateFiles()
    44. Using fsRead As FileStream = smallFile.OpenRead()
    45. Using fsWrite As FileStream = File.Create(Path.ChangeExtension(smallFile.FullName, ".cryp"))
    46. Using csEnrcypt As CryptoStream = New CryptoStream(fsWrite, aesEncryption.CreateEncryptor(), CryptoStreamMode.Write)
    47. Dim buffer As Byte() = New Byte(49999) {}
    48. Dim bytesRead As Integer = 0
    49. Do
    50. bytesRead = fsRead.Read(buffer, 0, buffer.Length)
    51. csEnrcypt.Write(buffer, 0, bytesRead)
    52. Loop While bytesRead = buffer.Length
    53. ClearCurrentConsoleLine()
    54. Console.Write($"{System.Threading.Interlocked.Increment(filesEncrypted)} of {filesCreated} files encrypted.")
    55. End Using
    56. End Using
    57. End Using
    58. Next
    59. End Sub
    60. Private Sub DecryptManyFiles()
    61. Console.WriteLine("Decrypt files....")
    62. Dim filesDecrypted As Integer = 0
    63. timeWatch.Restart()
    64. For Each smallFile As FileInfo In randomDirectory.EnumerateFiles("*.cryp")
    65. Using fsRead As FileStream = smallFile.OpenRead()
    66. Using fsWrite As FileStream = File.Create(Path.ChangeExtension(smallFile.FullName, ".decryp"))
    67. Using csDecrypt As CryptoStream = New CryptoStream(fsRead, aesEncryption.CreateDecryptor(), CryptoStreamMode.Read)
    68. Dim buffer As Byte() = New Byte(49999) {}
    69. Dim bytesRead As Integer = 0
    70. Do
    71. bytesRead = csDecrypt.Read(buffer, 0, buffer.Length)
    72. fsWrite.Write(buffer, 0, bytesRead)
    73. Loop While bytesRead = buffer.Length
    74. ClearCurrentConsoleLine()
    75. Console.Write($"{System.Threading.Interlocked.Increment(filesDecrypted)} of {filesCreated} files decrypted.")
    76. End Using
    77. End Using
    78. End Using
    79. Next
    80. End Sub
    81. Private Sub CompareManyFiles()
    82. Console.WriteLine("Compare files...")
    83. Dim rndFiles As FileInfo() = randomDirectory.GetFiles("*.rnd")
    84. Dim decryptFiles As FileInfo() = randomDirectory.GetFiles("*.decryp")
    85. For i As Integer = 0 To rndFiles.Length - 1
    86. Dim rndFile As Byte() = File.ReadAllBytes(rndFiles(i).FullName)
    87. Dim decryptFile As Byte() = File.ReadAllBytes(decryptFiles(i).FullName)
    88. Dim rndHash As Byte() = shaHash.ComputeHash(rndFile)
    89. Dim decrypHash As Byte() = shaHash.ComputeHash(decryptFile)
    90. ClearCurrentConsoleLine()
    91. If rndHash.SequenceEqual(decrypHash) Then
    92. Console.WriteLine($"rnd{i + 1}.rnd and rnd{i + 1}.decryp are Equal - {BitConverter.ToString(rndHash).Replace("-", "")}")
    93. Else
    94. Console.WriteLine($"rnd{i + 1}.rnd and rnd{i + 1}.decryp differ - {BitConverter.ToString(rndHash).Replace("-", "")} : {BitConverter.ToString(decrypHash).Replace("-", "")}")
    95. End If
    96. Next
    97. End Sub
    98. Private Sub EnsureFolder(ByVal folderPath As String)
    99. randomDirectory = New DirectoryInfo(folderPath)
    100. If randomDirectory.Exists Then
    101. randomDirectory.Delete(True)
    102. End If
    103. randomDirectory.Create()
    104. End Sub
    105. Private Sub CreateEncryption()
    106. Dim key As Byte() = New Byte(31) {}
    107. randomKey.NextBytes(key)
    108. aesEncryption = New AesManaged With {
    109. .Key = key,
    110. .Padding = PaddingMode.PKCS7,
    111. .Mode = CipherMode.CBC
    112. }
    113. aesEncryption.GenerateIV()
    114. End Sub
    115. Private Sub CreateBigFile()
    116. Console.WriteLine("Create big File...")
    117. Dim bigBuffer As Byte() = New Byte(499999) {}
    118. Using fsBIG As FileStream = File.Create(Path.Combine(randomDirectory.FullName, "rndBIG.rnd"))
    119. For i As Integer = 0 To 1200 - 1
    120. randomFile.NextBytes(bigBuffer)
    121. fsBIG.Write(bigBuffer, 0, bigBuffer.Length)
    122. Next
    123. End Using
    124. End Sub
    125. Private Sub EncryptBigFile()
    126. Console.WriteLine("Encrypt big file...")
    127. Using fsBig As FileStream = File.OpenRead(Path.Combine(randomDirectory.FullName, "rndBIG.rnd"))
    128. Using fsBigCryp As FileStream = File.Create(Path.Combine(randomDirectory.FullName, "rndBIG.cryp"))
    129. Using csBigEnrcypt As CryptoStream = New CryptoStream(fsBigCryp, aesEncryption.CreateEncryptor(), CryptoStreamMode.Write)
    130. Dim bigBuffer As Byte() = New Byte(499999) {}
    131. Dim bytesBigRead As Integer = 0
    132. Do
    133. bytesBigRead = fsBig.Read(bigBuffer, 0, bigBuffer.Length)
    134. csBigEnrcypt.Write(bigBuffer, 0, bytesBigRead)
    135. Loop While bytesBigRead = bigBuffer.Length
    136. End Using
    137. End Using
    138. End Using
    139. End Sub
    140. Private Sub DecryptBigFile()
    141. Console.WriteLine("Derypt big file...")
    142. Using fsBigCryp As FileStream = File.OpenRead(Path.Combine(randomDirectory.FullName, "rndBIG.cryp"))
    143. Using fsBigDecryp As FileStream = File.Create(Path.Combine(randomDirectory.FullName, "rndBIG.decryp"))
    144. Using csBigDecrypt As CryptoStream = New CryptoStream(fsBigCryp, aesEncryption.CreateDecryptor(), CryptoStreamMode.Read)
    145. Dim bigBuffer As Byte() = New Byte(499999) {}
    146. Dim bytesBigRead As Integer = 0
    147. Do
    148. bytesBigRead = csBigDecrypt.Read(bigBuffer, 0, bigBuffer.Length)
    149. fsBigDecryp.Write(bigBuffer, 0, bytesBigRead)
    150. Loop While bytesBigRead = bigBuffer.Length
    151. End Using
    152. End Using
    153. End Using
    154. End Sub
    155. Private Sub CompareBigFile()
    156. Console.WriteLine("Compare big files...")
    157. Using fsBig As FileStream = File.OpenRead(Path.Combine(randomDirectory.FullName, "rndBIG.rnd"))
    158. Using fsBigDecryp As FileStream = File.OpenRead(Path.Combine(randomDirectory.FullName, "rndBIG.decryp"))
    159. Dim bigFileOrig As Byte() = New Byte(fsBig.Length - 1) {}
    160. Dim bigFileDecrypt As Byte() = New Byte(fsBigDecryp.Length - 1) {}
    161. fsBig.Read(bigFileOrig, 0, bigFileOrig.Length)
    162. fsBigDecryp.Read(bigFileDecrypt, 0, bigFileDecrypt.Length)
    163. Dim hashOrig As String = BitConverter.ToString(shaHash.ComputeHash(bigFileOrig)).Replace("-", "")
    164. Dim hashDecryp As String = BitConverter.ToString(shaHash.ComputeHash(bigFileDecrypt)).Replace("-", "")
    165. If hashOrig.Equals(hashDecryp, StringComparison.Ordinal) Then
    166. Console.WriteLine("Files are equal - " & hashOrig)
    167. Else
    168. Console.WriteLine($"Files differ - {hashOrig}:{hashDecryp}")
    169. End If
    170. End Using
    171. End Using
    172. End Sub
    173. Sub Main(ByVal args As String())
    174. EnsureFolder("E:\DriveTest\RandomFiles")
    175. timeWatch = Stopwatch.StartNew()
    176. CreateManyFiles()
    177. Console.WriteLine()
    178. Console.WriteLine()
    179. timeWatch.Stop()
    180. writeFileTime = timeWatch.ElapsedMilliseconds
    181. timeWatch.Restart()
    182. CreateEncryption()
    183. EncryptManyFiles()
    184. Console.WriteLine()
    185. Console.WriteLine()
    186. timeWatch.Stop()
    187. encryptFileTime = timeWatch.ElapsedMilliseconds
    188. timeWatch.Restart()
    189. DecryptManyFiles()
    190. Console.WriteLine()
    191. Console.WriteLine()
    192. timeWatch.Stop()
    193. decryptFileTime = timeWatch.ElapsedMilliseconds
    194. timeWatch.Restart()
    195. CompareManyFiles()
    196. Console.WriteLine()
    197. Console.WriteLine()
    198. timeWatch.Stop()
    199. compareFileTime = timeWatch.ElapsedMilliseconds
    200. Console.WriteLine($"Files created in {writeFileTime / 1000.0} seconds.")
    201. Console.WriteLine($"Files encrypted in {encryptFileTime / 1000.0} seconds")
    202. Console.WriteLine($"Files decrypted in {decryptFileTime / 1000.0} seconds")
    203. Console.WriteLine($"Files compared in {compareFileTime / 1000.0} seconds")
    204. Console.WriteLine($"File operations done in {(writeFileTime + encryptFileTime + decryptFileTime + compareFileTime) / 1000.0} seconds! Press any key continue with big file test.")
    205. Console.ReadKey(True)
    206. Console.Clear()
    207. timeWatch.Restart()
    208. CreateBigFile()
    209. Console.WriteLine()
    210. timeWatch.Stop()
    211. createBigTime = timeWatch.ElapsedMilliseconds
    212. timeWatch.Restart()
    213. EncryptBigFile()
    214. Console.WriteLine()
    215. timeWatch.Stop()
    216. encryptBigTime = timeWatch.ElapsedMilliseconds
    217. timeWatch.Restart()
    218. DecryptBigFile()
    219. Console.WriteLine()
    220. timeWatch.Stop()
    221. decryptBigTime = timeWatch.ElapsedMilliseconds
    222. timeWatch.Restart()
    223. CompareBigFile()
    224. Console.WriteLine()
    225. timeWatch.Stop()
    226. compareBigTime = timeWatch.ElapsedMilliseconds
    227. Console.WriteLine($"Big file created in {createBigTime / 1000.0} seconds.")
    228. Console.WriteLine($"Big file encrypted in {encryptBigTime / 1000.0} seconds")
    229. Console.WriteLine($"Big file decrypted in {decryptBigTime / 1000.0} seconds")
    230. Console.WriteLine($"Big file compared in {compareBigTime / 1000.0} seconds")
    231. Console.WriteLine($"File operations done in {(createBigTime + encryptBigTime + decryptBigTime + compareBigTime) / 1000.0} seconds! Press any key to close program.")
    232. Console.ReadKey(True)
    233. End Sub
    234. Private Sub ClearCurrentConsoleLine()
    235. Dim currentLineCursor As Integer = Console.CursorTop
    236. Console.SetCursorPosition(0, Console.CursorTop)
    237. Console.Write(New String(" "c, Console.BufferWidth))
    238. Console.SetCursorPosition(0, currentLineCursor)
    239. End Sub
    240. End Module

    Full-quote removed ~ EaranMaleasi

    woow this is very usefull for me as a beginner. Thank you so much for the effort to make such good example.
    THis is is also be very usefull for others. I will do amalyze it and give you some feedback when I'm done :)
    It's really amazing because you don't use backgroundworker.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „EaranMaleasi“ ()

    As moderator:
    @zackmark29 please stop quoting the posts above you completely. Only use quotes when you want to answer something specific within a post, that has multiple lines or even parts to it.

    BTT:
    Well, I just got curious, and wrote it for myself. So why just let it go to waste on my drive when I could share it and probably help.

    Also, I don't really get what you mean by

    zackmark29 schrieb:

    It's really amazing because you don't use backgroundworker.
    Because if you use this code as is, it would certainly block your UI if you would repurpose that for a WinForms project. The Console is a bit diffrent when it comes to that.

    Besides that, there are so many more and better ways to do Threading/Multitasking/Async computing. The best of course, also the newest, Async/Await in Combination with Tasks. But what exactly are you trying to achieve with the BackgroundWorker ?

    Well, I just got curious, and wrote it for myself. So why just let it go to waste on my drive when I could share it and probably help.

    Yes absolutely it's really a big help


    Besides that, there are so many more and better ways to do Threading/Multitasking/Async computing. The best of course, also the newest, Async/Await in Combination with Tasks.

    Yes someone also suggested to me to use async/await. But only few tutorial in the internet that's why I can't still figure out how I can use it with filestream.
    They said it's better and simple than backgroundworker.


    But what exactly are you trying to achieve with the BackgroundWorker ?

    nothing actually. I just don't know how to use other way than backgroundworker