KiwiNinja - 1 year ago 90
Vb.net Question

# How does this code I found work? Cipher

So I found this cool cipher, im making a project for school (its kinda like, show us everything you learned kinda of project) and were allowed to get heavy look at other peoples code online (as long is its not a complete copy and paste). I want to understand how it works and make my own version (without copy and pasting), ive put comments for the parts I understand, and question marks for the parts I dont.

``````    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
``````

I kinda understand that each "letter" has an "Asc" value, but im not entirely sure how that works. Can anyone help, would be really great and helpful for me!

A good source of information can be found on the MSDN website. Below are some of the places you where getting stuck on. I've included references to the relivant documenation so if you still don't understand you can go away and see

`````` For i As Integer = 0 To strLength - 1
``````

Do until strLenth is equal to 0 decrementing 1 at a time

cKey = Val(key.Substring(j, 1)) //?? cKey is equal to value of the current character it is looking at (j, 1)??

Yes, cKey will be assigned the last char of the key. If they key was 854824 then cKey is now '4'.

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

Here this formula is used for decrying. What is useful to know here is that the Asc function will return the Asci Value for the character (table here)

A Asci character of a Single-byte Character Sets can not be greater than 255 or less than 0, so if it is greater, then they -255 + the cKey to find the original Ascii value.

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

Same as above but in reverse, this code will encrypt the character value by incrementing 255 to the char value if the ASCII value of char1 - x (where x = the numeric value of the last digit in the cKey) is greater than 0.

Result &= char2 //?????

and lastly this will concatanate the string onto Result each loop. This is short hand for writing

Result = Result + Char2

You'll probably understand it more stepping through the program in debug and seeing the values of the variables as you go along.

Best of luck to you,

hope this helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download