输入字符串在vb.net中的格式不正确

时间:2012-12-15 06:19:20

标签: vb.net

我有问题,当我试图解密消息时,它会提示我该错误。下面是我的代码..

Private Sub btndecrypt_Click(sender As Object, e As EventArgs) Handles btndecrypt.Click
    Dim key As Integer = Integer.Parse(txtkey.Text) << THIS CODE GIVE ERROR**'input string was not in the correct format**

    decrypt(key)
    Txtmessage.Text = totaldecry

End Sub

Private Sub decrypt(ByVal key As Integer)
    Dim length As Integer

    length = Len(receivemsg)
    ReDim y(length)
    Dim chararray() As Char = receivemsg.ToCharArray
    For a As Integer = 0 To length - 1
        y(a) = Asc(chararray(a)) - key
    Next

    For a As Integer = 0 To length - 1
        totaldecry = totaldecry & Chr(y(a)).ToString 
    Next


End Sub

3 个答案:

答案 0 :(得分:0)

简单:文本框中不包含可以转换为整数值的文本。如果我在你的文本框中输入我的名字,你怎么期望得到一个整数?你需要能够处理糟糕的输入。捕获异常或使用TryParse()而不是Parse()函数。你想如何处理失败取决于你。

虽然我们在这里,但我希望你没有用“解密”代码做任何真实的事情......根本没有真正的安全性。

但如果必须的话,我会写下这样的解密函数:

Private Function decrypt(ByVal key As Integer, ByVal message As String) As String
    Return New String(message.Select(Function(c) Chr(Asc(c)-key)).ToArray())  
End Function

只是一个班轮。这就是加密的“安全”。你可以使用这样的函数:

Private Sub btndecrypt_Click(sender As Object, e As EventArgs) Handles btndecrypt.Click
    Try
        Txtmessage.Text = decrypt(Integer.Parse(txtkey.Text), receivemsg)
    Catch
        'do something here to let user know it's a bad key
    End Try
End Sub

另请注意,我更改了函数定义,以减少对全局和类级变量的依赖,以在函数之间传递信息。这只是更好的风格。

答案 1 :(得分:0)

这是一个可以检查的链接,希望它能让您对自己的任务有所了解:)Encryption and Decryption of Password using VB.NET

答案 2 :(得分:0)

Integer.Parse()函数直接转换该值。如果参数不是整数,则不测试实际值并引发错误。

所以你可以使用integer.TryParse如下。 如果是Integer.TryParse(txtkey.Text,key)那么     '成功转换了整数,并将值存储在key中。 其他    'txtkey.Text没有任何有效的整数。所以0存储在密钥中。 结束如果

最好创建一个具有所有数据类型转换函数的类,并在需要时从类中调用该函数。我在我的代码

中创建了以下类

--------------------------我的代码中的类----------------- -------------------

Public Class NullValue
    '''''''''''''''''' 1. BYTE type convertion
    Public Shared Function getByte(ByVal mValue As String) As Byte
        Dim mNumber As Byte
        If Byte.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
    End Function

    '''''''''''''''''' 2. Int16/Short Integer type convertion
    Public Shared Function getInt16(ByVal mValue As String) As Int16
        Dim mNumber As Int16
        If Int16.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
    End Function

    '''''''''''''''''' 3. Int32/Integer type convertion
    Public Shared Function getInteger(ByVal mValue As String) As Int32
        Dim mNumber As Int32
        If Int32.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
    End Function

    '''''''''''''''''' 4. Int64/Long Integer type convertion
    Public Shared Function getInt64(ByVal mValue As String) As Int64
        Dim mNumber As Int64
        If Int64.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
    End Function

    '''''''''''''''''' 5. Single type convertion
    Public Shared Function getSingle(ByVal mValue As String) As Single
        Dim mNumber As Single
        If Single.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
    End Function

    '''''''''''''''''' 6. Double type convertion
    Public Shared Function getDouble(ByVal mValue As String) As Double
        Dim mNumber As Double
        If Double.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
    End Function


    '''''''''''''''''' 8. Integer(Int32 type convertion
    Public Shared Function getInt32(ByVal mValue As String) As Integer
        Dim mNumber As Int32
        If Int32.TryParse(mValue, mNumber) Then Return mNumber Else Return 0
    End Function

    '''''''''''''''''' 9. Short Date type convertion to String '''in Regional format
    Public Shared Function getDateString(ByVal mValue As String) As String
        Dim mDate As Date, strDate As String = ""
        If Date.TryParse(mValue, mDate) Then
            strDate = Format(mDate, "dd/MM/yyyy")
        Else
            strDate = ""
        End If
        Return strDate
    End Function

    '''''''''''''''''' 10. Short Date type convertion
    Public Shared Function getDate(ByVal mValue As String) As Date
        Dim mDate As Date
        Try
            Date.TryParse(mValue, mDate)
        Catch ex As Exception
            mDate = Date.MinValue
        End Try
        Return mDate
    End Function

    '''''''''''''''''' 11. Short Date type convertion
    Public Shared Function getDateTime(ByVal mValue As String) As DateTime
        Dim mDate As DateTime
        Try
            Date.TryParse(mValue, mDate)
        Catch ex As Exception
            mDate = Date.MinValue
        End Try
        Return mDate
    End Function

    '''''''''''''''''' 12. Short Date type convertion
    Public Shared Function getBoolean(ByVal mValue As String) As Boolean
        Dim mBool As Boolean
        If Boolean.TryParse(mValue, mBool) Then Return mBool Else Return False
    End Function

    '''''''''''''''''' 11. Time Convertion
    Public Shared Function getTime(ByVal mValue As String) As String
        Dim mDate As DateTime, mTime As String = ""
        Try
            mDate = Convert.ToDateTime(mValue)
            If mDate.Year > 1950 Then mTime = mDate.ToShortTimeString
        Catch ex As Exception
        End Try
        Return mTime
    End Function
End Class