Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I use this code to encrypt/decrypt files. It works fine. Now I want to change it for not having to write each decrypted file to disk, I want the decryption directly to a string variable...

I try this adapted code but keep on getting empty strings...
What is wrong???
Thx for any help

VB
Function DecryptFileToString(ByVal strInputFile As String, ByVal PW As String) As String

       'Setup  streams to handle input and output.
       Dim fsInput As System.IO.FileStream
       fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, FileAccess.Read)
       Dim fsOutput As MemoryStream
       fsOutput = New MemoryStream()
       Dim reader As StreamReader = New StreamReader(fsOutput)

       'Declare variables for encrypt/decrypt process.
       Dim bytBuffer(4096) As Byte 'holds a block of bytes for processing
       Dim lngBytesProcessed As Long = 0 'running count of bytes processed
       Dim lngFileLength As Long = fsInput.Length 'the input file's length
       Dim intBytesInCurrentBlock As Integer 'current bytes being processed
       Dim csCryptoStream As CryptoStream = Nothing

       'Declare CryptoServiceProvider & generate key
       Dim CrType1 As New System.Security.Cryptography.RijndaelManaged
       CrType1.Padding = PaddingMode.None
       Dim bytKey1 As Byte()
       Dim bytIV1 As Byte()
       bytKey1 = CreateKey(PW)
       bytIV1 = CreateIV(PW)
       csCryptoStream = New CryptoStream(fsOutput, CrType1.CreateDecryptor(bytKey1, bytIV1), CryptoStreamMode.Write)

       'Use While to loop until all of the file is processed.
       While lngBytesProcessed < lngFileLength
           intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
           csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
           lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)
       End While

       'Close FileStreams and CryptoStream.
       MsgBox("'" & reader.ReadToEnd & "'") 'for testing
       Return reader.ReadToEnd
       csCryptoStream.Close()
       fsInput.Close()
       fsOutput.Close()
   End Function


What I have tried:

It works fine for file encryption & decryption
Posted

Don't put it to strings. Strings contain characters, not bytes, and there is a very good chance that the string representation of your data will be different to the original - which means that it won't decrypt!.
Instead, use a byte array and it should work. The easiest way is to replace your FileStream object with MemoryStream instead, and use GetBuffer or ToArray to access the bytes directly.
 
Share this answer
 
This does the job:
VB
Function DecryptFileToString(Type As String, ByVal strInputFile As String, strOutputFile As String, ByVal PW As String) As String
        Dim Result As String = ""

        'Setup streams to handle input and output.
        Dim fsInput As System.IO.FileStream
        fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, FileAccess.Read)

        'Declare variables for encrypt/decrypt process.
        Dim bytBuffer(4096) As Byte 'holds a block of bytes for processing
        Dim lngBytesProcessed As Long = 0 'running count of bytes processed
        Dim lngFileLength As Long = fsInput.Length 'the input file's length
        Dim intBytesInCurrentBlock As Integer 'current bytes being processed
        Dim csCryptoStream As CryptoStream = Nothing

        'Declare CryptoServiceProvider & generate key
        Dim CrType1 As New System.Security.Cryptography.RijndaelManaged
        Dim bytKey1 As Byte()
        Dim bytIV1 As Byte()
        bytKey1 = CreateKey(PW)
        bytIV1 = CreateIV(PW)

        Select Case Type
            Case "S"
                CrType1.Padding = PaddingMode.PKCS7 ' of ISO10126 of PKCS7 cf: https://msdn.microsoft.com/en-us/library/system.security.cryptography.paddingmode(v=vs.110).aspx
                Dim fsOutput As MemoryStream
                fsOutput = New MemoryStream()
                fsOutput.SetLength(0) 'make sure fsOutput is empty
                csCryptoStream = New CryptoStream(fsOutput, CrType1.CreateDecryptor(bytKey1, bytIV1), CryptoStreamMode.Write)
                'Use While to loop until all of the file is processed.
                While lngBytesProcessed < lngFileLength
                    intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
                    csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
                    lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)
                End While
                csCryptoStream.FlushFinalBlock()
                fsOutput.Close()
                Result = Encoding.UTF8.GetString(fsOutput.ToArray()) 'vereist imports text
            Case "F"
                Dim fsOutput As System.IO.FileStream
                fsOutput = New System.IO.FileStream(strOutputFile, FileMode.OpenOrCreate, FileAccess.Write)
                fsOutput.SetLength(0) 'make sure fsOutput is empty
                csCryptoStream = New CryptoStream(fsOutput, CrType1.CreateDecryptor(bytKey1, bytIV1), CryptoStreamMode.Write)
                'Use While to loop until all of the file is processed.
                While lngBytesProcessed < lngFileLength
                    intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
                    csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
                    lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)
                End While
                csCryptoStream.Close()
                fsOutput.Close()
                Result = File.ReadAllText(strOutputFile)
        End Select
        fsInput.Close()

        Return Result
    End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900