Advertisement
coderail

SecureFile - VB.NET

Apr 24th, 2012
1,586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Security.Cryptography
  3.  
  4. '------------------
  5. 'Creator: aeonhack
  6. 'Site: elitevs.net
  7. 'Created: 4/16/2011
  8. 'Changed: 4/24/2012
  9. 'Version: 1.0.0
  10. '------------------
  11.  
  12. Class SecureFile
  13.  
  14. Enum Status As Byte
  15. Success = 0
  16. Failed = 1
  17. Progress = 2
  18. End Enum
  19.  
  20. Private Current As Long
  21. Private Maximum As Long
  22.  
  23. Private Data As Byte()
  24. Private Destination As String
  25.  
  26. Private SR, SW As Stream
  27. Private CS As CryptoStream
  28. Private FSR, FSW As FileStream
  29. Private SA As SymmetricAlgorithm
  30.  
  31. Private Callback As StatusCB
  32. Public Delegate Sub StatusCB(status As Status, value As Integer)
  33.  
  34. Sub New(_sa As SymmetricAlgorithm, _callback As StatusCB)
  35. SA = _sa
  36. Callback = _callback
  37. Data = New Byte(5242880 - 1) {}
  38. End Sub
  39.  
  40. Public Sub Secure([in] As String, out As String)
  41. Try
  42. Initialize([in], out)
  43. CS = New CryptoStream(FSW, SA.CreateEncryptor(), CryptoStreamMode.Write)
  44.  
  45. SR = FSR
  46. SW = CS
  47. BeginProcess()
  48. Catch
  49. HandleFailure()
  50. End Try
  51. End Sub
  52.  
  53. Public Sub Unsecure([in] As String, out As String)
  54. Try
  55. Initialize([in], out)
  56. CS = New CryptoStream(FSR, SA.CreateDecryptor(), CryptoStreamMode.Read)
  57.  
  58. SR = CS
  59. SW = FSW
  60. BeginProcess()
  61. Catch
  62. HandleFailure()
  63. End Try
  64. End Sub
  65.  
  66. Private Sub Initialize([in] As String, out As String)
  67. Destination = out
  68. FSW = New FileStream(out, FileMode.Create, FileAccess.Write)
  69. FSR = New FileStream([in], FileMode.Open, FileAccess.Read)
  70. End Sub
  71.  
  72. Private Sub BeginProcess()
  73. Current = 0
  74. Maximum = FSR.Length
  75.  
  76. Dim O As IAsyncResult = SR.BeginRead(Data, 0, Data.Length, AddressOf Process, Nothing)
  77. If O.CompletedSynchronously Then Process(O)
  78. End Sub
  79.  
  80. Private Sub Process(r As IAsyncResult)
  81. If _Cancel Then
  82. HandleFailure()
  83. Return
  84. End If
  85.  
  86. Try
  87. Dim I As Integer = SR.EndRead(r)
  88. Current += I
  89.  
  90. If Not I = 0 Then
  91. SW.Write(Data, 0, I)
  92. Callback(Status.Progress, CInt(Current / Maximum * 100))
  93.  
  94. Dim O As IAsyncResult = SR.BeginRead(Data, 0, Data.Length, AddressOf Process, Nothing)
  95. If O.CompletedSynchronously Then Process(O)
  96. Else
  97. CS.Close()
  98. FSW.Close()
  99. FSR.Close()
  100.  
  101. Callback(Status.Progress, 100)
  102. Callback(Status.Success, 0)
  103. End If
  104. Catch 'CryptoException for pass mismatch
  105. HandleFailure()
  106. End Try
  107. End Sub
  108.  
  109. Private Sub HandleFailure()
  110. Try
  111. If CS IsNot Nothing Then CS.Close()
  112. Catch
  113. 'Do nothing
  114. End Try
  115.  
  116. If FSR IsNot Nothing Then FSR.Close()
  117. If FSW IsNot Nothing Then FSW.Close()
  118.  
  119. Try
  120. File.Delete(Destination)
  121. Catch
  122. 'Do nothing.
  123. End Try
  124.  
  125. Callback(Status.Failed, 0)
  126. End Sub
  127.  
  128. Private _Cancel As Boolean
  129. Public Sub Cancel()
  130. _Cancel = True
  131. End Sub
  132.  
  133. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement