Imports System.Security.Cryptography Public Class EncryptDecryptClass Private Shared m_strPassPhrase As String = "MyPriv@Password!$$" '---- any text string is good here Private Shared m_strPasswordIterations As Integer = 2 '--- can be any number Private Shared m_strInitVector As String = "@1B2c3D4e5F6g7H8" '--- must be 16 bytes Private Shared m_intKeySize As Integer = 256 '--- can be 192 or 128 'Encrypt Function: Function EncryptPasswordMD5(ByVal plainText As String, ByVal p_strSaltValue As String) As String Dim strReturn As String = String.Empty ' Convert strings into byte arrays. ' Let us assume that strings only contain ASCII codes. ' If strings include Unicode characters, use Unicode, UTF7, or UTF8 ' encoding. Try Dim initVectorBytes As Byte() initVectorBytes = System.Text.Encoding.ASCII.GetBytes(m_strInitVector) Dim saltValueBytes As Byte() saltValueBytes = System.Text.Encoding.ASCII.GetBytes(p_strSaltValue) ' Convert our plaintext into a byte array. ' Let us assume that plaintext contains UTF8-encoded characters. Dim plainTextBytes As Byte() plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText) ' First, we must create a password, from which the key will be derived. ' This password will be generated from the specified passphrase and ' salt value. The password will be created using the specified hash ' algorithm. Password creation can be done in several iterations. Dim password As Rfc2898DeriveBytes password = New Rfc2898DeriveBytes(m_strPassPhrase, _ saltValueBytes, _ m_strPasswordIterations) ' Use the password to generate pseudo-random bytes for the encryption ' key. Specify the size of the key in bytes (instead of bits). Dim keyBytes As Byte() Dim intKeySize As Integer = 0 intKeySize = CType((m_intKeySize / 8), Integer) keyBytes = password.GetBytes(intKeySize) ' Create uninitialized Rijndael encryption object. Dim symmetricKey As System.Security.Cryptography.RijndaelManaged symmetricKey = New System.Security.Cryptography.RijndaelManaged ' It is reasonable to set encryption mode to Cipher Block Chaining ' (CBC). Use default options for other symmetric key parameters. symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC 'symmetricKey.Padding = PaddingMode.Zeros ' Generate encryptor from the existing key bytes and initialization ' vector. Key size will be defined based on the number of the key ' bytes. Dim encryptor As System.Security.Cryptography.ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes) ' Define memory stream which will be used to hold encrypted data. Dim memoryStream As System.IO.MemoryStream memoryStream = New System.IO.MemoryStream ' Define cryptographic stream (always use Write mode for encryption). Dim cryptoStream As System.Security.Cryptography.CryptoStream cryptoStream = New System.Security.Cryptography.CryptoStream(memoryStream, _ encryptor, _ System.Security.Cryptography.CryptoStreamMode.Write) ' Start encrypting. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length) ' Finish encrypting. cryptoStream.FlushFinalBlock() ' Convert our encrypted data from a memory stream into a byte array. Dim cipherTextBytes As Byte() cipherTextBytes = memoryStream.ToArray() ' Close both streams. memoryStream.Close() cryptoStream.Close() ' Convert encrypted data into a base64-encoded string. Dim cipherText As String cipherText = Convert.ToBase64String(cipherTextBytes) ' Return encrypted string. strReturn = cipherText Catch ex As Exception strReturn = Nothing End Try Return strReturn End Function 'Decrypt Function: Function DecryptPasswordMD5(ByVal cipherText As String, ByVal p_strSaltValue As String) As String Dim strReturn As String = String.Empty ' Convert strings defining encryption key characteristics into byte ' arrays. Let us assume that strings only contain ASCII codes. ' If strings include Unicode characters, use Unicode, UTF7, or UTF8 ' encoding. Try Dim initVectorBytes As Byte() initVectorBytes = System.Text.Encoding.ASCII.GetBytes(m_strInitVector) Dim saltValueBytes As Byte() saltValueBytes = System.Text.Encoding.ASCII.GetBytes(p_strSaltValue) ' Convert our ciphertext into a byte array. Dim cipherTextBytes As Byte() cipherTextBytes = Convert.FromBase64String(cipherText) ' First, we must create a password, from which the key will be ' derived. This password will be generated from the specified ' passphrase and salt value. The password will be created using ' the specified hash algorithm. Password creation can be done in ' several iterations. Dim password As Rfc2898DeriveBytes password = New Rfc2898DeriveBytes(m_strPassPhrase, _ saltValueBytes, _ m_strPasswordIterations) ' Use the password to generate pseudo-random bytes for the encryption ' key. Specify the size of the key in bytes (instead of bits). Dim keyBytes As Byte() Dim intKeySize As Integer intKeySize = CType((m_intKeySize / 8), Integer) keyBytes = password.GetBytes(intKeySize) ' Create uninitialized Rijndael encryption object. Dim symmetricKey As System.Security.Cryptography.RijndaelManaged symmetricKey = New System.Security.Cryptography.RijndaelManaged ' It is reasonable to set encryption mode to Cipher Block Chaining ' (CBC). Use default options for other symmetric key parameters. symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC 'symmetricKey.Padding = PaddingMode.Zeros ' Generate decryptor from the existing key bytes and initialization ' vector. Key size will be defined based on the number of the key ' bytes. Dim decryptor As System.Security.Cryptography.ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes) ' Define memory stream which will be used to hold encrypted data. Dim memoryStream As System.IO.MemoryStream memoryStream = New System.IO.MemoryStream(cipherTextBytes) ' Define memory stream which will be used to hold encrypted data. Dim cryptoStream As System.Security.Cryptography.CryptoStream cryptoStream = New System.Security.Cryptography.CryptoStream(memoryStream, _ decryptor, _ System.Security.Cryptography.CryptoStreamMode.Read) ' Since at this point we don't know what the size of decrypted data ' will be, allocate the buffer long enough to hold ciphertext; ' plaintext is never longer than ciphertext. Dim plainTextBytes As Byte() ReDim plainTextBytes(cipherTextBytes.Length) ' Start decrypting. Dim decryptedByteCount As Integer decryptedByteCount = cryptoStream.Read(plainTextBytes, _ 0, _ plainTextBytes.Length) ' Close both streams. memoryStream.Close() cryptoStream.Close() ' Convert decrypted data into a string. ' Let us assume that the original plaintext string was UTF8-encoded. Dim plainText As String plainText = System.Text.Encoding.UTF8.GetString(plainTextBytes, _ 0, _ decryptedByteCount) ' Return decrypted string. strReturn = plainText Catch ex As Exception strReturn = Nothing End Try Return strReturn End Function Function EncryptPassword(ByVal Password As String) As String 'Encrypt the Password Dim sEncryptedPassword As String = "" Dim sEncryptKey As String = "P@SSW@RD@09" 'Should be minimum 8 characters Try sEncryptedPassword = EncryptPasswordMD5(Password, sEncryptKey) Catch ex As Exception Return sEncryptedPassword End Try Return sEncryptedPassword End Function Function DecryptPassword(ByVal Password As String) As String 'Encrypt the Password Dim sDecryptedPassword As String = "" Dim sEncryptKey As String = "P@SSW@RD@09" 'Should be minimum 8 characters Try sDecryptedPassword = DecryptPasswordMD5(Password, sEncryptKey) Catch ex As Exception Return sDecryptedPassword End Try Return sDecryptedPassword End Function End Class Module VBModule Sub Main() Dim obj As New EncryptDecryptClass Console.WriteLine(obj.EncryptPassword("hello")) Console.WriteLine(obj.DecryptPassword("TwIHrur1CFsjEdYaNR60Qg==")) End Sub End Module
Write, Run & Share VB.net code online using OneCompiler's VB.net online compiler for free. It's one of the robust, feature-rich online compilers for VB.net language, running on the latest version 16. Getting started with the OneCompiler's VB.net compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as VB.net
. OneCompiler also has reference programs, where you can look for the sample code to get started with.
OneCompiler's VB.net online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample VB.net program which takes name as input and prints hello message with your name.
Public Module Program
Public Sub Main(args() As string)
Dim name as String = Console.ReadLine() ' Reading input from STDIN
Console.WriteLine("Hello " & name) ' Writing output to STDOUT
End Sub
End Module
Visual Basic is a event driven programming language by Microsoft, first released in the year 1991.
Variable is a name given to the storage area in order to identify them in our programs.
Simple syntax of Variable declaration is as follows
Dim variableName [ As [ New ] dataType ] [ = initializer ]
variableName = value
If condition-expression Then
'code
End If
If(conditional-expression)Then
'code if the conditional-expression is true
Else
'code if the conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
Else If(conditional-expression) Then
'code if the above conditional-expression is true
Else
'code if the above conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
If(conditional-expression)Then
'code if the above conditional-expression is true
End If
End If
Select [ Case ] expression
[ Case expressionlist
'code ]
[ Case Else
'code ]
End Select
For counter [ As datatype ] = begin To end [ Step step ]
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ counter ]
For Each element [ As datatype ] In group
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ element ]
While conditional-expression
'Code
[ Continue While ]
'Code
[ Exit While ]
'Code
End While
Do { While | Until } conditional-expression
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop
Do
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop { While | Until } conditional-expression
Procedure is a sub-routine which contains set of statements. Usually Procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Procedures are of two types.
Functions return a value when they are called.
[accessModifiers] Function functionName [(parameterList)] As returnType
'code
End Function
Sub-procedures are similar to functions but they don't return any value.
Sub ProcedureName (parameterList)
'Code
End Sub