Encryption and Decryption in VB.NET

Imports System.Security.Cryptography

Public Class Simple3Des

    Private TripleDes As New TripleDESCryptoServiceProvider

    Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte()

        Dim sha1 As New SHA1CryptoServiceProvider

        ' Hash the key.
        Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key)
        Dim hash() As Byte = sha1.ComputeHash(keyBytes)

        ' Truncate or pad the hash.
        ReDim Preserve hash(length - 1)
        Return hash

    End Function

    Sub New(ByVal key As String)

        ' Initialize the crypto provider.
        TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
        TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)

    End Sub

    Public Function EncryptData(ByVal plaintext As String) As String

        ' Convert the plaintext string to a byte array.
        Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)

        ' Create the stream.
        Dim ms As New System.IO.MemoryStream
        ' Create the encoder to write to the stream.
        Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

        ' Use the crypto stream to write the byte array to the stream.
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
        encStream.FlushFinalBlock()

        ' Convert the encrypted stream to a printable string.
        Return Convert.ToBase64String(ms.ToArray)

    End Function

    Public Function DecryptData(ByVal encryptedtext As String) As String

        ' Convert the encrypted text string to a byte array.
        Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)

        ' Create the stream.
        Dim ms As New System.IO.MemoryStream
        ' Create the decoder to write to the stream.
        Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

        ' Use the crypto stream to write the byte array to the stream.
        decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
        decStream.FlushFinalBlock()

        ' Convert the plaintext stream to a string.
        Return System.Text.Encoding.Unicode.GetString(ms.ToArray)

    End Function

    Public Function checkData(ByVal plainText As String, ByVal encryptedText As String) As Boolean

        Return plainText.Equals(DecryptData(encryptedText))

    End Function

End Class

Programmatically Change Monitor Display on Windows 7 or 8 using VB.Net

In order to change monitors, it is useful to know what process to be launched to switch displays, the path where the process to be called, and the parameters to pass to the command.

First, we need to know what will be the process to be called. “DisplaySwitch.exe” is the application that runs as a process to change between monitors. It can be launched through several methods and usually can be found on the system folder.

Next, we will get the parameters for our command:

  1. PC Screen Only – "/internal"
  2. Duplicate – "/clone"
  3. Extend – "/extended"
  4. Second Screen Only – "/external"


These four are selections for our displays. Each has its own parameter as a command for the process. For example, if we send a command to the system "DisplaySwitch.exe /clone" with spaces in between, the display will duplicate itself to all monitors.

Now we know how to change the display of our monitor. Next is to run the command programmatically.

The command shell on windows can also be used in vb.net by using "Shell" with 3 parameters but the last 2 are optional. This time, we will just use 1 parameter with data type string as the path name and as the name of the program to be executed. The syntax is:

Shell(PathName)

This time, we want to duplicate the display of our primary monitor so we need to use the complete command with a parameter. The complete code is:

Shell("DisplaySwitch.exe /clone")

Don’t forget that there’s a space between the program name and the parameter.

If we try to build and run our program in vb.net having this code, our screen will be displayed on both monitors.

Creating a Window Using JFrame in Java

Here's an example of a simple window in java:

Java JFrame Window (JFrame size - 300x280)
Here's the code:


import javax.swing.JFrame;

public class MyClass extends JFrame{
   
    public MyClass(){
        setSize(800,600);
        setTitle("JavaJFrameWindow1");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
   
    public static void main(String[] args){
        new MyClass();
    }
} 



 You can modify the window by adding or changing the JFrame's attributes in the constructor.


Custom Cursor Using an Icon in Visual Basic 2012

Here's an example and some code options in using a custom cursor in an ".ico" format in Visual Basic 2012:

Customized cursor in run-time
Option 1:

Dim myIcon As Icon
Dim myCursor As Cursor

myIcon = New Icon("C:\myIconFileName.ico")
myCursor = New Cursor(myIcon.Handle)

Me.Cursor = myCursor


Option 2:


Dim myIcon As New Icon("C:\myIconFileName.ico")
Dim myCursor As New Cursor(myIcon.Handle)

Me.Cursor = myCursor


Option 3:

Dim myIcon As New Icon("C:\myIconFileName.ico")

Me.Cursor = New Cursor(myIcon.Handle)


Option 4:

Me.Cursor = New Cursor(New Icon("C:\myIconFileName.ico").Handle)