在加密字符串中避免使用“==”

时间:2014-04-21 18:23:26

标签: asp.net vb.net encryption

以下是我的代码。每次我加密字符串时,它最后会给出字符串“==”。

我该如何避免这种情况。

  Private Function Encrypt(clearText As String) As String
    Dim EncryptionKey As String = "MAKV2SPBNI99212"
    Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText)
    Using encryptor As Aes = Aes.Create()
        Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _
         &H65, &H64, &H76, &H65, &H64, &H65, _
         &H76})
        encryptor.Key = pdb.GetBytes(32)
        encryptor.IV = pdb.GetBytes(16)
        Using ms As New MemoryStream()
            Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
                cs.Write(clearBytes, 0, clearBytes.Length)
                cs.Close()
            End Using
            clearText = Convert.ToBase64String(ms.ToArray())
        End Using
    End Using
    Return clearText
End Function

1 个答案:

答案 0 :(得分:1)

这是base-64 encoding的结果。它只是指示输入字节数组的长度不是3的倍数,并且是正确解码字符串所必需的。没什么可担心的,并不表示加密算法有任何问题。

如果你真的需要阻止它,请确保传递给ToBase64String的字节数组的长度是3的倍数,可能是用零填充数组(但是你必须是能够在解码字符串后删除这些零。)