Допустим, у нас есть этот код, который выполняется в конструкторе:
Dim initVectorBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(initVector)
Dim saltValueBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(saltValue)
Dim passPharse As String = GenerateKeyString(EncryptionKey)
Dim password As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(passPharse, saltValueBytes, passwordIterations)
Dim keyBytes As Byte() = password.GetBytes(CInt(keySize / 8))
mSymmetricKey = New RijndaelManaged()
mSymmetricKey.Padding = PaddingMode.PKCS7
mSymmetricKey.Mode = CipherMode.CBC
mSymmetricKey.BlockSize = 128
mSymmetricKey.Key = keyBytes
mSymmetricKey.IV = initVectorBytes
mDecryptor = mSymmetricKey.CreateDecryptor()
mEncryptor = mSymmetricKey.CreateEncryptor()
, а затем 2 общедоступных функции:
Public Function Encrypt(ByVal plainText As String) As String
Dim plainTextBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(plainText)
Dim cipherTextBytes As Byte() = mEncryptor.TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length)
Dim cipherText As String = Convert.ToBase64String(cipherTextBytes)
Return cipherText
End Function
Public Function Decrypt(ByVal cipherText As String) As String
Dim cipherTextBytes As Byte() = Convert.FromBase64String(cipherText)
Dim plainTextBytes As Byte() = mDecryptor.TransformFinalBlock(cipherTextBytes, 0, cipherTextBytes.Length)
Dim plainText As String = System.Text.Encoding.UTF8.GetString(plainTextBytes, 0, plainTextBytes.Length)
Return plainText
End Function
Будет ли безопасным вызывать потоки из нескольких потоков?