线程安全 RijndaelManaged、ICryptoTransform.TransformFinalBlock?

发布于 2024-08-11 05:37:45 字数 1624 浏览 3 评论 0原文

假设我们有这样的代码在构造函数中运行:

    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

从多个线程调用这些函数是否是线程安全的?

Lets say we have this code that runs in the constructor:

    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()

and then 2 public functions:

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

Would it be threadsafe to call those from more than one thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙味迷妹 2024-08-18 05:37:45

根据文档,它不是线程安全的。您将在 RijndaelManagedTransform 实例code>mDecryptor 和 mEncryptor 变量。 TransformFinalBlock 是一个实例方法,根据 MSDN 不是线程安全的:

任何公共静态(在视觉中共享
基本)该类型的成员是线程
安全的。任何实例成员都不是
保证线程安全。

如果您想保证拥有线程安全的代码,您可能需要同步对 TransformFinalBlock 方法的调用。

According to the documentation it is not thread safe. You will have RijndaelManagedTransform instances in the mDecryptor and mEncryptor variables. TransformFinalBlock is an instance method which according to MSDN is not thread safe:

Any public static (Shared in Visual
Basic) members of this type are thread
safe. Any instance members are not
guaranteed to be thread safe.

If you want to be guaranteed to have a thread safe code you might need to synchronize the call to TransformFinalBlock method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文