我有一个程序,我可以在其中编写和读取配置文件。当我写作时,我使用 vb.net 中的 RijndaelManaged 对象加密整个文件,当我阅读时,我使用相同的密钥和初始化向量值解密。
在我的开发机器和许多其他机器上一切正常。但是,某些 PC 无法使用相同的程序加密/解密文件。
这个加密对象中是否有任何东西可以阻止它,你会推荐使用什么?
谢谢
编辑:这是我用来加密和解密的代码:据我所知,如果我从我的主机加密字节,我可以从任何 PC 上解密它。但是,如果我从另一台 PC 加密字节,我无法从任何 PC 解密它。另外,当我查看文件的内容时,它们看起来根本不一样。请注意,我通常采用的方法是从文件(通常是 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