秘密信息加密/解密ÅÄÖ

时间:2015-02-08 17:01:31

标签: vb.net encryption message

我需要添加这个加密加密代码ÄÖÅ字符,但我不知道如何? 这是我的代码:

Public Function EncryptDecryptString(ByVal inputString As String, Optional ByVal decrypt As Boolean = False) As String
    Dim sourceChars As String = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    Dim resultChars As String = "36N8lkXruq94jMZInPpshR xHc2mTQb7eYai5vGWDzFdoC0wKSBt1EOgVALJfUy"

    Dim result As String
    If decrypt Then
        result = New String(inputString.Select(Function(c) sourceChars(resultChars.IndexOf(c))).ToArray())
    Else
        result = New String(inputString.Select(Function(c) resultChars(sourceChars.IndexOf(c))).ToArray())
    End If

    Return result
End Function

1 个答案:

答案 0 :(得分:0)

如果您只是将瑞典字符添加到两者 sourceChars和resultChars,就可以使用当前代码(以及英语字符)。

Dim sourceChars As String = " ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÅabcdefghijklmnopqrstuvwxyz0123456789äöå"
Dim resultChars As String = "äöå36N8lkXruq94jMZInPpshR xHc2mTQb7eYai5ÄÖÅvGWDzFdoC0wKSBt1EOgVALJfUy"

但是,如果输入字符串包含您不期望的任何字符(例如制表符或换行符),则代码将失败。这是一个函数版本,它不会在意外字符上引发异常,而只是在不加密该字符的情况下使用它(对于严重加密,最好是获得异常)。

Public Function EncryptDecryptString(ByVal inputString As String, Optional ByVal decrypt As Boolean = False) As String
    Dim sourceChars As String = " ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÅabcdefghijklmnopqrstuvwxyz0123456789äöå"
    Dim resultChars As String = "äöå36N8lkXruq94jMZInPpshR xHc2mTQb7eYai5ÄÖÅvGWDzFdoC0wKSBt1EOgVALJfUy"

    Dim result() As Char = inputString
    Dim inChars As String = If(decrypt, resultChars, sourceChars)
    Dim outChars As String = If(decrypt, sourceChars, resultChars)

    For i As Integer = 0 To inputString.Length - 1
        Dim pos As Integer = inChars.IndexOf(inputString(i))
        If pos >= 0 Then result(i) = outChars(pos)
    Next

    Return result
End Function