Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Encrypter in VB

Options
  • 21-02-2007 10:45am
    #1
    Closed Accounts Posts: 29,930 ✭✭✭✭


    Hi,

    I'm looking to write a program that will encrypt a given string using caesar cipher code. Can somebody explain or link to how it works (in general), and why I need a key rather then just an input and output box etc....

    thanks.


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    http://www.google.ie/search?hl=en&q=caesar+cipher+code+vb.net&btnG=Search&meta=

    Why ceaser cipher? There's plenty of good encryption algorithms available with .net.


  • Closed Accounts Posts: 29,930 ✭✭✭✭TerrorFirmer


    Thanks phil. It's an assignment, so it has to be caeser. But as with all my other programs rather then just copy somebody elses code I want to understand how it works!

    It's VB6 I'm using.

    Anyway, I've written my code now (mostly, had a tutorial this morning for help). Except...as dumb as this sounds I don't know how to get my sub function to write the results back to my main text box??


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Change the sub to a function of type string, here's a rough example (I'm not a vb coder anymore):
    private sub DoesSomething()
    
        TextBox1.Text = foo();
    
    end sub
    
    private function foo() as string
        
       foo = "hello world"
    
    end function
    
    


  • Closed Accounts Posts: 29,930 ✭✭✭✭TerrorFirmer


    Private Sub cmdEn_Click()
    Dim key As Integer, plain As String, cipher As String

    key = Val(txtKey.Text)
    plain = txtInput.Text

    If key >= 0 And key <= 25 Then
    cipher = Encrypt(plain, key)
    txtInput.Text = cipher
    Else
    txtInput.Text = "The key must be between 0 and 25, thanks."
    End If

    txtInput.Text = ??? (this part I'm stuck with, reading back info from the function)

    End Sub

    Private Sub cmdExit_Click()
    End
    End Sub


    Private Function Encrypt(plaintext As String, code As Integer)
    Dim i As Integer
    Dim char As String
    Dim temp As String
    Dim temp_asc As Integer

    For i = 1 To Len(plaintext)
    char = Mid(plaintext, i, 1)
    If asc_char >= 97 And asc_char <= 122 Then
    temp_asc = Asc(char) + code
    If temp_asc > 122 Then
    temp_asc = 96 + (temp_asc - 122)
    End If
    temp = temp & Chr(temp_asc)
    Else
    temp = temp + char
    End If
    Next i

    Encrypt = temp
    End Function

    I have this - how do I get read back the info from the private function to my main procedure...:o


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Your already doing it in the code you posted. Are you sure this is your code?!

    TBH I'm not going to tell you as it's easily googled an you'll learn more if you work it out for yourself.


  • Advertisement
  • Closed Accounts Posts: 29,930 ✭✭✭✭TerrorFirmer


    Yes it is my code, written with my lab tutor yesterday but I didn't check it before the class ended! Except it doesn't work - so I don't know how I'm supposed to get the encrypted string BACK to the main procedure! I've tried googling it but I can't find anything to help...

    edit: Oh wait I actually see now, sorry - it's already there with the textinput = cipher......sorry I don't know how I missed that part. Any ideas why it isn't working?


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Set a breakpoint at the start (Private Sub cmdEn_Click()) and step through you code, you should be able to work it out yourself. The VS help file will tell you how to do that, I can't remember how its done in vb6.


Advertisement