2013-04-13 26 views
0

我試圖保存一個文件,然後crypt是並刪除臨時的未加密文件。這是我的加密子,錯誤行出現在最後一行。該進程無法訪問該文件[VB.Net]

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() 

    'Set secret key for DES algorithm. 
    'A 64-bit key and an IV are required for this provider. 
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey) 

    'Set the initialization vector. 
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey) 

    'Create the DES encryptor from this instance. 
    Dim desencrypt As ICryptoTransform = DES.CreateEncryptor() 
    'Create the crypto stream that transforms the file stream by using DES encryption. 
    Dim cryptostream As New CryptoStream(fsEncrypted, _ 
             desencrypt, _ 
             CryptoStreamMode.Write) 

    'Read the file text to the byte array. 
    Dim bytearrayinput(fsInput.Length - 1) As Byte 
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length) 
    'Write out the DES encrypted file. 
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length) 
    cryptostream.Close() 
    System.IO.File.Delete(sInputFilename) 
End Sub 

任何人都可以幫我解決這個問題嗎?我無法弄清楚我做錯了什麼。

+0

您是否嘗試過發行'fsInput.Close()''之前System.IO.File.Delete(sInputFilename)'對不對? –

回答

0

使用權fsInput.Close()System.IO.File.Delete(sInputFilename)

希望這有助於

相關問題