Article ID: 317535
Article Last Modified on 3/29/2007
APPLIES TO
- Microsoft Office Access 2003
- Microsoft Access 2002 Standard Edition
- Microsoft Office Excel 2003
- Microsoft Excel 2002 Standard Edition
- Microsoft Office PowerPoint 2003
- Microsoft PowerPoint 2002 Standard Edition
- Microsoft Office Word 2003
- Microsoft Visual Studio .NET 2003 Professional Edition
- Microsoft Word 2002 Standard Edition
- Microsoft Visual Studio .NET 2002 Professional Edition
This article was previously published under Q317535
IN THIS TASK
SUMMARY
Use this step-by-step guide to call a .NET class library from Visual Basic for Applications. This sample shows how a Visual Basic for Applications program can use a Visual Basic .NET class library to encrypt and decrypt a string. You can use the cryptography namespace, included in the .NET Framework, for the encryption/decryption.
back to the top
Create the Visual Basic .NET Class Library
- Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Visual Basic Projects, select Class Library. Name the class CryptoClass and click OK. Class1 is created by default.
Replace the contents of Class1 with the following code:
Imports System.Security.Cryptography <ComClass(Class1.ClassId, Class1.InterfaceId, Class1.EventsId)> Public Class Class1 Public Const ClassId As String = "98349785-8BE2-4604-848D-F5B103D61715" Public Const InterfaceId As String = "36613EE9-125F-493d-9968-771E18C2226A" Public Const EventsId As String = "A036F02F-F87E-4548-A536-7DD7EA8E62B5" Const sKey As String = "MyKey" Public Function EncryptTripleDES(ByVal sIn As String) As String Dim DES As New TripleDESCryptoServiceProvider() Dim hashMD5 As New MD5CryptoServiceProvider() ' Compute the MD5 hash. DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey)) ' Set the cipher mode. DES.Mode = CipherMode.ECB ' Create the encryptor. Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor() ' Get a byte array of the string. Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(sIn) ' Transform and return the string. Return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)) End Function Public Function DecryptTripleDES(ByVal sOut As String) As String Dim DES As New TripleDESCryptoServiceProvider() Dim hashMD5 As New MD5CryptoServiceProvider() ' Compute the MD5 hash. DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey)) ' Set the cipher mode. DES.Mode = CipherMode.ECB ' Create the decryptor. Dim DESDecrypt As ICryptoTransform = DES.CreateDecryptor() Dim Buffer As Byte() = Convert.FromBase64String(sOut) ' Transform and return the string. Return System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)) End Function End Class
Note You can use the GUID generator to generate new GUIDs for the ClassId, InterfaceId, and EventsId. To generate new GUIDs, click Create GUID on the Tools menu.- From Project Properties, select Configuration Properties. Click Build and then select the Register for COM Interop check box. Click OK.
- On the Build menu, click Build Solution to create the DLL.
Create a Visual Basic for Applications Macro to Call The .NET Class Library
- Start Microsoft Excel. Press ALT+F11 to start the Visual Basic Editor.
- On the Insert menu, click Module to insert a blank module.
- On the Tools menu, click References. Add a reference to the CryptoClass library and click OK.
Type or paste the following code in the Module1 code window:
Sub TestCrypto() Dim oCrypto As New CryptoClass.Class1 Dim sCrypt As String sCrypt = oCrypto.EncryptTripleDES("This is a test") MsgBox "Encrypted text = " & sCrypt MsgBox "Decrypted text = " & oCrypto.DecryptTripleDES(sCrypt) End Sub
Test the Code
- On the Tools menu, click Macros. In the list of macros, click TestCrypto, and then click Run.
- If a message box appears showing the encrypted string, click OK. A second message box appears showing the decrypted string.
- If the class library needs to be registered on another computer running .NET Framework, copy the DLL to the system and run the following command:
regasm CryptoClass.dll /tlb:CryptoClass.tlb
REFERENCES
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
301070 HOW TO: Encrypt and Decrypt a File by Using Visual Basic .NET
For more information, see the following Microsoft Developer Network (MSDN) Web site:
Additional query words: crypto vba
Keywords: kbcrypt kbsecurity kbhowtomaster KB317535