在我们的网络应用程序中,我们有一个上传部分,您可以上传和下载PDF文件。它会在上传时加密它们并在下载时解密它们。它起初工作正常但现在有几个文件损坏了。
某些PDF文件每次都会成功加密和解密,而且每次都会损坏某些PDF文件。我尝试使用我之前知道的相同PDF文件重新测试它,它不再有效。因此,虽然它似乎是特定于文件的,但之前有效的文件不一定有效。
这是我正在使用的加密类:
Public Class clsEncryptFile
Public Sub Encrypt(inputFilePath As String, outputfilePath As String)
Dim EncryptionKey As String = "FAKEKEY"
Using encryptor As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using fs As New FileStream(outputfilePath, FileMode.Create)
Using cs As New CryptoStream(fs, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
Using fsInput As New FileStream(inputFilePath, FileMode.Open)
Dim data As Integer
While (Assign(data, fsInput.ReadByte())) <> -1
cs.WriteByte(CByte(data))
End While
End Using
End Using
End Using
End Using
End Sub
Public Sub Decrypt(inputFilePath As String, outputfilePath As String)
Dim EncryptionKey As String = "FAKEKEY"
Using encryptor As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _
&H65, &H64, &H76, &H65, &H64, &H65, _
&H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using fs As New FileStream(inputFilePath, FileMode.Open)
Using cs As New CryptoStream(fs, encryptor.CreateDecryptor(), CryptoStreamMode.Read)
Using fsOutput As New FileStream(outputfilePath, FileMode.Create)
Dim data As Integer
While (Assign(data, cs.ReadByte())) <> -1
fsOutput.WriteByte(CByte(data))
End While
End Using
End Using
End Using
End Using
End Sub
Private Shared Function Assign(Of T)(ByRef source As T, ByVal value As T) As T
source = value
Return value
End Function
End Class
这是加密文件的子程序(使用telerik radasyncupload事件):
Private Sub _upWorkPapers_FileUploaded(sender As Object, e As Telerik.Web.UI.FileUploadedEventArgs) Handles _upWorkPapers.FileUploaded
Dim newfilename As String
newfilename = Session("Asi_Num") & "_" & _hdnNoteDate.Value.Replace("/", "") '& e.File.GetExtension
e.File.SaveAs(IO.Path.Combine(Server.MapPath(_upWorkPapers.TargetFolder), newfilename & e.File.GetExtension))
Dim input As String = Convert.ToString(Server.MapPath("/Repository/WorkPapers/") & newfilename) & e.File.GetExtension
Dim output As String = Convert.ToString((Server.MapPath("/Repository/WorkPapers/") & newfilename) + "_enc") & e.File.GetExtension
'Encrypt the file
'crptFile is declared at top of the class as Dim crptFile As New clsEncryptFile
crptFile.Encrypt(input, output)
'Set hidden value to store filename
_hdnFileName.Value = newfilename
_hdnFileExtension.Value = e.File.GetExtension
'Delete Original File
File.Delete(input)
End Sub
这是解密文件的子:
Protected Sub DownloadFile()
Dim input As String = Convert.ToString((Server.MapPath("/Repository/WorkPapers/") & _hdnFileName.Value) + "_enc") & _hdnFileExtension.Value
Dim output As String = Convert.ToString((Server.MapPath("/Repository/WorkPapers/") & _hdnFileName.Value) + "_dec") & _hdnFileExtension.Value
'crptFile is declared at top of class as Dim crptFile As New clsEncryptFile
crptFile.Decrypt(input, output)
''Download the Decrypted File.
Response.Clear()
Response.ContentType = "application/pdf"
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(output))
Response.WriteFile(output)
Response.Flush()
'Delete the decrypted (output) file.
File.Delete(output)
End Sub
自实施以来,这些都没有改变,但突然间它的功能非常不稳定。有没有人对可能造成这种情况有什么想法?