1

i am only new in vb 10 and i am creating a vigenere cipher program but i dont know how to call a function in a button. here's my code:

Public Shared Function Encrypt(ByVal cipherTxt As String, ByVal key As String)
    Dim encryptedText As String = ""
    For i As Integer = 1 To cipherTxt.Length
        Dim temp As Integer = AscW(GetChar(cipherTxt, i)) + AscW(GetChar(key, i Mod key.Length + 1))
        encryptedText += ChrW(temp)
    Next
    Return encryptedText
End Function

Public Shared Function Decrypt(ByVal cipherTxt As String, ByVal key As String)
    Dim decryptedText As String = ""
    For i As Integer = 1 To cipherTxt.Length
        Dim temp As Integer = AscW(GetChar(cipherTxt, i)) - AscW(GetChar(key, i Mod key.Length + 1))
        decryptedText += ChrW(temp)
    Next
    Return decryptedText
End Function

Any help? Thank you.

1 Answers1

0

Well you would need to do something like this

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Encrypt("This is the string to Encrypt", "This is the key")
End Sub

You would need to pass in the text to encrypt which might be from a text box and the key could well be a private variable so to take that a stage further. Let's say that TextBox1 contains some text that you wish to encrypt (and return that encrypted text to the text box that it came from;

Private _myKey As String ="This is the key to encrypt & Decrypt"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = Encrypt(TextBox1.text, _myKey)
End Sub

The same basic functionality applies to decryption.

NB: This contains no error checking, and storing you key in an open fashion is not recommended, but there should be enough here to get you started

Dom Sinclair
  • 2,458
  • 1
  • 30
  • 47