locked
When Decrypting file, I got the error: The input data is not a complete block RRS feed

  • Question

  • User-786564416 posted

    I have tried the following code in order to decrypt a file that has been encrypted:

    Private Sub Decrypt(inputFilePath As String, outputfilePath As String)
    
            Dim EncryptionKey As String = "MAKV2SPBNI99212"
    
            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

    WHERE IS THE PROBLEM?

    Thursday, October 11, 2018 3:42 PM

Answers

  • User-786564416 posted

    I made it exactly as what you propose, but I got that, "the outputfilepath,(Server.MapPath("/csharpdemo/check3.txt") is not found". This means that it doesn't generates the decrypted file (Original file) automatically. It shows like it expects the existence of the file, which is not a common sense as the decrypted file (check3.txt) is my target when running the Decrypt sub.

    For the decryption, it done perfectly without any problem from the first time. The Encryption Sub generates the encrypted file (D:\E\MurasalatStorage\MinisterOffice\2018-1069-100A_enc.pdf)

    The Encryption Sub Code is as following where the inputfilepath is the path of the existing file to be decrypted. And the outputfilepath is the encrypted file name and path to be generated by the Encrypt Sub:

    Private Sub Encrypt(inputFilePath As String, outputfilePath As String)
    
            Dim EncryptionKey As String = "MAKV2SPBNI99212"
    
            Dim salt1 As Byte() = New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76}
    
            Using encryptor As Aes = Aes.Create()
    
                Dim k1 As New Rfc2898DeriveBytes(EncryptionKey, salt1)
    
                encryptor.KeySize = 128
                encryptor.BlockSize = 128
                encryptor.Key = k1.GetBytes(16)
                encryptor.IV = k1.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
    
                k1.Reset()
    
            End Using
    
        End Sub

    The Decryption code is as the following, where the inputFilePath is the encrypted file generated above (D:\E\MurasalatStorage\MinisterOffice\2018-1069-100A_enc.pdf), and the outputfilepath is the name of the decrypted file that supposed to be generated by the Decrypt Sub, which I passed it as (D:\E\MurasalatStorage\MinisterOffice\2018-1069-100A_dec.pdf). 

    Public Shared Sub Decrypt(inputFilePath As String, outputfilePath As String)
    
            Dim DecryptionKey As String = "MAKV2SPBNI99212"
    
            Dim salt1 As Byte() = New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76}
    
            Dim data As String = "my password"
    
            Dim k2 As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(DecryptionKey, salt1)
            Dim descrypt As Aes = Aes.Create()
    
            descrypt.KeySize = 128
            descrypt.BlockSize = 128
            descrypt.Key = k2.GetBytes(128 / 8)
            descrypt.IV = k2.GetBytes(128 / 8)
    
            Dim fsOutput As New FileStream(outputfilePath, FileMode.Open)
    
            Dim dec As CryptoStream = New CryptoStream(fsOutput, descrypt.CreateDecryptor(), CryptoStreamMode.Write)
            Dim edata As Byte() = File.ReadAllBytes(inputFilePath)
    
            dec.Write(edata, 0, edata.Length)
    
            dec.Flush()
            dec.Close()
            k2.Reset()
    
    
        End Sub

    When it reaches the statement:

    dim fsOutput as New FileStream(outputfilepath,FileMode.open),

    I get the error: Could not find file 'D:\E\MusrasalatStorage\MinisterOffice\2018-1069-100A_dec.pdf'

    The error shown as attached:

    So Where exactly the error?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2018 7:53 PM
  • User475983607 posted

    Mr. Mgebhard thanks for attempt to help but your guessing is totally wrong.

    I'm not guessing.... the file does not exist.  I gave you the benefit of the doubt on this one... but...

    Please try to understand before any intervention. The E is the folder name. And the path is correct but the file name supposed to be generated automatically by the Decrypt sub with the name passed in the outputfilePath parameter.

    As far as I can tell, your expectation does not match the code.  As written the Decrypt function expects the file to exist.  This line of code tries to open a file and read the contents as a stream.

    Dim fsOutput As New FileStream(outputfilePath, FileMode.Open)

    https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream?view=netframework-4.7.2

    Use this code to create a file if that's what you are trying to do.

    using (FileStream fs = new FileStream(path, FileMode.Create))

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2018 8:53 PM
  • User475983607 posted

    What about the type of the file I want to create? Suppose that the file that I want to create is a pdf file, will it make it the same?

    I'm not sure what you're asking. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2018 10:11 PM
  • User-893317190 posted

    Hi alihusain_77,

    As mgebhard has said, you  could use  FileMode.Create if the file doesn't exist.

    As to pdf file, if you write in this way ,it could also decrypt  the file but you couldn't open the decrypted file.

    I suggest you could save the  decrypted file as text and convert it to pdf first.

    About how to convert a text file to pdf  in vb.net , you could refer to the link below 

    https://www.c-sharpcorner.com/forums/convert-text-to-pdf-using-vb-net

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 16, 2018 2:02 AM

All replies

  • User-893317190 posted

    Hi alihusain_77,

    This may caused by the way your encrypt your code.

    Below is a demo for encrypting and descrypting.

     Dim pwd1 As String = "MAKV2SPBNI99212"
    
            Dim salt1 As Byte() = New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76}
    
    
            Dim data As String = "my password"
    
            Dim k1 As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(pwd1, salt1)
            Dim encryptor As Aes = Aes.Create()
    
            encryptor.Key = k1.GetBytes(32)
    
    
            Dim fsinput As New FileStream(Server.MapPath("/csharpdemo/check.txt"), FileMode.Open)
    
            Dim encrypt As CryptoStream = New CryptoStream(fsinput, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
    
            Dim utfd1 As Byte() = System.Text.Encoding.Default.GetBytes(data)
    
            encrypt.Write(utfd1, 0, utfd1.Length)
            encrypt.FlushFinalBlock()
            encrypt.Close()
            k1.Reset()
            Dim descrypt As Aes = Aes.Create()
            descrypt.Padding = PaddingMode.None
            descrypt.Key = k1.GetBytes(32)
    
            descrypt.IV = encryptor.IV
    
    
            Dim fsOutput As New FileStream(Server.MapPath("/csharpdemo/check2.txt"), FileMode.Open)
    
            Dim dec As CryptoStream = New CryptoStream(fsOutput, descrypt.CreateDecryptor(), CryptoStreamMode.Write)
            Dim edata As Byte() = File.ReadAllBytes(Server.MapPath("/csharpdemo/check.txt"))
    
            dec.Write(edata, 0, edata.Length)
            dec.Flush()
            dec.Close()
            k1.Reset()

    You could also refer to the link below , it has similar problem to yours.

    https://stackoverflow.com/questions/19614178/aes-encryption-error-the-input-data-is-not-a-complete-block

    https://social.msdn.microsoft.com/Forums/en-US/5c87da54-d45d-4f79-bf5c-ed61d4a4c7b5/c-decrypt-problem-?forum=csharpgeneral

    Best regards,

    Ackerly Xu

    Friday, October 12, 2018 5:43 AM
  • User-786564416 posted

    In the Decryption part, What I should actually write the code?

    The encrypting and decrypting are on two different forms. So let's suppose that for the decryption Sub, I have the parameters as following:

    Public Shared Sub Decrypt(inputFilePath As String, outputfilePath As String)

    Where the inputFilePath is the full path of the encrypted file, for example "D:\MyStorage\MyOffice\2018-1071-100A_enc.pdf". The outputfilePath is the full path of the supposed decrypted file (Lets assume its path: D:\MyStorage\MyOffice\2018-1071-100A_dec.pdf)

    The Sub I made is that release error is as following

    Public Shared Sub Decrypt(inputFilePath As String, outputfilePath As String)
    
    Dim pwd1 As String = "MAKV2SPBNI99212"
    
    Dim salt1 As Byte() = New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76}
    
    Dim data As String = "my password"
    
    Dim k1 As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(pwd1, salt1)
    Dim encryptor As Aes = Aes.Create()
    
    encryptor.Key = k1.GetBytes(32)
    
    k1.Reset()
    Dim descrypt As Aes = Aes.Create()
    descrypt.Padding = PaddingMode.None
    descrypt.Key = k1.GetBytes(32)
    
    descrypt.IV = encryptor.IV
    
    Dim fsOutput As New FileStream(outputfilePath, FileMode.Open)
    
    Dim dec As CryptoStream = New CryptoStream(fsOutput, descrypt.CreateDecryptor(), CryptoStreamMode.Write)
    Dim edata As Byte() = File.ReadAllBytes(outputfilePath)
    
    dec.Write(edata, 0, edata.Length)
    dec.Flush()
    dec.Close()
    k1.Reset()
    
    End Sub
    

    I expect that when calling this sub, it should generate the decrypted file, outputfilepath. with the full path as passed as parameter (D:\MyStorage\MyOffice\2018-1071-100A_dec.pdf). However, when it executing the following statement

    Dim fsOutput As New FileStream(outputfilePath, FileMode.Open)

    It generates the error: File (D:\MyStorage\MyOffice\2018-1071-100A_dec.pdf) is not found.

    So, how to tackle this problem?

    Sunday, October 14, 2018 4:44 PM
  • User-893317190 posted

    Hi alihusain_77,

    I have made an encapsulates a method of decryption.

    You only need to use descrypt in the method.

    Below is my code.

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim pwd1 As String = "MAKV2SPBNI99212"
    
            Dim salt1 As Byte() = New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64}
            
    
            Dim data As String = "Some  test  data Some  test  data Some  test  data Some  test  data "
    
            Dim k1 As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(pwd1, salt1)
    
            Dim encryptor As Aes = Aes.Create()
            encryptor.KeySize = 128
            encryptor.BlockSize = 128
    
            encryptor.Key = k1.GetBytes(128 / 8)
            encryptor.IV = k1.GetBytes(128 / 8)
    
            Dim fsinput As New FileStream(Server.MapPath("/csharpdemo/check.txt"), FileMode.Open)
    
            Dim encrypt As CryptoStream = New CryptoStream(fsinput, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
    
            Dim utfd1 As Byte() = New System.Text.UTF8Encoding(False).GetBytes(data)
    
            encrypt.Write(utfd1, 0, utfd1.Length)
            encrypt.FlushFinalBlock()
            encrypt.Close()
            k1.Reset()
    
        End Sub
    
        Public Shared Sub Decrypt(inputFilePath As String, outputfilePath As String)
            Dim pwd1 As String = "MAKV2SPBNI99212"
    
            Dim salt1 As Byte() = New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64}
    
            Dim k2 As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(pwd1, salt1)
            Dim descrypt As Aes = Aes.Create()
    
    
            descrypt.KeySize = 128
            descrypt.BlockSize = 128
            descrypt.Key = k2.GetBytes(128 / 8)
    
    
            descrypt.IV = k2.GetBytes(128 / 8)
    
            Dim fsOutput As New FileStream(outputfilePath, FileMode.Open)
    
            Dim dec As CryptoStream = New CryptoStream(fsOutput, descrypt.CreateDecryptor(), CryptoStreamMode.Write)
            Dim edata As Byte() = File.ReadAllBytes(inputFilePath)
    
            dec.Write(edata, 0, edata.Length)
    
            dec.Flush()
            dec.Close()
            k2.Reset()
        End Sub
    
        Protected Sub Button1_Click(sender As Object, e As EventArgs)
            Decrypt(Server.MapPath("/csharpdemo/check.txt"), Server.MapPath("/csharpdemo/check3.txt"))
            Response.Write(File.ReadAllText(Server.MapPath("/csharpdemo/check3.txt")))
        End Sub

    The code I marked as yellow of encrypt and descrypt should be the same value.

    Best regards,

    Ackerly Xu

    Monday, October 15, 2018 7:46 AM
  • User-786564416 posted

    I made it exactly as what you propose, but I got that, "the outputfilepath,(Server.MapPath("/csharpdemo/check3.txt") is not found". This means that it doesn't generates the decrypted file (Original file) automatically. It shows like it expects the existence of the file, which is not a common sense as the decrypted file (check3.txt) is my target when running the Decrypt sub.

    For the decryption, it done perfectly without any problem from the first time. The Encryption Sub generates the encrypted file (D:\E\MurasalatStorage\MinisterOffice\2018-1069-100A_enc.pdf)

    The Encryption Sub Code is as following where the inputfilepath is the path of the existing file to be decrypted. And the outputfilepath is the encrypted file name and path to be generated by the Encrypt Sub:

    Private Sub Encrypt(inputFilePath As String, outputfilePath As String)
    
            Dim EncryptionKey As String = "MAKV2SPBNI99212"
    
            Dim salt1 As Byte() = New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76}
    
            Using encryptor As Aes = Aes.Create()
    
                Dim k1 As New Rfc2898DeriveBytes(EncryptionKey, salt1)
    
                encryptor.KeySize = 128
                encryptor.BlockSize = 128
                encryptor.Key = k1.GetBytes(16)
                encryptor.IV = k1.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
    
                k1.Reset()
    
            End Using
    
        End Sub

    The Decryption code is as the following, where the inputFilePath is the encrypted file generated above (D:\E\MurasalatStorage\MinisterOffice\2018-1069-100A_enc.pdf), and the outputfilepath is the name of the decrypted file that supposed to be generated by the Decrypt Sub, which I passed it as (D:\E\MurasalatStorage\MinisterOffice\2018-1069-100A_dec.pdf). 

    Public Shared Sub Decrypt(inputFilePath As String, outputfilePath As String)
    
            Dim DecryptionKey As String = "MAKV2SPBNI99212"
    
            Dim salt1 As Byte() = New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76}
    
            Dim data As String = "my password"
    
            Dim k2 As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(DecryptionKey, salt1)
            Dim descrypt As Aes = Aes.Create()
    
            descrypt.KeySize = 128
            descrypt.BlockSize = 128
            descrypt.Key = k2.GetBytes(128 / 8)
            descrypt.IV = k2.GetBytes(128 / 8)
    
            Dim fsOutput As New FileStream(outputfilePath, FileMode.Open)
    
            Dim dec As CryptoStream = New CryptoStream(fsOutput, descrypt.CreateDecryptor(), CryptoStreamMode.Write)
            Dim edata As Byte() = File.ReadAllBytes(inputFilePath)
    
            dec.Write(edata, 0, edata.Length)
    
            dec.Flush()
            dec.Close()
            k2.Reset()
    
    
        End Sub

    When it reaches the statement:

    dim fsOutput as New FileStream(outputfilepath,FileMode.open),

    I get the error: Could not find file 'D:\E\MusrasalatStorage\MinisterOffice\2018-1069-100A_dec.pdf'

    The error shown as attached:

    So Where exactly the error?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2018 7:53 PM
  • User475983607 posted

    The error is pretty clear, the file is not found.  Have you tried simply reading the error to retrieve the file name and verifying the file exists or that the path is correct and expected?  The part of the path that I can see looks suspicious.  Do you have a folder named E?  Maybe E was another drive letter?

    D:\E\MusrasalatStorage\MinisterOffice\2018-1069-100A_dec.pdf'

    Monday, October 15, 2018 8:07 PM
  • User-786564416 posted

    Mr. Mgebhard thanks for attempt to help but your guessing is totally wrong. Please try to understand before any intervention. The E is the folder name. And the path is correct but the file name supposed to be generated automatically by the Decrypt sub with the name passed in the outputfilePath parameter.

    So let me wait for other's help specially for the helpful user who Ackerly Xu  is thankfully trying to provide a geniune help.

    Monday, October 15, 2018 8:20 PM
  • User475983607 posted

    Mr. Mgebhard thanks for attempt to help but your guessing is totally wrong.

    I'm not guessing.... the file does not exist.  I gave you the benefit of the doubt on this one... but...

    Please try to understand before any intervention. The E is the folder name. And the path is correct but the file name supposed to be generated automatically by the Decrypt sub with the name passed in the outputfilePath parameter.

    As far as I can tell, your expectation does not match the code.  As written the Decrypt function expects the file to exist.  This line of code tries to open a file and read the contents as a stream.

    Dim fsOutput As New FileStream(outputfilePath, FileMode.Open)

    https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream?view=netframework-4.7.2

    Use this code to create a file if that's what you are trying to do.

    using (FileStream fs = new FileStream(path, FileMode.Create))

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2018 8:53 PM
  • User-786564416 posted

    Thanks you very much for your valuable help.

    What about the type of the file I want to create? Suppose that the file that I want to create is a pdf file, will it make it the same?

    Monday, October 15, 2018 9:00 PM
  • User475983607 posted

    What about the type of the file I want to create? Suppose that the file that I want to create is a pdf file, will it make it the same?

    I'm not sure what you're asking. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2018 10:11 PM
  • User-893317190 posted

    Hi alihusain_77,

    As mgebhard has said, you  could use  FileMode.Create if the file doesn't exist.

    As to pdf file, if you write in this way ,it could also decrypt  the file but you couldn't open the decrypted file.

    I suggest you could save the  decrypted file as text and convert it to pdf first.

    About how to convert a text file to pdf  in vb.net , you could refer to the link below 

    https://www.c-sharpcorner.com/forums/convert-text-to-pdf-using-vb-net

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 16, 2018 2:02 AM