Encryption code problem in visual basic

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 2795 Views - Last Post: 27 September 2011 - 07:30 AM Rate Topic: -----

#1 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Encryption code problem in visual basic

Posted 25 September 2011 - 02:59 PM

I'm working on a project and it: Should read a four digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10 then swap the first digit with the third and swat the second digit with the fourth. Display the encrypted integer in it's own box.

When I hit the encrypt button it is encrypting the text I put in and the text in the encrypt box. I need for it not to encrypt the text that is in the text box too. I think it has something to do with my string but I've tried changing it and I'm stumped. Can anyone help.?

'Charyl Janney
'Sept. 23, 2011
'Cins 137 Exercise 4.18
'Enforcing privacy with Cryptography 
'This program is to encrypt numbers so it may be transmitted more securely


Public Class Encryption

    Private Sub txtInPut_KeyPress(ByVal send As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
          Handles TxtInput.KeyPress

        'text box accepts only numbers, allows backspace to edit  

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
           AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If


    End Sub

    Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEncrypt.Click

        TxtInput.Text = Encrypt(TxtInput.Text)

        If Val(TxtInput.Text) < 0 Or
       (IsNumeric(TxtInput.Text) = False) Then
            'MessageBox.Show("Invalid Entry", "Input Error")
            Exit Sub
        End If
        lblEncrypt.Text = "Encrypted number is" & Encrypt(TxtInput.Text).ToString

    End Sub

    Private Function Encrypt(ByVal strInput As String) As String
        Dim i As Integer 
        Dim strDigit As String
        Dim intDigit As Integer
        Dim intDigits(3) As Integer

        For i = 1 To 4
            strDigit = Mid(strInput, i, 1)
            intDigit = CInt(strDigit) + 7
            intDigits(i - 1) = intDigit Mod 10

        Next i
        Encrypt = CStr(intDigits(2)) & CStr(intDigits(3)) & CStr(intDigits(0)) & CStr(intDigits(1))


    End Function
End Class





Is This A Good Question/Topic? 0
  • +

Replies To: Encryption code problem in visual basic

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 03:45 PM

Youre saying TxtInput.Text = Encrypt(suchandsuch)

HTH charyl
Was This Post Helpful? 1
  • +
  • -

#3 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 04:19 PM

Sorry, I'm a little confused. The name of my text box is txtInput. I changed it to
txtEnter. I thought it had to be called as a text and now I have the fallowing error. Do I need a throw exception?

Error 1 Argument not specified for parameter 'strInput' of 'Private Function Encrypt(strInput As String) As String'. H:\school\Fall2011\CJanneyEx4_18\Encryption\Encryption\Encryption.vb 27 20 Encryption
'Charyl Janney
'Sept. 23, 2011
'Cins 137 Exercise 4.18
'Enforcing privacy with Cryptography 
'This program is to encrypt numbers so it may be transmitted more securely


Public Class Encryption

    Private Sub txtInPut_KeyPress(ByVal send As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
          Handles TxtEnter.KeyPress

        'text box accepts only numbers, allows backspace to edit  

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
           AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If


    End Sub

    Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEncrypt.Click

        'Integers moved to string variable is named

        TxtEnter = Encrypt()

        If Val(TxtEnter.Text) < 0 Or
       (IsNumeric(TxtEnter.Text) = False) Then

            Exit Sub
        End If
        lblEncrypt.Text = "Encrypted number is " & Encrypt(TxtEnter.Text).ToString

    End Sub


    Private Function Encrypt(ByVal strInput As String) As String
        Dim i As Integer
        Dim strDigit As String
        Dim intDigit As Integer
        Dim intDigits(3) As Integer


        For i = 1 To 4
            strDigit = Mid(strInput, i, 1)
            intDigit = CInt(strDigit) + 7
            intDigits(i - 1) = intDigit Mod 10

        Next i
        Encrypt = CStr(intDigits(2)) & CStr(intDigits(3)) & CStr(intDigits(0)) & CStr(intDigits(1))


    End Function


End Class





My error has to do with the line of coding that says:

'Integers moved to string variable is named. (line right below it.
Was This Post Helpful? 0
  • +
  • -

#4 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 04:55 PM

ok, I think I figured it out???? Can you double check? Tell me if I'm on the right track. Seems to work.

'Charyl Janney
'Sept. 23, 2011
'Cins 137 Exercise 4.18
'Enforcing privacy with Cryptography 
'This program is to encrypt numbers so it may be transmitted more securely


Public Class Encryption

    Private Sub txtInPut_KeyPress(ByVal send As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
          Handles TxtEnter.KeyPress

        'text box accepts only numbers, allows backspace to edit  

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
           AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If


    End Sub

    Private Sub btnEncrypt_Click(ByVal format As System.Object, ByVal e As System.EventArgs) Handles BtnEncrypt.Click

        'Integers moved to string variable is named

        'TxtEnter.Text = "Encrypt"
        If Val(TxtEnter.Text) < 0 Or
       (IsNumeric(TxtEnter.Text) = False) Then

            Exit Sub
        End If
        lblEncrypt.Text = "Encrypted number is " & Encrypt(TxtEnter.Text).ToString

    End Sub


    Private Function Encrypt(ByVal strInput As String) As String
        Dim i As Integer
        Dim strDigit As String
        Dim intDigit As Integer
        Dim intDigits(3) As Integer


        For i = 1 To 4
            strDigit = Mid(strInput, i, 1)
            intDigit = CInt(strDigit) + 7
            intDigits(i - 1) = intDigit Mod 10

        Next i
        Encrypt = CStr(intDigits(2)) & CStr(intDigits(3)) & CStr(intDigits(0)) & CStr(intDigits(1))


    End Function


End Class





My next concern is I only want to be able to type in 4 numbers, suppose I should have an error message box.
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 05:01 PM

Sure looks fine - I can't test it but looking at it it's fine. Glad you figured it out!!
Was This Post Helpful? 1
  • +
  • -

#6 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 05:07 PM

Regarding the text length:
In the designer window click on your TextBox and scroll to the MaxLength property - set it to 4.
Was This Post Helpful? 1
  • +
  • -

#7 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 05:12 PM

Soooo exciting!!!! Now, wanna help decrypt this? :whistling:

I was suppose to create a separate gui to decrypt, but I'm putting in a decrypt button and text box on the same gui. Doesn't make any sense to create a whole new program when all you have to do is hit a button.....
Was This Post Helpful? 0
  • +
  • -

#8 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 05:18 PM

maybe it would be simpler just to use the gui I already have and reverse the coding a little and call it Decrypter instead?
Was This Post Helpful? 0
  • +
  • -

#9 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 05:18 PM

You are quite right - if you don't need to do it why do it (apart from wanting to) - perhaps position it below the encryption section (place the seperate functions in GroupBoxes?)
Was This Post Helpful? 1
  • +
  • -

#10 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 06:36 PM

When I close my program and reopen it the code is fine but I get this error: Value cannot be null. Parameter name: objectType
I rebuilt it after messing it up. Do you have any idea what this is? When I turn this in will the files be corrupt?
Was This Post Helpful? 0
  • +
  • -

#11 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 06:47 PM

Here's what it says at the bottom:

Error 2 Unable to open module file 'C:\Documents and Settings\CE User\Local Settings\Temp\.NETFramework,Version=v4.0,Profile=Client.AssemblyAttributes.vb': The system cannot find the file specified. C:\Documents and Settings\CE User\Local Settings\Temp\.NETFramework,Version=v4.0,Profile=Client.AssemblyAttributes.vb 1 1 EncryptMe
Was This Post Helpful? 0
  • +
  • -

#12 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 06:50 PM

Do you have any code in your Load event? If so, can you post it so I can have a look?
Was This Post Helpful? 1
  • +
  • -

#13 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 07:08 PM

When I load it or go to my external drive (You'll have to help me learn something new here- I'm not sure what a load event is) I'm a darn web designer who prefers water color painting :)...Decided I needed to go out of my comfort zone and educate myself so it's very difficult for people of your great knowledge to remember what it was like to be a dork :)..I do mean that in the nicest way. But how can I not know what a load event is... (I do by the way love learning) I've zipped my file and was going to try e-mailing to myself, but I have already been warned if my teacher opens error messages it's an automatic 75%.

at the top of the VB screen this is what it says:

To prevent possible data loss before loading the designer, the following errors must be resolved:

This is the error messages at the bottom.

Warning 1 Value cannot be null.
Parameter name: objectType 0 0
Error 2 Unable to open module file 'C:\Documents and Settings\CE User\Local Settings\Temp\.NETFramework,Version=v4.0,Profile=Client.AssemblyAttributes.vb': The system cannot find the file specified. C:\Documents and Settings\CE User\Local Settings\Temp\.NETFramework,Version=v4.0,Profile=Client.AssemblyAttributes.vb 1 1 EncryptMe
Was This Post Helpful? 0
  • +
  • -

#14 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 07:15 PM

on the screen there is a instances of this error report? Personally I took one look at that and closed it, but here is what is says:


Instances of this error (1)

1. Hide Call Stack

at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.GetRuntimeType(Type objectType)
at System.ComponentModel.TypeDescriptionProvider.GetRuntimeType(Type reflectionType)
at Microsoft.VisualStudio.Design.MultiTargetingContextProvider.GetRuntimeType(Type objectType)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.HandlesClauseManager.GetFieldType(String fieldName, Type documentBaseType)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.HandlesClauseManager.GetReferencedComponentType(String componentName, CodeTypeDeclaration codeTypeDecl, ITypeResolutionService loader, IDictionary& cache)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.HandlesClauseManager.ParseHandlesClauses(CodeTypeDeclaration codeTypeDecl, Boolean updateCache)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnMethodPopulateStatements(Object sender, EventArgs e)
at System.CodeDom.CodeMemberMethod.get_Statements()
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)
Was This Post Helpful? 0
  • +
  • -

#15 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Encryption code problem in visual basic

Posted 25 September 2011 - 07:25 PM

Have you tried:
Copying the code to a new Windows Forms Application, as long as the controls?
I feel worried that you're going to get deducted marks because I couldn't help you (I'm a worried person) - whens it due?
The longer the better, it gives us more time two work out your issue.

Also, think back to a moment when it didn't spit errors at you. If there is one then try to roll back to it.
If not, or you can't restore it, reconstruct the program through a new Windows Form Application.

Also, lookingback at your errors it could be corrupt temp files.you could try moving to another computer and working from there

HTH!!!
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3