''' <summary>
''' 存在多行加密数据的文件解密
''' </summary>
''' <param name="encryptedFile">加密文件源文件完整路径及其名称</param>
''' <param name="DeclassifiedFile">加密后文件存放路径及其名称</param>
''' <param name="key">密码</param>
''' <param name="iv">偏移量</param>
Private Sub FileDecrypt(encryptedFile As String, DeclassifiedFile As String, key As String, iv As String)
Dim fstream As FileStream = Nothing
Dim fswrite As FileStream = Nothing
Dim sr As StreamReader = Nothing
Dim cs As CryptoStream = Nothing
Try
Dim btKey As Byte() = Encoding.Default.GetBytes(key)
Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
Dim des As New DESCryptoServiceProvider With { '设置des解码模式
.Mode = CipherMode.ECB,
.Padding = PaddingMode.Zeros
}
fstream = New FileStream(encryptedFile, FileMode.Open, FileAccess.Read) '文件读取
fswrite = New FileStream(DeclassifiedFile, FileMode.Create, FileAccess.Write) '文件写入
sr = New StreamReader(fstream)
cs = New CryptoStream(fswrite, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)
Dim strLine As String = sr.ReadLine
Do While strLine IsNot Nothing
Try
Dim inData As Byte() = Convert.FromBase64String(strLine) 'base64解码
cs.Write(inData, 0, inData.Length)
cs.Flush()
fswrite.Flush() '存入文件
Catch ex As Exception
End Try
strLine = sr.ReadLine() '读取下一行数据
Loop
MessageBox.Show("success")
Catch ex As Exception
Finally
Try
If sr IsNot Nothing Then
sr.Dispose()
End If
If fstream IsNot Nothing Then
fstream.Dispose()
End If
If fswrite IsNot Nothing Then
fswrite.Dispose()
End If
If cs IsNot Nothing Then
cs.Dispose()
End If
Catch ex As Exception
End Try
End Try
End Sub
一个文件可能存在多行加密数据,使用流逐行读取进行解密并存入新的文件中。