【问题标题】:Cross-platform encryption/decryption - Handling Keys and Initialization Vectors (IV)跨平台加密/解密 - 处理密钥和初始化向量(IV)
【发布时间】:2011-03-28 23:28:48
【问题描述】:

在查看了一些示例之后,我总结了一些使用Rfc2898DeriveBytes 来获取密钥和初始化向量的加密/解密方法。我担心的是接收我的加密内容的一方必须能够解密它。由于我无法控制他们使用的语言(可能是 Java、PHP、C 等),我如何确保他们能够像使用 Rfc2898DeriveBytes 一样导出密钥和初始化向量 (IV) .NET 中的类?以下是我正在使用的加密和解密方法。

Public Shared Function EncryptText(ByVal plainText As String, ByVal password As String) As String

  Dim aesCrypto As Rijndael = Nothing
  Dim plainTextBytes As Byte()
  plainTextBytes = Encoding.Default.GetBytes(plainText)

  Dim rfc2898 As Rfc2898DeriveBytes
  rfc2898 = New Rfc2898DeriveBytes(password, GenerateSalt(password))
  aesCrypto = Rijndael.Create()
  aesCrypto.Padding = PaddingMode.ISO10126
  Dim tx As ICryptoTransform
  tx = aesCrypto.CreateEncryptor(rfc2898.GetBytes(32), rfc2898.GetBytes(16))
  Dim encryptedBytes As Byte()
  encryptedBytes = tx.TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length)
  Return Convert.ToBase64String(encryptedBytes)

End Function

Public Shared Function DecryptText(ByVal encryptedText As String, ByVal password As String) As String

  Dim aesCrypto As Rijndael = Nothing
  Dim encryptedTextBytes As Byte()
  encryptedTextBytes = Convert.FromBase64String(encryptedText)

  Dim rfc2898 As Rfc2898DeriveBytes
  rfc2898 = New Rfc2898DeriveBytes(password, GenerateSalt(password))
  aesCrypto = Rijndael.Create()
  aesCrypto.Padding = PaddingMode.ISO10126
  Dim tx As ICryptoTransform
  tx = aesCrypto.CreateEncryptor(rfc2898.GetBytes(32), rfc2898.GetBytes(16))
  Dim decryptedBytes As Byte()
  decryptedBytes = tx.TransformFinalBlock(encryptedTextBytes, 0, encryptedTextBytes.Length)
  Return Encoding.Default.GetString(decryptedBytes)

End Function

【问题讨论】:

    标签: .net cross-platform encryption


    【解决方案1】:

    您会告诉收件人实施 PBKDF2,这是在 RFC2898PKCS #5 中定义的标准。 Microsoft's documentation 表示他们的函数使用 HMAC-SHA-1 作为伪随机函数,并使用 1000 作为默认迭代次数。这是他们需要的信息。

    但是,您还需要在发送方传输使用GenerateSalt() 创建的盐。收件人不能只是自己打电话给GenerateSalt() - 它应该为每条消息随机生成。

    【讨论】:

    • 谢谢!这就是我需要知道的。我会将“盐”连同加密文本一起发送。
    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多