simple encrypting / decrypting in VB.Net

后端 未结 4 1137
执笔经年
执笔经年 2020-12-28 10:55

I\'m trying to figure out how to encrypt / decrypt a string in VB.Net.

I followed the example given here and wrote the following code (below). There\'s a text box,

4条回答
  •  抹茶落季
    2020-12-28 11:36

    Your encryption looks mostly correct, but I am not sure if the UTF8 encoding or other settings on the encryption object is throwing you off. Here is the heart of the encryption method that we use, tailored slightly to your code:

    ' Return the encrypted bytes from the memory stream.
    Dim aoBytes As Byte() = Nothing
    
    ' Declare the RijndaelManaged object used to encrypt the data.
    Using oEncryptor As New RijndaelManaged
        Try
            ' Initialize the encryptor with the specified key and initialization vector
            oEncryptor.Key = KEY_128
            oEncryptor.IV = IV_128
    
            ' Declare the streams used to encrypt to an in memory array of bytes.
            Using msEncrypt As New MemoryStream
                ' Create the streams used for encryption.
                Using csEncrypt As New CryptoStream(msEncrypt, oEncryptor.CreateEncryptor(), CryptoStreamMode.Write)
                    Using swEncrypt As New StreamWriter(csEncrypt)
                        ' Write all data to the stream.
                        swEncrypt.Write(Me.TextBox1.Text)
                    End Using
    
                    ' Retrieve the bytes
                    aoBytes = msEncrypt.ToArray()
                End Using
    
            End Using
        Finally
            ' Clear the RijndaelManaged object.
            If oEncryptor IsNot Nothing Then
                oEncryptor.Clear()
            End If
        End Try
    End Using
    
    If aoBytes IsNot Nothing Then
        Me.TextBox1.Text = System.Convert.ToBase64String(aoBytes)
    Else
        Me.TextBox1.Text = String.Empty
    End If
    

    And the decryption is:

    Dim sDecryptedValue As String = ""
    
    ' Declare the RijndaelManaged object used to encrypt the data.
    Using oDecryptor As New RijndaelManaged
        Try
            ' Initialize the encryptor with the specified key and a default initialization vector
            oDecryptor.Key = KEY_128
            oDecryptor.IV = IV_128
    
            Using msDecrypt As New MemoryStream(System.Convert.FromBase64String(Me.TextBox1.Text))
                ' Create the streams used for encryption.
                Using csDecrypt As New CryptoStream(msDecrypt, oDecryptor.CreateDecryptor(), CryptoStreamMode.Read)
                    Using srDecrypt As New StreamReader(csDecrypt)
                        ' Write all data to the stream.
                        sDecryptedValue = srDecrypt.ReadToEnd()
                    End Using
                End Using
            End Using
        Finally
            ' Clear the RijndaelManaged object.
            If oDecryptor IsNot Nothing Then
                oDecryptor.Clear()
            End If
        End Try
    End Using
    
    Me.TextBox1.Text = sDecryptedValue
    

    One minor difference is that we accept a string key and intializaton vector from the caller and clean them up as follows.

    InitializationVector cleanup:

    If sInitializationVector.Length > 16 Then
        ' Trim the IV if it is too long
        sInitializationVector = sInitializationVector.Substring(0, 16)
    ElseIf sInitializationVector.Length < 16 Then
        ' Pad the IV if it is too short
        sInitializationVector = sInitializationVector.PadRight(16)
    End If
    
    oDecryptor.IV = System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(sInitializationVector)
    

    Encryption key cleanup:

    oDecryptor.Key = GetLegalEncryptionKey(sKey, oDecryptor)
    
    Public Function GetLegalEncryptionKey(ByVal sKey As String, ByVal oEncryptor As RijndaelManaged) As Byte()
    
    Dim sTemp As String
    
    If oEncryptor.LegalKeySizes.Length > 0 Then
        Dim wSize As Integer
        ' key sizes are in bits
    
        With oEncryptor.LegalKeySizes(0)
            wSize = .MinSize
            Do While sKey.Length * 8 > wSize AndAlso .SkipSize > 0 AndAlso wSize < .MaxSize
                wSize += oEncryptor.LegalKeySizes(0).SkipSize
            Loop
        End With
        Dim wTotalChars As Integer
    
        wTotalChars = CInt(wSize / 8)
        If sKey.Length > wTotalChars Then
            sTemp = sKey.Substring(0, wTotalChars)
        Else
            sTemp = sKey.PadRight(wTotalChars, " "c)
        End If
    Else
        sTemp = sKey
    End If
    
    ' convert the secret key to byte array
    Return System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(sTemp)
    

    End Function

提交回复
热议问题