Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在vb.net中加密/解密并不总是返回相同的值_Vb.net_Encryption_Rijndaelmanaged - Fatal编程技术网

在vb.net中加密/解密并不总是返回相同的值

在vb.net中加密/解密并不总是返回相同的值,vb.net,encryption,rijndaelmanaged,Vb.net,Encryption,Rijndaelmanaged,我有一个写和读配置文件的程序。当我写入时,我使用vb.net中的RijndaelManaged对象加密整个文件,当我读取时,我使用密钥和初始向量的相同值进行解密 它在我的开发机器和其他许多机器上都工作得很好。但是,有些电脑无法使用相同的程序加密/解密文件 此加密对象中是否存在阻止它的内容,以及您建议使用什么替代 谢谢 编辑:这是我用来加密和解密的代码:从我所看到的,如果我从我的主机加密字节,我可以从任何PC解密。但是,如果我从我的另一台PC加密字节,我无法从任何PC解密。而且,当我查看文件的内容

我有一个写和读配置文件的程序。当我写入时,我使用vb.net中的RijndaelManaged对象加密整个文件,当我读取时,我使用密钥和初始向量的相同值进行解密

它在我的开发机器和其他许多机器上都工作得很好。但是,有些电脑无法使用相同的程序加密/解密文件

此加密对象中是否存在阻止它的内容,以及您建议使用什么替代

谢谢

编辑:这是我用来加密和解密的代码:从我所看到的,如果我从我的主机加密字节,我可以从任何PC解密。但是,如果我从我的另一台PC加密字节,我无法从任何PC解密。而且,当我查看文件的内容时,它们看起来根本不一样。注意,我通常使用的方法是从文件(通常是XML)创建一个memorystream,然后加密/解密

Public Shared Function EncryptBytes(ByVal strContenu() As Byte, ByVal initVectorBytes() As Byte, ByVal saltValueBytes() As Byte) As Byte()

    ' Convert our plaintext into a byte array.
    ' Let us assume that plaintext contains UTF8-encoded characters.
    Dim plainTextBytes As Byte() = strContenu
    'plainTextBytes = System.Text.Encoding.Unicode.GetBytes(strMessage)

    Dim strPassPhrase As String = "d%6&?76dhd8?532LDhds8!7?&?8&?dhcv77"

    Dim strHashAlgorithm As String = "SHA1"

    Dim intPswdIterations As Integer = 2

    ' First, we must create a password, from which the key will be derived.
    ' This password will be generated from the specified passphrase and 
    ' salt value. The password will be created using the specified hash 
    ' algorithm. Password creation can be done in several iterations.
    Dim password As PasswordDeriveBytes
    password = New PasswordDeriveBytes(strPassPhrase, _
                                       saltValueBytes, _
                                       strHashAlgorithm, _
                                       intPswdIterations)

    ' Use the password to generate pseudo-random bytes for the encryption
    ' key. Specify the size of the key in bytes (instead of bits).
    Dim keyBytes As Byte()
    keyBytes = password.GetBytes(32)

    ' Create uninitialized Rijndael encryption object.
    Dim symmetricKey As RijndaelManaged
    symmetricKey = New RijndaelManaged()

    ' It is reasonable to set encryption mode to Cipher Block Chaining
    ' (CBC). Use default options for other symmetric key parameters.
    symmetricKey.Mode = CipherMode.CBC

    ' Generate encryptor from the existing key bytes and initialization 
    ' vector. Key size will be defined based on the number of the key 
    ' bytes.
    Dim encryptor As ICryptoTransform
    encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)

    ' Define memory stream which will be used to hold encrypted data.
    Dim memoryStream As System.IO.MemoryStream
    memoryStream = New System.IO.MemoryStream()

    ' Define cryptographic stream (always use Write mode for encryption).
    Dim cryptoStream As CryptoStream
    cryptoStream = New CryptoStream(memoryStream, _
                                    encryptor, _
                                    CryptoStreamMode.Write)
    ' Start encrypting.
    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)

    ' Finish encrypting.
    cryptoStream.FlushFinalBlock()

    ' Convert our encrypted data from a memory stream into a byte array.
    Dim cipherTextBytes As Byte()
    cipherTextBytes = memoryStream.ToArray()

    ' Close both streams.
    memoryStream.Close()
    cryptoStream.Close()

    ' Convert encrypted data into a base64-encoded string.
    'Dim cipherText As String
    'cipherText = 
    Return cipherTextBytes

    ' Return encrypted string.
    'Return cipherText

End Function

Public Shared Function DecryptBytes(ByVal strContenuEncrypte() As Byte, ByVal initVectorBytes() As Byte, ByVal saltValueBytes() As Byte) As Byte()

    ' Convert strings defining encryption key characteristics into byte
    ' arrays. Let us assume that strings only contain ASCII codes.
    ' If strings include Unicode characters, use Unicode, UTF7, or UTF8
    ' encoding.

    ' Convert our ciphertext into a byte array.
    Dim cipherTextBytes As Byte() = strContenuEncrypte

    Dim strPassPhrase As String = "d%6&?76dhd8DSDhds8!7?&?8&?dhcv77"

    Dim strHashAlgorithm As String = "SHA1"

    Dim intPswdIterations As Integer = 2

    ' First, we must create a password, from which the key will be 
    ' derived. This password will be generated from the specified 
    ' passphrase and salt value. The password will be created using
    ' the specified hash algorithm. Password creation can be done in
    ' several iterations.
    Dim password As PasswordDeriveBytes
    password = New PasswordDeriveBytes(strPassPhrase, _
                                       saltValueBytes, _
                                       strHashAlgorithm, _
                                       intPswdIterations)

    ' Use the password to generate pseudo-random bytes for the encryption
    ' key. Specify the size of the key in bytes (instead of bits).
    Dim keyBytes As Byte()
    keyBytes = password.GetBytes(32)

    ' Create uninitialized Rijndael encryption object.
    Dim symmetricKey As RijndaelManaged
    symmetricKey = New RijndaelManaged()

    ' It is reasonable to set encryption mode to Cipher Block Chaining
    ' (CBC). Use default options for other symmetric key parameters.
    symmetricKey.Mode = CipherMode.CBC

    ' Generate decryptor from the existing key bytes and initialization 
    ' vector. Key size will be defined based on the number of the key 
    ' bytes.
    Dim decryptor As ICryptoTransform
    decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)

    ' Define memory stream which will be used to hold encrypted data.
    Dim memoryStream As System.IO.MemoryStream
    memoryStream = New System.IO.MemoryStream(cipherTextBytes)

    ' Define memory stream which will be used to hold encrypted data.
    Dim cryptoStream As CryptoStream
    cryptoStream = New CryptoStream(memoryStream, _
                                    decryptor, _
                                    CryptoStreamMode.Read)

    ' Since at this point we don't know what the size of decrypted data
    ' will be, allocate the buffer long enough to hold ciphertext;
    ' plaintext is never longer than ciphertext.
    Dim plainTextBytes As Byte()
    ReDim plainTextBytes(cipherTextBytes.Length)

    ' Start decrypting.
    Dim decryptedByteCount As Integer
    decryptedByteCount = cryptoStream.Read(plainTextBytes, _
                                           0, _
                                           plainTextBytes.Length)

    ' Close both streams.
    memoryStream.Close()
    cryptoStream.Close()

    ' Convert decrypted data into a string. 
    ' Let us assume that the original plaintext string was UTF8-encoded.
    Dim plainText As String
    plainText = System.Text.Encoding.Unicode.GetString(plainTextBytes, _
                                        0, _
                                        decryptedByteCount)

    ' Return decrypted string.
    Return plainTextBytes


End Function

胡乱猜测:你是在以文本模式写和读文件吗?当加密数据碰巧包含0x1A(EOF,ctrl-z)时,读取可能会过早停止,您将解密到几个字节。

猜测:您是否以文本模式写入和读取文件?当加密数据碰巧包含0x1A(EOF,ctrl-z)时,读取可能会提前停止,您将解密到几个字节。

如果您使用Mono而不是普通的.NET作为运行时,则对于任何高于20的输出,
PasswordDeriveBytes
将失败
PasswordDeriveBytes
当请求的输出超过
PasswordDeriveBytes
中的哈希输出所能提供的数量时,使用未知、专有、不确定、不可靠、加密不安全的方法

最好的办法是升级到
Rfc2898DeriveBytes
,它实现了PBKDF2而不是PBKDF1。PBKDF2定义了如何扩展输出量,以便所有实现都应按指定的方式运行

如果您需要比哈希函数提供的输出更多的输出,那么在PBKDF2的输出上使用诸如HKDF之类的KBKDF会更安全。PKBDF2需要另一整套轮次来创建更多数据,这可能有利于攻击者而不是普通用户,并将根据请求的字节数使CPU负载增加一倍或三倍

[编辑]

还要注意,接受密码字符串的
PasswordDeriveBytes
构造函数没有指定要使用的字符编码,因此最好在将其提供给构造函数之前将其转换为字节


根据大多数观察,这两个版本似乎都使用UTF-8,但要注意其他运行时(如Java)在这方面可能有所不同。

如果您使用Mono而不是普通的.NET作为运行时,则
PasswordDeriveBytes
对于任何高于20的输出都将失败
PasswordDeriveBytes
当请求的输出超过
PasswordDeriveBytes
中的哈希输出所能提供的数量时,使用未知、专有、不确定、不可靠、加密不安全的方法

最好的办法是升级到
Rfc2898DeriveBytes
,它实现了PBKDF2而不是PBKDF1。PBKDF2定义了如何扩展输出量,以便所有实现都应按指定的方式运行

如果您需要比哈希函数提供的输出更多的输出,那么在PBKDF2的输出上使用诸如HKDF之类的KBKDF会更安全。PKBDF2需要另一整套轮次来创建更多数据,这可能有利于攻击者而不是普通用户,并将根据请求的字节数使CPU负载增加一倍或三倍

[编辑]

还要注意,接受密码字符串的
PasswordDeriveBytes
构造函数没有指定要使用的字符编码,因此最好在将其提供给构造函数之前将其转换为字节


根据大多数观察,这两个版本似乎都使用UTF-8,但要注意其他运行时(如Java)在这方面可能有所不同。

您不能将120Gb的文件上载到byte()。或者你有一台疯狂的机器。 尝试在Ram中逐字节上传

Dim fStream As FileStream = New FileStream("Encrypted.Encrypted", FileMode.Create)
        Dim cryptoStream As New CryptoStream(fStream, Encryptor, CryptoStreamMode.Write)
        Using UploadFile As FileStream = New FileStream(OpenfileDialog.FileName, FileMode.Open)
            For i = 0 To UploadFile.Length - 1
                Dim NewByte As New Byte
                NewByte = UploadFile.ReadByte
                cryptoStream.WriteByte(NewByte)
            Next
        End Using

        cryptoStream.FlushFinalBlock()
        cryptoStream.Close()
        fStream.Close()

无法将120Gb的文件上载到byte()。或者你有一台疯狂的机器。 尝试在Ram中逐字节上传

Dim fStream As FileStream = New FileStream("Encrypted.Encrypted", FileMode.Create)
        Dim cryptoStream As New CryptoStream(fStream, Encryptor, CryptoStreamMode.Write)
        Using UploadFile As FileStream = New FileStream(OpenfileDialog.FileName, FileMode.Open)
            For i = 0 To UploadFile.Length - 1
                Dim NewByte As New Byte
                NewByte = UploadFile.ReadByte
                cryptoStream.WriteByte(NewByte)
            Next
        End Using

        cryptoStream.FlushFinalBlock()
        cryptoStream.Close()
        fStream.Close()

您可以发布加密算法的代码吗?您可以发布加密算法的代码吗?文件本身是一个XML文件,在加密到文件之前,我会将其写入内存流。这在不同的个人电脑之间有什么不同?你所说的“加密到文件”是什么意思?您可以加密数据或将数据保存到文件。所有计算机上的加密数据是否相同,或者是否使用不同的加密密钥。正如Moe所建议的:请发布一些相关代码。文件本身是一个XML文件,在加密到文件之前,我会将其写入内存流。这在不同的个人电脑之间有什么不同?你所说的“加密到文件”是什么意思?您可以加密数据或将数据保存到文件。所有计算机上的加密数据是否相同,或者是否使用不同的加密密钥。正如教育部建议的:请发布一些相关代码。