Visual Studio : Storing and Retrieving Data from Registry

While creating desktop applications many times you need to save some application data in the registry. This removes application dependency on the files for configuration or one-off information. This code does not need any additional library to be added and the developer can create their own path for storing key-value pairs.

Saving Data in Windows Registry:- 

SaveSetting(Application Name, Section Name, Key , Value);

e.g . SaveSetting(“simpleDesktopApplication”,”Configuration Item”, “UserName”,”Sushant”);

Retrieving Data from Windows Registry:- 

reutrnValue = GetSetting (Application Name, Section Name, Key);

e.g . strUserName = GetSetting (“simpleDesktopApplication”,”Configuration Item”, “UserName”);

Advertisements

Visual Studio : Simple 3DES Encryption Working Example

For a long period of time, I was using my own code encryption techniques as part of login validations for the standalone desktop applications. But recently I was looking for high level of encryption for storing login information. This can also be used to create licence keys. Below code provided two-way encryption.It needs the private key string as the wrapper to generate unique decrypted string.

Main Form code : – 


Public Class frmMain

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtEncryptedValue.Text = “”
txtInputKey.Text = “”

End Sub

Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
If Len(Trim(txtInputKey.Text)) > 3 Then

‘Wrapper text is similer to the private key which can be any random string
Dim wrapper As New Simple3Des(“dusyifdsuy2787%45^hffhj%$*kfhk*gGF4982798^^^879hfsjsbdkjfdkjjfdklmbmbmvefhkhwhelh“)
‘Dim cipherText As String = wrapper.EncryptData(nics(0).GetPhysicalAddress.ToString)
Dim strInputText As String
strInputText = “”
For i = 1 To 10
strInputText = strInputText & Trim(txtInputKey.Text)
Next i
txtEncryptedValue.Text = wrapper.EncryptData(strInputText)
Else
MsgBox(“No Information To Encrypt”, MsgBoxStyle.Critical, “License Maker”)
End If
End Sub

Private Sub btnDecrypt_Click(sender As Object, e As EventArgs) Handles btnDecrypt.Click
If Len(Trim(txtInputKey.Text)) > 3 Then

Dim wrapper As New Simple3Des(“dusyifdsuy2787%45^hffhj%$*kfhk*gGF4982798^^^879hfsjsbdkjfdkjjfdklmbmbmvefhkhwhelh“)
‘Dim cipherText As String = wrapper.EncryptData(nics(0).GetPhysicalAddress.ToString)
Dim strInputText As String
Dim intDecryptLength As Integer
strInputText = Trim(txtInputKey.Text)
‘For i = 1 To 10
‘ strInputText = strInputText & UCase(Trim(txtInputKey.Text))
‘Next i
intDecryptLength = Len(wrapper.DecryptData(strInputText)) / 10
txtEncryptedValue.Text = Mid(wrapper.DecryptData(strInputText), 1, intDecryptLength)
Else
MsgBox(“No Information To Decrypt”, MsgBoxStyle.Critical, “License Maker”)
End If
End Sub
End Class


Encryption Module :– 


Imports System.Security.Cryptography
Public NotInheritable 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
End Class

——————————————————————-

Welcome to My Page

This is my first post on my personal website. 🙂

In this blog, you can find various commands, queries, tips, tricks and information related to Unix, SQL, Oracle, Linux, Aix, Shell Scripting, Java, Javascript, Jquery, Servlets, Web servers, ITIL Framework, Support tools and discussions on the latest digital announcements.

Always welcome for suggestions and feedback.