我发现这个代码是如何工作的?暗号

时间:2016-04-25 21:06:00

标签: vb.net

所以我找到了这个很酷的密码,我正在为学校制作一个项目(有点像,向我们展示你所学到的一切项目),并允许他们在网上重视其他人的代码(只要它不是一个完整的复制和粘贴)。我想了解它是如何工作的,并制作我自己的版本(没有复制和粘贴),我已经为我理解的部分添加了注释,并且我没有对这些部分的问号进行评论。

    Function EncryptDecrypt(ByVal text1 As String, ByVal key As String, ByVal isEncrypt As Boolean) As String //yea got this
        Dim char1 As String //Defining char one
        Dim char2 As String //Defining char two
        Dim cKey As Byte //Defining a key as a byte
        Dim strLength As Integer //Defining strLength as an integer
        Dim Result As String = "" //Defining Result as String equal to nothing
        Dim j As Integer = -1 //Defining j as an integer equal to -1
        If text1 <> "" And IsNumeric(key) Then //if text1 is not nothing and the key is numeric then...
            strLength = text1.Length //making strLength equal to the length of the text.
            For i As Integer = 0 To strLength - 1 //Do until strLenth is less than 1 ???
                char1 = text1.Substring(i, 1) //Char one is equal to 
                If j < key.Length - 1 Then //if j (-1) is less than the key's length - 1 then...
                    j = j + 1 //add one to j
                Else //no explanation needed
                    j = 0 //no explanation needed
                End If //no explanation needed
                cKey = Val(key.Substring(j, 1)) //?? cKey is equal to value of the current character it is looking at (j, 1)??
                If isEncrypt Then //if were encypting it
                   If (Asc(char1) + cKey) > 255 Then //????
                        char2 = Chr(Asc(char1) + cKey - 255) //????
                   Else //no explanation needed
                        char2 = Chr(Asc(char1) + cKey) //??
                   End If //no explanation needed
                Else //no explanation needed
                    If (Asc(char1) - cKey) < 1 Then //????
                        char2 = Chr(Asc(char1) - cKey + 255) //?????
                    Else //no explanation needed
                        char2 = Chr(Asc(char1) - cKey) //?????
                    End If //no explanation needed
                End If //no explanation needed
                Result &= char2 //?????
            Next //no explanation needed
        Else //no explanation needed
            MsgBox("Enter text or key!") //no explanation needed
        End If //no explanation needed
        Return Result //no explanation needed
    End Function //no explanation needed


No explanation needed for these either VVVV
    Private Sub btCrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCrypt.Click
        txtResult.Text = EncryptDecrypt(txtText.Text, txtKey.Text, True)

    End Sub

    Private Sub btDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDecrypt.Click
        txtResult.Text = EncryptDecrypt(txtText.Text, txtKey.Text, False)
    End Sub
End Class

我有点明白,每个“字母”都有一个“Asc”值,但我不完全确定它是如何工作的。任何人都可以提供帮助,对我来说真的很棒,乐于助人!

2 个答案:

答案 0 :(得分:1)

循环输入逐个字符和键(所有数字,一次一个数字)。每个字符都转换为ASCII character code,并在其中添加或减去关键数字。

当密钥用完时,它会循环回到密钥的开头并再次通过。

如果添加/减去获取ASCII字符代码表(&lt; 0或&gt; 255)之外的字符,则它会绕到表的另一侧。

逐个字符地构建一串输出。

答案 1 :(得分:1)

可以在MSDN网站上找到很好的信息来源。以下是您可能会遇到的一些地方。我已经提到了相关文件,所以如果你仍然不明白,你可以离开看看

 For i As Integer = 0 To strLength - 1 

直到strLenth等于0,每次递减1

for loops msdn

  

cKey = Val(key.Substring(j,1))// ?? cKey等于的值   它正在查看当前字符(j,1)??

是的,将为cKey分配密钥的最后一个字符。如果他们的密钥是854824那么cKey现在是#4;&#39;。

  

If(Asc(char1)+ cKey)&gt; 255然后// ????

     

char2 = Chr(Asc(char1)+ cKey - 255)// ????

     

否则//无需解释

     

char2 = Chr(Asc(char1)+ cKey)// ??

     

结束如果//无需解释

这个公式用于谴责。这里要知道的是,Asc函数将返回字符的Asci值(table here)

Single-byte Character Sets的Asci字符不能大于255或小于0,所以如果它更大,则它们-255 + cKey以找到原始的Ascii值。

  

If(Asc(char1) - cKey)&lt; 1然后// ????

     

char2 = Chr(Asc(char1) - cKey + 255)// ?????

     

否则//无需解释

     

char2 = Chr(Asc(char1) - cKey)// ?????

     

结束如果//无需解释

与上面相同,但反过来说,如果char1 - x的ASCII值(其中x = cKey中最后一位数字的数值)大于此值,则此代码将通过将255递增到char值来加密字符值。 0.

  

结果&amp; = char2 // ?????

最后这会将字符串连接到Result每个循环。 这是写作的简写

  

结果=结果+ Char2

您可能会理解它在调试中更多地逐步执行程序并在您查看变量的值时查看它们。

祝你好运,

希望这会有所帮助