ARTICLE

System.Security.Cryptography Namespace in VB.NET

Posted by Dinesh Beniwal Articles | Visual Basic Language September 13, 2010
In this article I will explain you about System.Security.Cryptography Namespace in VB.NET.
 
Reader Level:

The System.Security.Cryptography namespace contains support for the most common symmetric (DES, 3DES, RC2, Rijndael), asymmetric (RSA, DSA), and hash (MD5, SHA-1, SHA-256, SHA- 384, SHA-512) cryptography algorithms. It also includes a helpful class to encrypt and decrypt streams. You can use this class, called CryptoStream, in combination with other stream classes or you can use several CryptoStream class objects together. With a little effort, you can feed the output of one CryptoStream object into another CryptoStream object.
 
 Table 22.6 lists the classes and interfaces that are part of the System.Security.Cryptography namespace of the VB.NET Framework base class library. These classes and interfaces define the abstract object model for encryption algorithms within the VB.NET Framework. New algorithms may be added to the VB.NET Framework by subclassing and/or implementing a portion of these classes and interfaces.
 
 Table 22.6: Classes and Interfaces in the System.Security.Cryptography Namespace

Table-22.6.gif
 
 Listing 22.35 illustrates the use of DES and CryptoStream classes to encipher and decipher a file.
 
 Listing 22.35: Cryptostream1.VB, CryptoStream with DES
 
 Imports System.IO
 Imports System.Security
 Imports System.Security.Cryptography
 Imports System.Text
 Class CryptoDESSample
     Shared Sub Main(ByVal args As String())
         Dim fsInput As New FileStream("c:\myfile.txt", FileMode.Open, FileAccess.Read)
         Dim fsCiphered As New FileStream("c:\ciphered.txt", FileMode.Create, FileAccess.Write
         Dim ourDESProvider As New DESCryptoServiceProvider()
         ' our 8 byte DES secret key is 12345678
         ourDESProvider.Key = ASCIIEncoding.ASCII.GetBytes("12345678")
         ourDESProvider.IV = ASCIIEncoding.ASCII.GetBytes("12345678")
         Dim des1 As ICryptoTransform = ourDESProvider.CreateEncryptor()
         Dim des2 As ICryptoTransform = ourDESProvider.CreateDecryptor()
         Dim cryptostream1 As New CryptoStream(fsCiphered, des1, CryptoStreamMode.Write)
         Dim bytearrayinput As Byte() = New Byte(fsInput.Length - 1) {}
         fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
         cryptostream1.Write(bytearrayinput, 0, bytearrayinput.Length)
         cryptostream1.Close()
         fsInput.Close()
         fsCiphered.Close()
         Dim fsread As New FileStream("c:\ciphered.txt", FileMode.Open, FileAccess.Read)
         Dim cryptostream2 As New CryptoStream(fsread, des2, CryptoStreamMode.Read)
         Dim fsEnciphered As New StreamWriter("c:\enciphered.txt")
         fsEnciphered.Write(New StreamReader(cryptostream2).ReadToEnd())
         fsEnciphered.Flush()
         fsEnciphered.Close()
     End Sub
 End Class
 
 The code in Listing 22.36 uses the SHA1 algorithm to compute the hash value of a given string.
 
Listing 22.36: Hash1.VB, Hashing a String Using SHA-1 Algorithm
 
 Imports System.IO
 Imports System.Security
 Imports System.Security.Cryptography
 Public Class Class1
     Shared Sub Main(ByVal args As String())
         Dim str1 As [String] = "MCBinc"
         Dim char1a As [Char]() = str1.ToCharArray()
         Dim byte1a As [Byte]() = New [Byte](char1a.Length - 1) {}
         For i As Integer = 0 To byte1a.Length - 1
             byte1a(i) = CType(AscW(char1a(i)), [Byte])
         Next
         ' create hash value from str1 using SHA1 instance
         ' returned by CryptoConfig 
         Dim hash1 As Byte() = DirectCast(CryptoConfig.CreateFromName("SHA1"), HashAlgorithm
         .ComputeHash(byte1a)
         ' or you can use directly created instance of the SHA1 class
         ' byte[] hash1 = (new SHA1CryptoServiceProvider()).ComputeHash(byte1a );
         Console.WriteLine(str1 & " hashed Value is <" & BitConverter.ToString(hash1) & ">")
         Console.ReadLine()
     End Sub
 End Class
 
Output Window

sscn1.gif

Conclusion
 
Hope this article would have helped you in understanding System.Security.Cryptography Namespace in VB.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor