我是一个自学成才的新手试验 Rijndael encryption algorithm,我之前已经很好地工作了,但是因为我试图让后台工作者运行加密来为用户释放表单我自己遇到了很多问题. 首先,我尝试将我在前面
首先,我尝试将我在前面的代码中使用的byval放入后台工作程序,但是我得到一个错误,它不兼容 – 特别是ByVal Direction As CryptoAction.
所以要清楚,我正在尝试这样的事情:
Private Sub bw_DoWork(ByVal sender As Object,ByVal strInputFile As String, ByVal strOutputFile As String, ByVal bytKey() As Byte, ByVal bytIV() As Byte, ByVal Direction As CryptoAction, ByVal e As DoWorkEventArgs) Dim worker As BackgroundWorker = CType(sender, BackgroundWorker) For i = 1 To 1 If bw.CancellationPending = True Then e.Cancel = True Exit For Else 'Encryption code goes here. end sub
但正如我所说,我被告知方向与CrytoAction不兼容.在完整的completness中,那个sub是:
Private Enum CryptoAction 'Define the enumeration for CryptoAction. ActionEncrypt = 1 ActionDecrypt = 2 End Enum
所以为了让代码工作我完成了这个,我认为这就是为什么我的加密文件解密成一个不可读的文件,我有时会得到错误:填充无效,无法删除.关闭文件流时.
所以我在我的公共类中声明了bytkey,byIV,Direction和其余部分,就像我说的那样让代码运行.但是我可以在加密后恢复文件.我的想法是,当我的代码引用函数时,它不是接收返回的值,而是接收一个空字符串?
这是引用生成键的函数的代码,只是为了清楚我的意思.
Select Case Direction Case CryptoAction.ActionEncrypt csCryptoStream = New CryptoStream(fsOutput, _ cspRijndael.CreateEncryptor(bytKey, bytIV), _ CryptoStreamMode.Write) Case CryptoAction.ActionDecrypt csCryptoStream = New CryptoStream(fsOutput, _ cspRijndael.CreateDecryptor(bytKey, bytIV), _ CryptoStreamMode.Write) End Select
所以,我的问题是,我是正确的,代码是从声明中引用空值,还是代码仍然引用函数?并且,是否可以将ByVal CryptoStream添加到后台工作程序中?
我对我的问题的大小我最真实的道歉,我到处寻找解决方案,但找不到任何相关的东西.
您无法更改DoWork事件的签名,但它包含一个通用对象供您使用(称为e.Argument).定义一个自定义类,以包含要移入和移出BackgroundWorker的所有数据.Option Strict On Public Class Form1 Class MyParameters Friend Input As Integer Friend Output As Integer Friend Message As String End Class Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim m As New MyParameters m.Input = 1 ListBox1.Items.Add("Button1_Click") ListBox1.Items.Add(" Input: " & m.Input) BackgroundWorker1.WorkerReportsProgress = True BackgroundWorker1.RunWorkerAsync(m) 'this triggers BackgroundWorker1_DoWork. When finished, BackgroundWorker1_RunWorkerCompleted() is raised End Sub Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim m As MyParameters = DirectCast(e.Argument, MyParameters) 'Convert the generic Object back into a MyParameters object m.Message = "Progress report" BackgroundWorker1.ReportProgress(50, m) 'this triggers BackgroundWorker1.ProgressChanged. N.B. We could actually use a different class here, but we'll re-use the same class for simplicity m.Output = m.Input + 1 e.Result = m 'fun continues at BackgroundWorker1_RunWorkerCompleted End Sub Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged ListBox1.Items.Add("BackgroundWorker1_ProgressChanged") Dim m As MyParameters = DirectCast(e.UserState, MyParameters) 'Convert the generic Object back into a MyParameters object ListBox1.Items.Add(" " & m.Message & " " & e.ProgressPercentage.ToString) End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted ListBox1.Items.Add("BackgroundWorker1_RunWorkerCompleted") Dim m As MyParameters = DirectCast(e.Result, MyParameters) 'Convert the generic Object back into a MyParameters object ListBox1.Items.Add(" Output: " & m.Output) End Sub End Class