[英]Cryptography in VB.net - decrypted file is larger than source file?
您好我一直在嘗試使用System.Security.Cryptography加密和解密文件,但它不適合我
這段代碼
Private Sub EncryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
Dim fsInput As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
Dim fsEncrypted As New FileStream(sOutputFilename, FileMode.Create, FileAccess.Write)
Dim DES As New DESCryptoServiceProvider()
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
Dim cryptostream As New CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write)
Dim bytearrayinput(fsInput.Length - 1) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Close()
End Sub
打電話給
EncryptFile(OpenFileDialog1.FileName, SaveFileDialog1.FileName, "12345678")[/CODE]
似乎工作正常,我得到一個與源文件大小相同的文件
繼承人哪里出錯了
這段代碼
Private Sub DecryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
Dim DES As New DESCryptoServiceProvider()
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()
End Sub
打電話給
DecryptFile(OpenFileDialog1.FileName, SaveFileDialog1.FileName, "12345678")[/CODE]
輸出的文件幾乎是加密源文件的2倍。
什么事情發生,我肯定這幾周前工作正常,我看不出任何明顯的錯誤。
有什么想法嗎?
主要問題是EncryptFile使用字節數組讀取數據,而DecryptFile使用流讀取數據。 EncryptFile和DecryptFile方法之間的唯一區別應該是ICryptoTransform
賦值。 在1個過程中使用公共代碼會更容易:
Private Sub EncryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
Crypto(sInputFilename, sOutputFilename, sKey, True)
End Sub
Private Sub DecryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
Crypto(sInputFilename, sOutputFilename, sKey, False)
End Sub
Private Sub Crypto(ByVal sInputFileName As String, ByVal sOutputFileName As String, ByVal sKey As String, ByVal bEncrypt As Boolean)
'Define the service provider
Dim DES As New DESCryptoServiceProvider()
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'Read the input file into array
Dim fsInput As New FileStream(sInputFileName, FileMode.Open, FileAccess.Read)
Dim bytearrayinput(fsInput.Length - 1) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
'Define the crypto transformer
Dim cryptoTransform As ICryptoTransform
If bEncrypt Then
cryptoTransform = DES.CreateEncryptor()
Else
cryptoTransform = DES.CreateDecryptor
End If
'Create the encrypting streams
Dim fsEncrypted As New FileStream(sOutputFileName, FileMode.Create, FileAccess.Write)
Dim cryptostream As New CryptoStream(fsEncrypted, cryptoTransform, CryptoStreamMode.Write)
'Write the output file
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Close()
End Sub
加密過程幾乎與EncryptFile相同。 不同之處在於我根據您是加密還是解密來更改ICryptoTransform
分配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.