My Interview with some of the great VB.NET programmer in MSDN Website. It great to be in the list with leading programmer of VB.NET.
http://blogs.msdn.com/vbteam/pages/i-m-a-vb-gaurav-khanna.aspx
My Interview with some of the great VB.NET programmer in MSDN Website. It great to be in the list with leading programmer of VB.NET.
http://blogs.msdn.com/vbteam/pages/i-m-a-vb-gaurav-khanna.aspx
In this article I have code which can be used encrypt the text before saving it in the file, database, registry, etc to protect from misuse. There are many article we can find on internet to encrypt/decrypt text in .Net. Each have different ways for encryption with some having very long code. So I have created a very simple two functions to encrypt/decrypt a text in .Net. Both the functions contains two parameter. First parameter “Text” will be the text which need to encrypted and second parameter is “Key” which is a private key which will help identity during decrypt. So key during decrypt should be same as in encypt to decrypt the text correctly.
Code to encrypt the Text
Imports System.Security.Cryptography Imports System.Text Imports System.IO Function Encrypt(ByVal text As String, ByVal key As String) As String Try Dim crp As New TripleDESCryptoServiceProvider Dim uEncode As New UnicodeEncoding Dim bytPlainText() As Byte = uEncode.GetBytes(text) Dim stmCipherText As New MemoryStream Dim slt() As Byte = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Dim pdb As New Rfc2898DeriveBytes(key, slt) Dim bytDerivedKey() As Byte = pdb.GetBytes(24) crp.Key = bytDerivedKey crp.IV = pdb.GetBytes(8) Dim csEncrypted As New CryptoStream(stmCipherText, crp.CreateEncryptor(), CryptoStreamMode.Write) csEncrypted.Write(bytPlainText, 0, bytPlainText.Length) csEncrypted.FlushFinalBlock() Return Convert.ToBase64String(stmCipherText.ToArray()) Catch ex As Exception Throw End Try End Function
So for example Encrypt(“Demo”, “12345″) will return 1T5yxecmgLbsxQu4iQx5Bg==”
Code to decrypt the Text
Function Decrypt(ByVal text As String, ByVal key As String) As String Dim crp As TripleDESCryptoServiceProvider Try crp = New TripleDESCryptoServiceProvider Dim uEncode As New UnicodeEncoding Dim bytCipherText() As Byte = Convert.FromBase64String(text) Dim stmPlainText As New MemoryStream Dim stmCipherText As New MemoryStream(bytCipherText) Dim slt() As Byte = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Dim pdb As New Rfc2898DeriveBytes(key, slt) Dim bytDerivedKey() As Byte = pdb.GetBytes(24) crp.Key = bytDerivedKey crp.IV = pdb.GetBytes(8) Dim csDecrypted As New CryptoStream(stmCipherText, crp.CreateDecryptor(), CryptoStreamMode.Read) Dim sw As New StreamWriter(stmPlainText) Dim sr As New StreamReader(csDecrypted) sw.Write(sr.ReadToEnd) sw.Flush() csDecrypted.Clear() crp.Clear() Return uEncode.GetString(stmPlainText.ToArray()) Catch ex As Exception Throw End Try End Function
So now when we decrypt the text encrypted above we will get the required text
So Decrypt(“1T5yxecmgLbsxQu4iQx5Bg==”, “12345″) will return “Demo”
Recently one of my friend wanted to send email containing embedded image, similarly like we can do using Outlook. So I decided to create an example for him. To embed external resource in email we can use LinkedResource class provide in System.Net.Mail. In this article we will send email containing embedded image with smtp server as gmail.
Code:
Private Sub SendEmail() Dim mail As New MailMessage("fromAddress@gmail.com", "toAddress@gmail.com") mail.Subject = "This is an embedded image mail" 'create the image resource from image path using LinkedResource class.. Dim imageResource1 As New LinkedResource("C:\MyPhotos\Photo1.jpg", "image/jpeg") imageResource1.ContentId = "uniqueId1" imageResource1.TransferEncoding = TransferEncoding.Base64 Dim imageResource2 As New LinkedResource("C:\MyPhotos\Photo2.jpg", "image/jpeg") imageResource2.ContentId = "uniqueId2" imageResource2.TransferEncoding = TransferEncoding.Base64 Dim htmlBody As New StringBuilder htmlBody.AppendLine("") htmlBody.AppendLine("<b>LIST OF PHOTOS</b>") htmlBody.AppendLine("</br></br>") htmlBody.AppendLine("PHOTO 1") htmlBody.AppendLine("<img alt="""" hspace=0 src=""cid:uniqueId1"" align=baseline border=0 >") htmlBody.AppendLine("</br></br>") htmlBody.AppendLine("PHOTO 2") htmlBody.AppendLine("<img alt="""" hspace=0 src=""cid:uniqueId2"" align=baseline border=0 >") htmlBody.AppendLine("</br></br>") Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(htmlBody.ToString, Nothing, "text/html") 'adding the imaged linked to htmlView... htmlView.LinkedResources.Add(imageResource1) htmlView.LinkedResources.Add(imageResource2) mail.AlternateViews.Add(htmlView) Dim smtp As New SmtpClient("smtp.gmail.com", 587) 'If smtp server requires SSL smtp.EnableSsl = True smtp.Credentials = New NetworkCredential("fromAddress@gmail.com", "SMTP Password") smtp.Send(mail) End Sub
When we open MessageBox in a WPF application, it looks are very similar to that of a Window Form. We cannot inherits or apply style to MessageBox. Using MessageBox in a WPF application with a rich user interface can look bad. So in this article we will create our own WPFMessageBox with rich user interface.
WPFMessageBox is very similar to MessageBox in WPF with added functionality and rich user interface. We can apply two different skin to WPFMessageBox just by setting Skin property.
WPFMessageBox with Black Skin
WPFMessageBox with Blue Skin
Code to apply Black Skin and Blue Skin to WPFMessageBox is as below
'Code to apply Black Skin WpfMessageBox.Skin = DisplaySkin.Black 'Code to apply Blue Skin WpfMessageBox.Skin = DisplaySkin.Blue
Just like MessageBox, WPFMessageBox also has Show() method with various overloads to set caption, text, icon, buttons and default result.
Various overloaded functions for WPFMessageBox are shown in following code.
Function Show(ByVal text As String) As WpfMessageBoxResult Function Show(ByVal text As String, ByVal caption As String) As WpfMessageBoxResult Function Show(ByVal text As String, ByVal caption As String, ByVal button As WpfMessageBoxButton) As WpfMessageBoxResult Function Show(ByVal text As String, ByVal caption As String, ByVal button As WpfMessageBoxButton, ByVal icon As WpfMessageBoxImage) As WpfMessageBoxResult Function Show(ByVal text As String, ByVal caption As String, ByVal button As WpfMessageBoxButton, ByVal icon As WpfMessageBoxImage, ByVal defaultResult As WpfMessageBoxResult) As WpfMessageBoxResult
All these overload functions are shared. So we can use them without creating their object.
Above example will show WPFMessageBox with “Message box with a text and Caption” as text and “WPF Message Box” as caption.
Above example will show “Message with Yes and No” as text, “WPF Message Box” as caption and will contain Yes and No button.
Code for WPFMessageBox.Show have similar overload parameters to MessageBox.Show with great user interface. So one familiar to MessageBox can easily use WPFMessageBox
You can download the code from below link
Download Source Code