从Crypto Service Provider获取字节或图像

时间:2014-08-11 03:00:07

标签: vb.net encryption

我在我的应用程序中使用此代码加密和解密数据 但代码使用文件流来完成所有工作这是代码:

    Dim bytKey As Byte()
    Dim bytIV As Byte()
    bytKey = CreateKey("mrbrashad1999")
    bytIV = CreateIV("mrbrashad1999")

    Try 'In case of errors.

        'Setup file streams to handle input and output.
        fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, _
                                           FileAccess.Read)
        fsOutput = New System.IO.FileStream(strOutputFile, FileMode.OpenOrCreate, _
                                            FileAccess.Write)
        fsOutput.SetLength(0) 'make sure fsOutput is empty

        '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
        'Declare your CryptoServiceProvider.
        Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged
        'Setup Progress Bar
        'pbStatus.Value = 0
        'pbStatus.Maximum = 100

        'Determine if ecryption or decryption and setup CryptoStream.
        Select Case Direction
            Case CryptoAction.ActionEncrypt
                csCryptoStream = New CryptoStream(fsOutput, cspRijndael.CreateEncryptor(bytKey, bytIV), CryptoStreamMode.Write)
            Case CryptoAction.ActionDecrypt
                csCryptoStream = New CryptoStream(fsOutput, cspRijndael.CreateDecryptor(bytKey, bytIV), CryptoStreamMode.Write)
        End Select

        'Use While to loop until all of the file is processed.
        While lngBytesProcessed < lngFileLength
            'Read file with the input filestream.
            intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
            'Write output file with the cryptostream.
            csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
            'Update lngBytesProcessed
            lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)
            'Update Progress Bar
            'pbStatus.Value = CInt((lngBytesProcessed / lngFileLength) * 100)
        End While

        'Close FileStreams and CryptoStream.
        csCryptoStream.Close()
        fsInput.Close()
        fsOutput.Close()

        'Catch file not found error.
    Catch When Err.Number = 53 'if file not found
        MsgBox("Please check to make sure the path and filename" + _
                "are correct and if the file exists.", _
                 MsgBoxStyle.Exclamation, "Invalid Path or Filename")

        'Catch all other errors. And delete partial files.
    Catch
        fsInput.Close()
        fsOutput.Close()


    End Try

我应该做些什么来获取byte()或image从我的文件系统中的加密数据直接到我的应用程序变量。

1 个答案:

答案 0 :(得分:0)

CryptoStream只是位于另一个Stream之上。当您WriteCryptoStream时,它会传入您传入的Byte数组,对其进行加密并使用结果填充另一个数组,然后它将Write传递给基础Stream。同样,当您致电Read时,它会在基础Read上调用Stream,对其进行解密,然后将结果传递给您。在您的情况下,您使用的是FileStream,但它可以是任何类型的Stream,例如NetworkStreamGZipStream。如果您想要一个临时的地方在您的应用程序中存储二进制数据,那么您可以使用MemoryStream。无论其类型如何,使用Stream的代码都是相同的。