-
Feb 9th, 2018, 11:58 AM
#1
Thread Starter
New Member
Textbox Validation
Hey guys, I need help. I'm trying to make validation to few textbox in VS 2017 but i'm noob with VB.
This is my code:
Code:
Public Class form1
' define the local key and vector byte arrays
Private ReadOnly key() As Byte =
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
'Private ReadOnly iv() As Byte = {8, 7, 6, 5, 4, 3, 2, 1}
Private ReadOnly iv() As Byte = {255, 70, 60, 50, 40, 30, 20, 10}
' instantiate the class with the arrays
Private des As New cTripleDES(key, iv)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text = des.Encrypt(TextBox1.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox2.Text = des.Decrypt(TextBox1.Text)
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
Clipboard.SetText(TextBox2.Text)
End Sub
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Process.Start("testtesttest")
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
End Class
Last edited by Shaggy Hiker; Feb 9th, 2018 at 02:38 PM.
Reason: Added CODE tags.
-
Feb 9th, 2018, 12:07 PM
#2
Thread Starter
New Member
Re: Textbox Validation
Code:
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Friend Class cTripleDES
' define the triple des provider
Private m_des As New TripleDESCryptoServiceProvider
' define the string handler
Private m_utf8 As New UTF8Encoding
' define the local property arrays
Private m_key() As Byte
Private m_iv() As Byte
Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
Me.m_key = key
Me.m_iv = iv
End Sub
Public Function Encrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateEncryptor(m_key, m_iv))
End Function
Public Function Decrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateDecryptor(m_key, m_iv))
End Function
Public Function Encrypt(ByVal text As String) As String
Dim input() As Byte = m_utf8.GetBytes(text)
Dim output() As Byte = Transform(input, _
m_des.CreateEncryptor(m_key, m_iv))
Return Convert.ToBase64String(output)
End Function
Public Function Decrypt(ByVal text As String) As String
Try
Dim input() As Byte = Convert.FromBase64String(text)
Dim output() As Byte = Transform(input, _
m_des.CreateDecryptor(m_key, m_iv))
Return m_utf8.GetString(output)
Catch ex As Exception
MsgBox("Error, Please check your characters.")
End Try
End Function
Private Function Transform(ByVal input() As Byte, _
ByVal CryptoTransform As ICryptoTransform) As Byte()
' create the necessary streams
Dim memStream As MemoryStream = New MemoryStream
Dim cryptStream As CryptoStream = New _
CryptoStream(memStream, CryptoTransform, _
CryptoStreamMode.Write)
' transform the bytes as requested
cryptStream.Write(input, 0, input.Length)
cryptStream.FlushFinalBlock()
' Read the memory stream and convert it back into byte array
memStream.Position = 0
Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
memStream.Read(result, 0, CType(result.Length, System.Int32))
' close and release the streams
memStream.Close()
cryptStream.Close()
' hand back the encrypted buffer
Return result
End Function
End Class
'Public Class cTripleDES
'End Class
Last edited by Shaggy Hiker; Feb 9th, 2018 at 02:39 PM.
Reason: Added CODE tags.
-
Feb 9th, 2018, 12:11 PM
#3
Thread Starter
New Member
Re: Textbox Validation
How to validate that the input in TextBox1 is 3DES string?
And this is 3DES code:
Code:
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Friend Class cTripleDES
' define the triple des provider
Private m_des As New TripleDESCryptoServiceProvider
' define the string handler
Private m_utf8 As New UTF8Encoding
' define the local property arrays
Private m_key() As Byte
Private m_iv() As Byte
Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
Me.m_key = key
Me.m_iv = iv
End Sub
Public Function Encrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateEncryptor(m_key, m_iv))
End Function
Public Function Decrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateDecryptor(m_key, m_iv))
End Function
Public Function Encrypt(ByVal text As String) As String
Dim input() As Byte = m_utf8.GetBytes(text)
Dim output() As Byte = Transform(input, _
m_des.CreateEncryptor(m_key, m_iv))
Return Convert.ToBase64String(output)
End Function
Public Function Decrypt(ByVal text As String) As String
Try
Dim input() As Byte = Convert.FromBase64String(text)
Dim output() As Byte = Transform(input, _
m_des.CreateDecryptor(m_key, m_iv))
Return m_utf8.GetString(output)
Catch ex As Exception
MsgBox("Error, Please check your characters.")
End Try
End Function
Private Function Transform(ByVal input() As Byte, _
ByVal CryptoTransform As ICryptoTransform) As Byte()
' create the necessary streams
Dim memStream As MemoryStream = New MemoryStream
Dim cryptStream As CryptoStream = New _
CryptoStream(memStream, CryptoTransform, _
CryptoStreamMode.Write)
' transform the bytes as requested
cryptStream.Write(input, 0, input.Length)
cryptStream.FlushFinalBlock()
' Read the memory stream and convert it back into byte array
memStream.Position = 0
Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
memStream.Read(result, 0, CType(result.Length, System.Int32))
' close and release the streams
memStream.Close()
cryptStream.Close()
' hand back the encrypted buffer
Return result
End Function
End Class
'Public Class cTripleDES
'End Class
Last edited by Shaggy Hiker; Feb 9th, 2018 at 02:39 PM.
Reason: Added CODE tags.
-
Feb 9th, 2018, 02:08 PM
#4
Re: Textbox Validation
What I read is that you ripped off some code and cant figure out how it works...
-
Feb 9th, 2018, 02:40 PM
#5
Re: Textbox Validation
Welcome to the forums. I edited your posts to add [CODE][/CODE] tags to make the code look nicer. You can do that by pressing the # button and pasting the code between the resulting tags.
My usual boring signature: Nothing
-
Feb 9th, 2018, 02:41 PM
#6
Re: Textbox Validation
Originally Posted by kpmc
What I read is that you ripped off some code and cant figure out how it works...
So, what's wrong with that? Code reuse is the name of the game.
My usual boring signature: Nothing
-
Feb 9th, 2018, 02:50 PM
#7
Re: Textbox Validation
With encryption code it can be a really bad idea. For example, many people currently consider 3DES to be insecure, there are enough successful attacks against it they caution against its use. There are many other "good" algorithms that display weaknesses when configured improperly. Jeff Atwood's "The Bathroom Wall of Code" post is a good example, he demonstrated an attackable flaw in a .NET encryption code snippet that has been copy/pasted into thousands of tutorials and, presumably, many more instances of production code. This is what happens when you copy and paste code you don't understand.
The problem with security-focused code is it's almost always not intuitive, and only very skilled individuals understand all of the innocent things you might do that can dramatically weaken your crypto. You can read it, understand it, decide it looks perfectly valid, and be doing something incredibly dumb all at the same time. It's the brain surgery of programming, and we're willing to recruit our surgeons from the equivalent of Craigslist ads
To answer OP's question:
How to validate that the input in TextBox1 is 3DES string?
Technically, you can't. You can try to decrypt it, and you can display the results if successfully decrypted. (It will fail for various situations like "the string doesn't represent the right number of bytes".) If you know what the results are supposed to look like, you can perform validation on them and decide if the input was "valid".
But good encryption algorithms aren't supposed to let you deduce things about the nature of their plaintext from observing the ciphertext. So you can only do some very basic tests ahead of time, like a length check. But if you're working with Base-64 encoded Strings, determining the length of the output byte array is roughly as complex as decoding it. So you may as well make a decryption attempt and consider "it threw an exception" as "failed validation".
Last edited by Sitten Spynne; Feb 9th, 2018 at 02:53 PM.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Feb 9th, 2018, 03:45 PM
#8
Thread Starter
New Member
Re: Textbox Validation
Okey guys.. Thanks.. I'm trying to learn Visual Basic. I'm tryin to do something realistic
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|