加密/在vb.net解密并不总是返回相同的值(Encrypting / Decrypting in

2019-10-17 10:15发布

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

这一切都正常工作我的机器上和其他许多人。 然而,一些PC不能加密/使用相同的程序解密文件。

有没有在这个加密对象,防止它和你会建议使用,而不是什么吗?

谢谢

编辑:这里是我使用的加密和解密的代码:从我所看到的,如果我从我的主机加密字节,我可以从任何PC decypt它。 但是,如果我从我的其他电脑进行加密字节,我无法从任何PC解密。 此外,当我看了看文件的内容,他们不看一样的。 需要注意的是通常的方式,我绕过去是我创建了文件中的MemoryStream(通常是XML)和我加密/解密。

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

Answer 1:

如果正在使用,而不是正常的.NET作为运行时单声道,然后PasswordDeriveBytes将任何输出失败高于20 PasswordDeriveBytes使用未知的,专有的,非确定性的,破碎,加密不安全方法当请求比散列输出多个输出可以在提供PasswordDeriveBytes 。 这已被标记为通过Mono开发没有修复。 。

做的最好的事情是升级到Rfc2898DeriveBytes ,它实现PBKDF2代替PBKDF1。 的PBKDF2定义如何扩展输出的,以便所有的实现应该表现为规定的数额。

如果您需要比散列函数则提供了更多的输出它却是更安全了PBKDF2的输出端使用KBKDF如香港民主促进会。 PKBDF2需要一组完整的轮创造更多的数据,这可能有利于攻击者而不是正常的用户将增加一倍,或根据多字节是如何要求的三倍CPU的负荷。

[编辑]

还要指出的是构造PasswordDeriveBytes ,需要一个密码字符串不指定字符编码将被使用,所以你最好把它转换成字节将其送入构造函数之前。

根据大多数的意见似乎都使用UTF-8虽然 - 提防其他运行时(如Java)在这方面可能会有所不同。



Answer 2:

胡乱猜测:你在写作和阅读文本模式的文件吗? 当加密数据恰好包括0x1A的(EOF,CTRL-Z)读数可能过早停止,你解密,以几个字节。



Answer 3:

你不能上传120GB文件的字节()。 或者你有一些疯狂的机器。 试着上传逐字节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()


文章来源: Encrypting / Decrypting in vb.net does not always return the same value