加密程序字符串错误

时间:2013-06-28 01:03:20

标签: vb.net string encryption

所以,我正在制作一个简单的加密程序,这就是算法:

  1. 例:字符:一个;键:10000
  2. 从CONTROL编号1000 - 97 = 9903
  3. 中减去ASCII编号
  4. 将结果转换为十六进制,然后转换为字符串9903 = 26AF
  5. 将十六进制结果拆分为两位数的组26,AF
  6. 将分割十六进制值转换为ASCII 26 = 38(&)AF = 175(¯)您的结果将是:&¯
  7. 问题在于,有时(下面的代码),取决于加密密钥,加密结果全部磨损,解密功能不起作用。我已经完成了测试,我知道问题在于加密功能,但我不知道在哪里。这是代码:

    Option Strict On
    
    Imports System.Numerics
    
    Public Class MainF
    
        'tick for random keys
        Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))   
    
        Private Function AsciiToChars(ByVal nums As IEnumerable(Of Integer)) As Char()
            'converts ASCII value to char
            Return (From c In nums Select Chr(c)).ToArray
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            'set random key
            KeyE.Text = CStr(GetRandomNumber(10000, 99999))
            KeyD.Text = KeyE.Text
        End Sub
    
        Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
    
            'generate a randomic number, for the random key
            Return objRandom.Next(Low, High + 1)
        End Function
    
        Private Sub GenKey_Click(sender As Object, e As EventArgs) Handles GenKey.Click
    
            'renew key
            KeyE.Text = CStr(GetRandomNumber(10000, 99999))
            Dim tmptxt As String
            tmptxt = En.Text
            En.Text = ""
            En.Text = tmptxt
            KeyD.Text = KeyE.Text
            tmptxt = String.Empty
        End Sub
    
        Sub En_TextChanged(sender As Object, e As EventArgs) Handles En.TextChanged
    
            'encrypt
            If (String.IsNullOrEmpty(En.Text)) Then
                Enres.Text = ""
            Else
                Dim key As Integer = CInt(KeyE.Text)
                Dim chars() As Char = En.Text.ToCharArray
                Dim ints(chars.Length) As Integer
                Dim hex(chars.Length) As String
                Dim fex As String = ""
                Dim Mlist As New List(Of String)
    
                For Loop0 As Integer = 0 To chars.Length - 1
                    ints(Loop0) = key - Asc(chars(Loop0))
                    hex(Loop0) = Conversion.Hex(ints(Loop0))
                    fex &= hex(Loop0)
                Next
    
                If fex.Length Mod 2 <> 0 Then 'Mod returns the remainder of a division calculation. It will be 0 if the number is even.
                    fex = "0" & fex 'This will change "6AF" to "06AF"
                End If
    
                For x As Integer = 0 To fex.Length - 1 Step 2
                    Mlist.Add(fex.Substring(x, 2))
                Next
    
                Dim fdec(CInt((fex.Length - 2) / 2)) As Integer
                Dim fstr As String
    
                For y As Integer = 0 To fdec.Length - 1
                    fdec(y) = CInt(Val("&H" & Mlist(y)))
                Next
    
                fstr = AsciiToChars(fdec)
                Enres.Text = fstr
            End If
        End Sub
    
        Private Sub De_TextChanged(sender As Object, e As EventArgs) Handles De.TextChanged
    
            'decrypt
            If (String.IsNullOrEmpty(De.Text)) Then
                DeRes.Text = ""
            Else
                Dim final As String = ""
                Dim key As Integer
                key = CInt(KeyD.Text)
                Dim FSTR As String = De.Text
                Dim chars() As Char = FSTR.ToCharArray
                Dim hexsub(chars.Length) As String
                Dim ints(chars.Length) As String
                Dim finalhex As String
    
                For loop1 As Integer = 0 To chars.Length - 1
                    ints(loop1) = CStr(Asc(chars(loop1)))
                    hexsub(loop1) = Hex(ints(loop1))
                Next          
    
                finalhex = Join(hexsub, String.Empty)
    
                If finalhex.Length Mod 4 = 0 Then
                    Dim newlist As New List(Of String)
    
                    For x As Integer = 0 To finalhex.Length - 1 Step 4
                        newlist.Add(finalhex.Substring(x, 4))
                    Next
    
                    Dim sourceNUM(newlist.Count) As Int32
                    Dim finalascii(newlist.Count) As Int32
                    Dim finalchar(newlist.Count) As Char
                    key = CInt(KeyD.Text)
    
                    For b As Int32 = 0 To newlist.Count - 1
                        sourceNUM(b) = Convert.ToInt32(newlist(b), 16)
                        finalascii(b) = key - sourceNUM(b)
    
                        If finalascii(b) >= 32 And finalascii(b) <= 255 Then
                            finalchar(b) = Chr(finalascii(b))
                            final &= finalchar(b)
                        Else : final = "Invalid Input"
                        End If
                    Next
    
                    DeRes.Text = final
                Else
                    DeRes.Text = ""
                End If
            End If
        End Sub
    
        Private Sub KeyE_TextChanged(sender As Object, e As KeyEventArgs) Handles KeyE.KeyDown
    
            'future idea, enter key will run encription sub
            If e.KeyCode = Keys.Enter Then
                e.SuppressKeyPress = True
            End If
        End Sub
    End Class
    

1 个答案:

答案 0 :(得分:1)

  

问题是有时(下面的代码),取决于加密   密钥,加密结果全部磨损和解密   功能不会起作用。

我可以找到的主要问题是,有时您将不可显示的代码传递给加密字符串,然后将其视为空,这反过来会使解密算法失败

您可能需要弄清楚哪些键会给出错误值并过滤掉这些键或使用字节数组而不是字符串