alright i have this problem out of my book that i bought used and goin through doin some self excersiesfor practice now this one has me stumped to ends the only thing i have so far is my dim as integer thats it but the problem reads like this:
Your application 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 swap the second digit with the fourth. Then display the encrypted integer. Write a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number.

now can someone get me on the right path iam curious bout knowning how to encrypt numbers like this and have it read back but just stumps me ?? any ideas besides a dim statement where to begin ??

Recommended Answers

All 8 Replies

Let's try to make it clear with an example. Take the number 9184 or 9 1 8 4.

Add 7 to each digit giving 16 8 15 11

Take the remainder after dividing by 10 giving 6 8 5 1

Swap digits 1 & 3 giving 5 8 6 1

Swap digits 2 & 4 giving 5 1 6 8 or a resulting number of 5168.

Now let's reverse the process.

Swap digits 2 & 4 giving 5 8 6 1

Swap digits 1 & 3 giving 6 8 5 1

Add 10 to each digit giving 16 18 15 11

Now subtract 7 from each number giving 9 11 8 4

Oops. We have a two digit number in there. Because we "threw away" some information when we divided by 10 at the start we may end up with some "extra" numbers, in other words, a two digit result where we expect to have only one digit. We compensate for this by doing another remainder operation giving 9 1 8 4 or the original number. Does that help?

yes that did help out plus other sources here is my code and it works i have one more ? :

Public Class frmEncryption  

02     Private Sub txtenter_KeyPress(ByVal send As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _  

03           Handles txtenter.KeyPress  


05         'text box accepts only numbers, allows backspace to edit  


07         If (e.KeyChar < "0" OrElse e.KeyChar > "9") _  

08             AndAlso e.KeyChar <> ControlChars.Back Then 

09             e.Handled = True 

10         End If 


12     End Sub 



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



18         txtenter.Text = Encrypt(txtenter.Text)  


20         If Val(txtenter.Text) < 0 Or 

21         (IsNumeric(txtenter.Text) = False) Then 

22             MessageBox.Show("Invalid Entry", "Input Error")  

23             Exit Sub 

24         End If 



27         lblresult.Text = Encrypt(txtenter.Text).ToString  

28     End Sub 

29     Private Function Encrypt(ByVal strInput As String) As String 

30         Dim i As Integer 

31         Dim strDigit As String 

32         Dim intDigit As Integer 

33         Dim intDigits(3) As Integer 


35         For i = 1 To 4  

36             strDigit = Mid(strInput, i, 1)  

37             intDigit = CInt(strDigit) + 7  

38             intDigits(i - 1) = intDigit Mod 10  

39         Next i  


41         Encrypt = CStr(intDigits(2)) & CStr(intDigits(3)) & CStr(intDigits(0)) & CStr(intDigits(1))  

42     End Function 



45 End Class

my ? is how do i make this word "Encrypted number is:" in the result lbl with the acutal # ?? idk tried couple things so just need that and ill be happy lark :P

You mean like

lblMyLabel.Text = "Encrypted number is " & Encrypt

thnak you that helped =] tho i have new issue in when u debug it and run the program 1. thing is i can eanter more than 4 digits how do i limit it to enter just 4?
and 2 when i hit the encryptbutton both the input box and output change the input should not change with the output how do u fix this ?? plz ASAP !!

You could use a textbox for the data entry and use the key filtering to limit entry to only digits, backspace and delete. You can set the Maxlength property of the textbox to 4. Further, you could disable the Encrypt button until the fourth digit had been entered. That would prevent trying to encrypt with the wrong number of digits.

can u give me an example for both ??

set the form KeyPreview property to True and txtDigits.Maxlength = 4

Public Class Form1

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If txtDigits.Focused Then        'ensures filtering only on this textbox

            Select Case e.KeyCode
                Case 48 To 57, 8, 46, 37, 39
                    'digits 0-9, bs, del, left, right
                Case Else
                    e.SuppressKeyPress = True
            End Select

        End If

    End Sub

    Private Sub txtDigits_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDigits.TextChanged
        btnEncrypt.Enabled = txtDigits.Text.Length = 4
    End Sub

End Class

I'm going to keep you honest now.

alright i have this problem out of my book that i bought used and goin through doin some self excersiesfor practice


how do u fix this ?? plz ASAP !!

seem to be at odds. You claim to be doing some self educating but the ASAP comment implies some sort of deadline which to me says "homework assignment that is due right freaking now". If you need help with homework I am happy to provide explanations or pointers, even sample code to illustrate a technique such as the key filtering, but please be honest enough to admit this up front.

If I am misreading the situation then I apologize.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.