解密加密文本文件内容并将其复制到MemoryStream [英] Decrypting and Copying Contents of Encrypted Text File to MemoryStream

查看:39
本文介绍了解密加密文本文件内容并将其复制到MemoryStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文本文件的加密内容复制到内存流中,然后解密这些内容并将其复制到新的内存流中.当我到达发生复制的代码时,我在调试时收到无效数据错误.

I'm trying to copy the encrypted contents of a text file into a memory stream and then decrypt and copy those contents into a new memory stream. When I reach the code where the copy occurs I get a Invalid Data error on debug.

这是我得到的代码块:

Function DecryptFile(ByVal sInputFilename As String, ByVal sKey As String) As Byte()

    Dim DES As New DESCryptoServiceProvider()

    DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

    Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()

    Dim encryptedByteArray() As Byte
    encryptedByteArray = File.ReadAllBytes(sInputFilename)

    Dim encryptedMS As MemoryStream = New MemoryStream(encryptedByteArray)
    Dim cryptostreamDecr As New CryptoStream(encryptedMS, desdecrypt, CryptoStreamMode.Read)
    Dim decryptedMS As MemoryStream = New MemoryStream()

    cryptostreamDecr.CopyTo(decryptedMS) 'Error occurs here
    cryptostreamDecr.Close()

    Return decryptedMS.ToArray()
End Function

我正在关注散布在网络上的示例,根据我阅读的内容,此代码应该可以工作...

I'm following examples I've found scattered around the web and from what I've read, this code should work...

谁能向我解释一下我做错了什么?

May anyone explain to me what am I doing wrong?

推荐答案

这是一个使用 Key 加密/解密字符串和文件的示例,无需明确提供初始化向量(因此您无需存储和检索它解密加密数据).

Here's an example to encrypt/decrypt a string and a file using a Key without explicitly providing an Initialization Vector (so yo don't need to store and retrieve it to decrypt encrypted data).

我在这里使用的加密提供程序是 TripleDESCryptoServiceProvider.
如果您需要使用 DES 提供程序,则完全相同,您只需要将 TripleDESCryptoServiceProvider 更改为 DESCryptoServiceProvider.
但是,正如您在文档中所读到的,最好转到 AesCryptoServiceProvider,如果可能的话.

The Encryption provider I'm using here is TripleDESCryptoServiceProvider.
If you need to use a DES provider, it's exactly the same thing, you just need to change TripleDESCryptoServiceProvider to DESCryptoServiceProvider.
But, as you can read in the Docs, better move to the AesCryptoServiceProvider, if/when possible.

初始化向量 (IV) 是根据指定的密钥计算的,如果解密数据的密钥与用于加密数据的密钥相同,则它是相同的哈希值.

The Initialization Vector (IV) is calculated based on the Key specified and it's the same Hashed value if the Key to Decrypt the data is the same as the Key used to Encrypt it.

在这种情况下,您会失去一些安全性,但您不需要存储 Key 或 IV(如果 Key 是由负责保护 Key 的用户提供的).

In this case, you lose some security, but you don't need to store either the Key or the IV (if the Key is provided by a User, who's responsible for protecting the Key).

模式是保留其默认值:CipherMode.CBC.
填充模式为其默认值: PaddingMode.PKCS7.

The Mode is left to its default: CipherMode.CBC.
The Padding Mode to its default: PaddingMode.PKCS7.

加密和解密 Base64String 和来自 Base64String 的字符串:

Encrypt and decrypt a sting to and from a Base64String:

Dim enc3Des As New TripleDesEncryptor("MyFancyKey")

Dim inputString = "Some fancy string to be encoded to a Base64 string"
Dim encodedB64 = enc3Des.EncryptStringToBase64(inputString)
Dim decoded64 = enc3Des.DecryptBase64String(encoded64)

要加密文件,请提供源文件的路径,然后将加密方法返回的字节保存到目标文件:

To encrypt a file, provide the path to the Source file, then save the bytes returned by the Encryption method to a destination file:

Dim enc3Des As New TripleDesEncryptor("MyFancyKey")

Dim plainTextFilePath = [Source file Path]
Dim encryptedFilePath = [Encrypted file Path]

Dim encodedBytes = enc3Des.EncryptFile(plainTextFilePath)
File.WriteAllBytes(encryptedFilePath, encodedBytes)

您当然可以在需要时使用相同的密钥解密文件:

You can of course decrypt the File when required, using the same Key:

Dim encryptedFilePath = [Encrypted file Path]
Dim decryptedFilePath = [Decrypted file Path]

Dim enc3Des2 As New TripleDesEncryptor("MyFancyKey")

Dim decodedBytes = enc3Des2.DecryptFile(encryptedFilePath)
File.WriteAllBytes(decryptedFilePath, decodedBytes)

TripleDesEncryptor 辅助类:

Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Public NotInheritable Class TripleDesEncryptor

    Private tripleDesProvider As New TripleDESCryptoServiceProvider()

    Sub New(key As String)
        tripleDesProvider.Key = GetKeyHash(key, tripleDesProvider.LegalKeySizes(0).MaxSize \ 8)
        tripleDesProvider.IV = GetKeyHash(key, tripleDesProvider.LegalBlockSizes(0).MaxSize \ 8)
    End Sub

    Public Function EncryptStringToBase64(inputString As String) As String
        Dim dataBytes As Byte() = Encoding.Unicode.GetBytes(inputString)
        Return Convert.ToBase64String(Encrypt(dataBytes))
    End Function

    Public Function EncryptFile(fileName As String) As Byte()
        Dim dataBytes As Byte() = File.ReadAllBytes(fileName)
        Return Encrypt(dataBytes)
    End Function

    Private Function Encrypt(dataBytes As Byte()) As Byte()
        Using ms As New MemoryStream(),
            encStream As New CryptoStream(ms, tripleDesProvider.CreateEncryptor(), CryptoStreamMode.Write)
            encStream.Write(dataBytes, 0, dataBytes.Length)
            encStream.FlushFinalBlock()
            Return ms.ToArray()
        End Using
    End Function

    Public Function DecryptBase64String(base64String As String) As String
        Dim dataBytes As Byte() = Convert.FromBase64String(base64String)
        Return Encoding.Unicode.GetString(Decrypt(dataBytes))
    End Function

    Public Function DecryptFile(fileName As String) As Byte()
        Dim dataBytes As Byte() = File.ReadAllBytes(fileName)
        Return Decrypt(dataBytes)
    End Function

    Private Function Decrypt(encryptedData As Byte()) As Byte()
        Using ms As New MemoryStream(),
            decStream As New CryptoStream(ms, tripleDesProvider.CreateDecryptor(), CryptoStreamMode.Write)
            decStream.Write(encryptedData, 0, encryptedData.Length)
            decStream.FlushFinalBlock()
            Return ms.ToArray()
        End Using
    End Function

    Private Function GetKeyHash(key As String, length As Integer) As Byte()
        Using sha1 As New SHA1CryptoServiceProvider()
            Dim varHash As Byte() = New Byte(length - 1) {}
            Dim keyBytes As Byte() = Encoding.Unicode.GetBytes(key)
            Dim hash As Byte() = sha1.ComputeHash(keyBytes).Take(length).ToArray()
            Array.Copy(hash, 0, varHash, 0, hash.Length)
            hash = Nothing
            keyBytes = Nothing
            Return varHash
        End Using
    End Function
End Class

这篇关于解密加密文本文件内容并将其复制到MemoryStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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