Hi guys! I am a newbie in VB.NET and am trying to create a cipher. In my program, before you can decrypt a string, you are prompted to enter a key. After you enter the correct c key,the string is decrypted. The problem is that the plaintext appears at the same time with the key form.The key should be entered first and verified then the plaintext to appear later. I have the following objects on my form;

  1. Plaintext textbox
  2. Ciphertext textbox
  3. Encrypt button
  4. Decrypt button

Recommended Answers

All 7 Replies

Set the Visible to false for the plain text. Then, when the entered key is OK. set the property to true.

Hope this helps

Member Avatar for Unhnd_Exception

Heres another example

If i remember the encrypt and decrypt function were pretty much a cut and paste from the MSDN Library

Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

Public Class Form1

    Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
        Dim key As String = InputBox("Key:", "Encryption Key")
        If String.IsNullOrEmpty(key) Then
            MsgBox("Key is not optional", MsgBoxStyle.OkOnly, "Encryption Key")
            Exit Sub
        End If

        txtEncrypt.Text = Encrypt(txtPlain.Text, key)

        txtPlain.Text = String.Empty

    End Sub

    Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click
        Dim key As String = InputBox("Key:", "Decryption Key")
        If String.IsNullOrEmpty(key) Then
            MsgBox("Key is not optional", MsgBoxStyle.OkOnly, "Decryption Key")
            Exit Sub
        End If

        txtPlain.Text = Decrypt(txtEncrypt.Text, key)

        txtEncrypt.Text = String.Empty
    End Sub

    Private Function GetKeys(ByVal KeyString As String, ByVal length As Integer) As Byte()
        'Returns a hash value of the secret key


        Dim HashValue() As Byte
        Dim UE As New UnicodeEncoding()

        'Convert the string into an array of bytes.
        Dim MessageBytes As Byte() = UE.GetBytes(KeyString)

        'Create the hash value from the array of bytes.
        Dim SHhash As New SHA1Managed()
        HashValue = SHhash.ComputeHash(MessageBytes)

        'Optional feature to adjust the length of the hash
        ReDim Preserve HashValue(length - 1)

        UE = Nothing
        SHhash = Nothing

        Return HashValue

    End Function

    Private Function Encrypt(ByVal ClearText As String, ByVal SecretKey As String) As String

        Dim EncryptedText As String = String.Empty
        Dim mstream As New MemoryStream()
        Dim CryptStream As CryptoStream = Nothing

        Try

            'Get the bytes
            Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(ClearText)

            'Create a new instance of the RijndaelManaged class
            Dim RMCrypto As New RijndaelManaged()

            'get the key and iv
            Dim Key As Byte() = GetKeys(SecretKey, 16)
            Dim IV As Byte() = GetKeys(SecretKey, 16)

            CryptStream = New CryptoStream(mstream, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write)

            'Encrypt the text
            CryptStream.Write(plaintextBytes, 0, plaintextBytes.Length)
            CryptStream.FlushFinalBlock()

            'Convert the encrypted bytes to a 64 base string
            EncryptedText = System.Convert.ToBase64String(mstream.ToArray)

        Catch ex As Exception


        Finally
            CryptStream.Dispose()
            mstream.Close()
        End Try

        Return EncryptedText

    End Function

    Private Function Decrypt(ByVal EncryptedText As String, ByVal SecretKey As String) As String

        Dim PlainText As String = String.Empty

        'convert the 64 base string back to bytes
        Dim EncryptedBytes() As Byte = System.Convert.FromBase64String(EncryptedText)

        Dim mStream As New MemoryStream

        Try

            Dim RMCrypto As New RijndaelManaged()

            'get the key and iv
            Dim Key As Byte() = GetKeys(SecretKey, 16)
            Dim IV As Byte() = GetKeys(SecretKey, 16)

            Dim CryptStream As New CryptoStream(mStream, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Write)

            'Decrypt the message
            CryptStream.Write(EncryptedBytes, 0, EncryptedBytes.Length)
            CryptStream.FlushFinalBlock()

            'Convert the bytes to the plain text
            PlainText = System.Text.Encoding.Unicode.GetString(mStream.ToArray)

            CryptStream.Close()
            mStream.Close()

        Catch ex As Exception
            MsgBox("The key may be incorrect", MsgBoxStyle.OkOnly, "Decryptoin Error")

        Finally
            mStream.Dispose()

        End Try

        Return PlainText

    End Function

End Class

Good example on cypheing.

If I understood well your example, you first enter the plain text, then ask for the key in the btnEncrypt_Click event handler, but IMO the question from Denni_Mwebia is how to force the user to enter the key before entering the txtEncrypt.Text. I am wrong?

Member Avatar for Unhnd_Exception

I not sure what the question is.

The plain text is showing at the same time as the key form? The key should be entered first and verified before decrypting?

If the plain text is showing before the key is entered then it wasn't encrypted in the first place.

I provided an example where you can type in some clear text, hit a button that asks you for a key, encrypts it with the key, removes the clear text and displays the encrypted text. To decrypt it: a button has to be pressed, the original key entered, text is decrypted, and the clear text is displayed.

Thanks to clarify

Thanks guys for your help. I've changed the program to accomodate an easier way of key entry. This is what i've done: below each button i.e Encryption and decryption, i've inserted a textbox, each requiring a key entry. Without the key entry, you cannot proceed any further! Appealing?

So I think the problem is solved.

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.