VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
 

Code Snippets

  

VB.NET Source Code



RSA Encryption/Decryption with salt

Takes todays date in specified format encrypts it and saves encrypted date and encryption key to registry. If registrykey exists takes encrypted string from registry and decrypts it.

Submitted By: TEH
Actions:
Rating:
Views: 5,870

Language: VB.NET

Last Modified: March 27, 2009
Instructions: Save this snippet as a class (for example crypting.vb).

Now you can use that class in any of your projects just add it in.

Now you can start using it:
TEST PROJECT
Make a new Windows Forms Project.
Add 2 textboxes and 1 button to your form.
add just created class to your project
(Project-add existing item-browse and select just created class).
double click your button-component and add next code

CODE

Public Class Form1
'Declare class for use
Dim crypto As New Crypting

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' now you can use it like
crypto.Start() 'starts encrypting/decrypting
TextBox1.Text = crypto.encrypting.ToString() ' shows encrypted text.
TextBox2.Text = crypto.decrypting.ToString() ' shows original/decrypted text

End Sub
End Class


Next time you start this application it will find encrypted date from registry and show it to you.

Snippet


  1. Imports System.Security.Cryptography
  2. Imports System.Text
  3.  
  4. Public Class Crypting
  5.     'declare variables
  6.     Dim tday, encrypted, decrypted As String 'tday is date when program was first run.
  7.     'encrypted is string readed from registry.
  8.     Dim encoder As New UTF8Encoding
  9.     Dim salt As String = "salt" 'salt is a string added to tday before encrypting and removed before showing decrypted text.
  10.     Dim rsa As New RSACryptoServiceProvider
  11.  
  12.     Public Sub Start() 'start encrypting.
  13.  
  14.         tday = Today.ToString("dd.MM.yyyy") 'Set date at specified format to tday
  15.         encrypted = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\" & "trial", "trial", Nothing) 'get string from registry.
  16.  
  17.         If encrypted = Nothing Then 'Check if string is empty.
  18.             encrypting()
  19.             My.Computer.Registry.CurrentUser.CreateSubKey("trial") 'set key to save encrypted date.
  20.             My.Computer.Registry.SetValue("HKEY_CURRENT_USER\" & "trial", "trial", encrypted, Microsoft.Win32.RegistryValueKind.String) 'Set value to key.
  21.         Else
  22.             decrypting()
  23.         End If
  24.  
  25.     End Sub
  26.  
  27.     Public Function encrypting()
  28.  
  29.         Dim bytes, toencrypt As Byte()
  30.         My.Computer.Registry.SetValue("HKEY_CURRENT_USER\" & "trial", "key", rsa.ToXmlString(True), Microsoft.Win32.RegistryValueKind.String) 'Set value to key.set string used to encrypt data to "key" in registry
  31.         bytes = encoder.GetBytes(tday & salt) 'get bytes of string to encrypt
  32.         toencrypt = rsa.Encrypt(bytes, True) 'encrypt string
  33.         encrypted = Convert.ToBase64String(toencrypt) 'converts encrypted bytes to string for showing.
  34.  
  35.  
  36.         Return encrypted
  37.  
  38.     End Function
  39.  
  40.     Public Function decrypting()
  41.  
  42.         Dim bytes, todecrypt As Byte()
  43.         Dim decrypted As String
  44.         bytes = Convert.FromBase64String(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\" & "trial", "trial", Nothing)) 'converts string from registry to bytes.
  45.         rsa.FromXmlString(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\" & "trial", "key", Nothing)) 'Get key from registry.
  46.         todecrypt = rsa.Decrypt(bytes, True) 'Decrypting.
  47.         decrypted = encoder.GetString(todecrypt).Remove(10, salt.Length) 'Convert decrypted bytes to string and removes salt. 10 is the lenght of original string.
  48.         Return decrypted
  49.     End Function
  50.  
  51. End Class
  52.  

Copy & Paste


Comments

TEH 2009-03-10 14:58:41

Of course you can use that class to encrypt any string just put it into tday variable.

Bort 2009-03-13 05:25:35

Nice one TEH :)

TEH 2009-03-13 12:25:54

Thanks Bort


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.