将加密的文本文件读入字符串而不将其写入文件 [英] Read encrypted textfile into string without writing it to a file

查看:70
本文介绍了将加密的文本文件读入字符串而不将其写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我使用此代码加密/解密文件。它工作正常。现在我想更改它,因为不必将每个解密文件写入磁盘,我希望解密直接到字符串变量...



我尝试这个改编的代码但继续得空字符串...

有什么问题???

Thx任何帮助



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

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

推荐答案

不要把它放到字符串中。字符串包含字符,而不是字节,并且很有可能数据的字符串表示形式与原始字符串不同 - 这意味着它不会被解密!

相反,使用字节数组,它应该工作。最简单的方法是用MemoryStream替换你的FileStream对象,并使用GetBuffer或ToArray直接访问字节。
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.


这样做的工作:

This does the job:
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


这篇关于将加密的文本文件读入字符串而不将其写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆