本文介绍了避免" =="在加密字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code。我每次加密字符串,它给字符串到底==。

我怎样才能避免这种情况。

 专用功能加密(明文作为字符串)作为字符串
    昏暗EncryptionKey设置为String =MAKV2SPBNI99212
    昏暗clearBytes为字节()= Encoding.Uni code.GetBytes(明文)
    使用加密如AES = Aes.Create()
        昏暗PDB作为新Rfc2898DeriveBytes(EncryptionKey设置,新的字节(){&安培; H49,和放大器; H76,和放大器; H61,和放大器; H6E,&安培; H20,和放大器; H4D,_
         &安培; H65,和放大器; H64,和放大器; H76,和放大器; H65,和放大器; H64,和放大器; H65,_
         &安培; H76})
        encryptor.Key = pdb.GetBytes(32)
        encryptor.IV = pdb.GetBytes(16)
        使用MS作为新的MemoryStream()
            通过CS作为新的CryptoStream(MS,encryptor.CreateEncryptor(),CryptoStreamMode.Write)
                cs.Write(clearBytes,0,clearBytes.Length)
                cs.Close()
            使用完
            明码= Convert.ToBase64String(ms.ToArray())
        使用完
    使用完
    返回明文
结束功能


解决方案

这是。它只是有以指示输入字节数组的长度不是3的倍数,并且是必要的正确的串进行解码。这没有什么可担心的,这并不表示有什么不对您的加密算法。

如果你真的需要prevent,请确保你传递给 ToBase64String 字节数组的长度是3的倍数,可能通过浸轧阵列零(但你必须要能够剥夺那些零了,你去code后的字符串)。

Below is my code. Every time I encrypt the string, it is giving string as "==" in the end.

How can I avoid that.

  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
解决方案

That's a result of base-64 encoding. It's just there to indicate that the length of the input byte array was not a multiple of 3, and is necessary for properly decoding the string. It's nothing to worry about, and it doesn't indicate that there's anything wrong with your encryption algorithm.

If you really need to prevent it, make sure the byte array you pass to ToBase64String has a length that is a multiple of 3, possibly by padding the array with zeroes (but then you'd have to be able to strip those zeros off after you decode the string).

这篇关于避免" =="在加密字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:15