Is anybody know what is the easiest way to encrypt and decrypt text from textbox? Thank you in advance. Good day!

Recommended Answers

All 7 Replies

You said "easy", correct?:D

'// encrypt.
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        With TextBox1
            .Text = .Text.Replace("a", "z").Replace("b", "y").Replace("c", "x")
        End With
    End Sub
    '// decrypt.
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        With TextBox1
            .Text = .Text.Replace("z", "a").Replace("y", "b").Replace("x", "c")
        End With
    End Sub

That is actually really helpful :)

Sorry to jump in on someone else's thread but could you show me a way of encryption which is still easy (ish!) to write but a bit more secure. So maybe a level or two up from this encryption?

This would be amazing if you could. Many thanks!

See if this helps.

Public Class Form1
    Private arLetterChars() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 "
    Private arEncryptedChars() As Char = "¿s¢çc‹i˜Ö´åÄÚKÍìæ¯~4Ûÿ5ÑûÏÉí³èô§ŠÀÙ9ÒÓ?þ.äƒ%*š¾†±HI2@æŒ,"

    '// encrypt.
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        With TextBox1
            For Each myTextBoxChar As Char In .Text '// loop thru TextBox, one char. at a time.
                For i As Integer = 0 To arLetterChars.Length - 1 '// loop thru all letters in the Array.
                    '// if TextBox char ='s the char in your Array, replace the TextBox char with the same #'ed Array char of the Encrypted letters.
                    If myTextBoxChar = arLetterChars(i) Then .Text = .Text.Replace(myTextBoxChar, arEncryptedChars(i))
                Next
            Next
        End With
    End Sub
    '// decrypt.
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        With TextBox1
            For Each myTextBoxChar As Char In .Text '// loop thru TextBox, one char. at a time.
                For i As Integer = 0 To arEncryptedChars.Length - 1 '// loop thru all Encrypted char.s in the Array.
                    '// if TextBox char ='s the char in your Array, replace the TextBox char with the same #'ed Array char of the Letters.
                    If myTextBoxChar = arEncryptedChars(i) Then .Text = .Text.Replace(myTextBoxChar, arLetterChars(i))
                Next
            Next
        End With
    End Sub
End Class

This uses 2 For/Next loops.
First loop, loops through each char. in the TextBox, one at a time. The loop inside the first loop, loops through the char.s in your Arrays, depending on which Array to use, if to encrypt or decrypt.
If a letter is the same letter as in your selected Array, it replaces the same Indexed char. from one Array, with a char. from the other Array.
The weird looking char.s were found by loading a small image in Notepad.

commented: agree +13

Many thanks, this is perfect. :)

One last thing, I hope I'm not being a pain, but do you know of a way to make each character different, but the computer knows how to distinguish which letters are what and so on. For example, the password hello, could this be encrypted so that the l's have a different character and hence it makes it harder to crack.

If not, no worries you've been a massive help, thank you.

From "Sybex - Mastering Microsoft Visual Basic 2008":

Another benefit of using streams is that you can combine them. The typical example is that of encrypting and decrypting data. Data is encrypted through a special type of Stream, the CryptoStream. You write plain data to the CryptoStream, and they’re encrypted by the stream itself. In other words, the CryptoStream object accepts plain data and emits encrypted data. You can connect the CryptoStream object to another Stream object that represents a file andwrite the encrypted data directly to the file. Your code uses simple statements to write data to the CryptoStream object,
which encrypts the data and then passes it to another stream that writes the encrypted data to a file.

I haven't tried this but it may point you in the right direction of a more secure encryption.

Sir Codeorder. Thank you for the very helpful code. Your one of kind. Thank you again and Good day! Also to Sir Reverend Jim. Thank you for the suggestions. i'll to research also for that. :)

Be a part of the DaniWeb community

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