2012-12-15 50 views
-1

餘米具有提示我,當我試圖解密message..below的錯誤問題是我的代碼..輸入字符串的不正確的格式在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 
+1

你在做什麼不是加密。充其量只是一個密碼。查看System.Security.Cryptography命名空間以獲得真正的加密。 –

+0

除了喬爾所說的,究竟是什麼原因導致「提示我那個錯誤」?你已經發布了代碼 - 不要讓我們猜測問題出在哪裏。 –

+0

你試圖解密一個字符串,並試圖解析爲'整數',但你有一些字符串'​​不能接受'整數'這就是爲什麼錯誤出現:) – spajce

回答

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 

還要注意我改變你的函數定義,以減少對全球和類級別的變量的依賴傳遞功能中的信息。這只是更好的風格。

0

Integer.Parse()函數直接轉換的價值有所瞭解。如果參數不是整數,它不會測試實際值並引發錯誤。

所以你可以使用integer.TryParse如下。如果Integer.TryParse(txtkey.Text,key) ' '成功轉換整數並且該值存儲在密鑰中。 else 'txtkey.Text沒有任何有效的整數。所以0被存儲在密鑰中。 end if

最好創建一個具有所有數據類型轉換函數的類,並在需要時從類中調用該函數。我創建了以下課程,供我在代碼中使用

-------------------------- class in my code ------ ------------------------------

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