Danish Habib

Danish Habib

  • NA
  • 694
  • 235.1k

Encryption Decryption Errror

Jan 20 2015 5:41 AM
Dear All  I want to encrypt and decrypt a password while creating the user account, below is the  code which  i am using for  encoding and decoding  and while encoding mean creating the users my password saved into the encrypted form but while  getting the values it gives an error on base64Decode the error is "nvalid length for a base-64 char array or string in base64 decode" below are my two methods and i am saving password "pak1234" in my password column 

 Private Function base64Encode(ByVal sData As String) As String
        Try
            Dim encData_byte As Byte() = New Byte(sData.Length - 1) {}
            encData_byte = System.Text.Encoding.UTF8.GetBytes(sData)
            Dim encodedData As String = Convert.ToBase64String(encData_byte)
            Return (encodedData)
        Catch ex As Exception
            Throw (New Exception("Error in base64Encode" & ex.Message))
        End Try
    End Function
    Public Function base64Decode(ByVal sData As String) As String
        Dim encoder As New System.Text.UTF8Encoding()
        Dim utf8Decode As System.Text.Decoder = encoder.GetDecoder()
        Dim todecode_byte As Byte() = Convert.FromBase64String(sData)
        Dim charCount As Integer = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length)
        Dim decoded_char As Char() = New Char(charCount - 1) {}
        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0)
        Dim result As String = New [String](decoded_char)
        Return result
    End Function



Answers (3)