对象引用未设置为字节处对象的实例

时间:2017-02-26 02:39:01

标签: vb.net

对不起,我有一个程序而且我收到了错误。错误

 fsdekrip.Write(dbytes, 0, dbytes.Length)

请帮助我学习。谢谢,抱歉我的英语不好。

这是我的代码:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim inputfile As String = TextBox1.Text
        Dim outputfile As String = TextBox3.Text
        Dim keyInD As String = TextBox2.Text
        Dim decrypted As Byte() = decrypDES(inputfile, keyInD)
    bf.Key = System.Text.Encoding.UTF8.GetBytes("aaaaaaaa")
    'Dim fbytes As Byte() = File.ReadAllBytes(baru)
    Dim dbytes As Byte() = bf.DecodeBytes(decrypted)
    Dim fsdekrip As New FileStream(outputfile, FileMode.Create, FileAccess.Write)
    fsdekrip.Write(dbytes, 0, dbytes.Length) 'here is error
    fsdekrip.Flush()
    fsdekrip.Close()

End Sub

'此函数用于解密DES

Private Function decrypDES(ByVal cipherteks As String, ByVal key As String) As Byte()
    Dim decrypted() As Byte

    If cipherteks Is Nothing OrElse cipherteks.Length <= 0 Then
        Throw New ArgumentNullException("cipherteks")
    End If
    If key Is Nothing OrElse key.Length <= 0 Then
        Throw New ArgumentNullException("Key")
    End If

    Dim cs As CryptoStream
    Dim ms As New MemoryStream()
    Dim des As New DESCryptoServiceProvider
    des.Key = ASCIIEncoding.ASCII.GetBytes(key)
    'des.IV = ASCIIEncoding.ASCII.GetBytes(StrReverse(IV))

    cs = New CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)
    Dim fsdekrip As New StreamWriter(cs)
    fsdekrip.Write(cipherteks)
    decrypted = ms.ToArray()

    Return decrypted
End Function

这适用于DecodeBytes方法:

Public Function DecodeBytes(Bytes As Byte()) As Byte()

    Dim ResLength As Integer = Bytes.Length Mod 8

    If ResLength <> 0 Then
        LastError = DateTime.Now & " : " & "Incorrect Data Length"
        RaiseEvent Terminated(LastError)
    End If

    Try
        PrepareTheFish()
        Return SessionFish.Decode(Bytes)

    Catch ex As Exception
        LastError = DateTime.Now & " : " & ex.Message
        RaiseEvent Terminated(LastError)
    End Try

    Return Nothing

End Function

0 个答案:

没有答案